Operator Precedence in Python
Flow Control Statements in Python
Flow control statements determine the order in which instructions are executed in a program. Python
provides several flow control statements:
1. Conditional Statements (Decision Making):
Used to execute code based on conditions.
- if statement:
if condition:
# code block
- if-else statement:
if condition:
# code block
else:
# else block
- if-elif-else ladder:
if condition1:
# code block
elif condition2:
# code block
else:
# else block
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
Operator Precedence in Python
else:
print("You are not eligible.")
2. Looping Statements (Iteration):
Used to repeat a block of code multiple times.
- for loop:
for item in sequence:
# code block
- while loop:
while condition:
# code block
Example:
for i in range(5):
print(i) # Prints 0 to 4
count = 0
while count < 5:
print(count)
count += 1
3. Loop Control Statements:
Used to alter the behavior of loops.
- break: Exits the loop immediately.
- continue: Skips the current iteration and moves to the next.
- pass: Placeholder that does nothing (used when a statement is required syntactically).
Example:
Operator Precedence in Python
for i in range(5):
if i == 3:
break
print(i) # Prints 0, 1, 2