Like other programming languages, Python also includes conditional statements. But the only difference is that instead of else if, we have elif.
Conditional statements control the flow of a program based on specific conditions. They enable decision-making by allowing the program to execute different blocks of code depending on whether a condition evaluates to True or False.
instead of explaining the if,elif and else individually let us cover them all in a single example.
if a%2==0: print("The Number is an Even Composite") elif not_prime(a): print("The Number is an Odd Composite") else: print("The Number is a Prime")
here, let the number be 3.
First the program will check whether the number is divisible by 2 (if a%2==0)
since it is not even, it goes to elif satement(if not_prime(a))
since neither the if, nor the elif are not true, the program will go to the else part and it will print:
The Number is a Prime
age=19 if age>18 and age<25: print("the person is an Young Adult")
You can nest conditional statements within one another to evaluate complex conditions.
age = 20 if age >= 18: if age < 25: print("You are a young adult.") else: print("You are an adult.") else: print("You are not an adult yet.")
bob_score=87 alen_score=92 answer=bob_score if bob_score>alen_score else alen_score print(answer)
Answer:92
startswith() and endswith()
Let’s say you want to return all the names in a list that starts with
"a."
here is how you would use startswith() to accomplish that.
Using startswith():
listl = ['lemon','Orange','apple', 'apricot'] new_list = [i for i in listl if i.startswith('a')] pri nt(new_li st)
Answer: ['apple', 'apricot']
listl = ['lemon','Orange','apple', 'apricot'] new_list = [i for i in listl if i.endswith('e')]] pri nt(new_li st)
Answer: ['apple', 'Orange']
In Addition to decision-making statements, Python programming also supports looping statements. There are
1. while
2. for
The for loop in Python iterates over a sequence (such as a list, tuple, string, or range) and performs an operation for each item in that sequence.
a=[1,2,3,4] for i in a: print(a)
Answer: 0n 1n 2n 3n 4n
Here, the for loop iterates through all the elements in the list a and prints them.
Using range() with for:
You can use the range() function to generate a sequence of numbers.
if a%2==0: print("The Number is an Even Composite") elif not_prime(a): print("The Number is an Odd Composite") else: print("The Number is a Prime")
Answer: 0n 1 n 2n 3n
Range():
The basic syntax of the range() function is:
age=19 if age>18 and age<25: print("the person is an Young Adult")
here start=0 and step=1 by default.
age = 20 if age >= 18: if age < 25: print("You are a young adult.") else: print("You are an adult.") else: print("You are not an adult yet.")
Answer:1n 2n
1n 3n
The while loop continues to execute the block of code as long as the condition evaluates to True.
bob_score=87 alen_score=92 answer=bob_score if bob_score>alen_score else alen_score print(answer)
Answer: 4n 3n 2n 1n
The break statement is used to terminate a loop prematurely, regardless of its condition. Once the break statement is executed, the control exits the loop.
listl = ['lemon','Orange','apple', 'apricot'] new_list = [i for i in listl if i.startswith('a')] pri nt(new_li st)
Answer: 10n 9n 8n 7n 6n
The continue statement is used to skip the rest of the code in the current iteration and proceed to the next iteration of the loop.
listl = ['lemon','Orange','apple', 'apricot'] new_list = [i for i in listl if i.endswith('e')]] pri nt(new_li st)
Answer: 1n 3n 5n 7n 9n
The pass statement is a placeholder used when a block of code is syntactically required but you don't want to execute any code. It literally does nothing.
a=[1,2,3,4] for i in a: print(a)
Answer: 0n 1n 2n 4n
The above is the detailed content of Day Mastering the Art of Conditional Statements and Loops. For more information, please follow other related articles on the PHP Chinese website!