If Statements
The if
statement is used for conditional execution. It allows you to execute a block of code only if a specified condition is true.
Syntax:
if condition:
# code block to execute if condition is true
Example:
age = 18
if age >= 18:
print("You are an adult.")
Else Statements
The else
statement follows an if
statement and executes a block of code if the condition in the if
statement is false.
Syntax:
if condition:
# code block to execute if condition is true
else:
# code block to execute if condition is false
Example:
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Elif Statements
The elif
(else if) statement allows you to check multiple expressions for True
and execute a block of code as soon as one of the conditions is True
. It's used to avoid nested if...else
statements.
Syntax:
if condition1:
# code block to execute if condition1 is true
elif condition2:
# code block to execute if condition2 is true
else:
# code block to execute if none of the above conditions are true
Example:
age = 20
if age < 13:
print("You are a child.")
elif age < 18:
print("You are a teenager.")
else:
print("You are an adult.")
Nested If Statements
An if
statement can be nested inside another if
statement to allow for more complex conditions.
Syntax:
if condition1:
if condition2:
# code block to execute if both condition1 and condition2 are true
Example:
age = 25
if age >= 18:
if age < 21:
print("You are an adult but not old enough to drink alcohol in the US.")
else:
print("You are an adult and old enough to drink alcohol in the US.")
For Loops
The for
loop is used for iterating over a sequence (e.g., list, tuple, dictionary, set, or string).
Syntax:
for variable in sequence:
# code block to execute for each item in the sequence
Example:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
Using range()
:
for i in range(5):
print(i)
While Loops
The while
loop allows code to be executed repeatedly based on a given Boolean condition. The loop continues to execute as long as the condition is True
.
Syntax:
while condition:
# code block to execute as long as condition is true
Example:
count = 0
while count < 5:
print(count)
count += 1
Break and Continue Statements
break
: Terminates the loop and exits it.continue
: Skips the rest of the code inside the loop for the current iteration and moves to the next iteration.
Break Example:
for i in range(10):
if i == 5:
break
print(i)
Continue Example:
for i in range(10):
if i % 2 == 0:
continue
print(i)
Nested Loops
Loops can be nested inside other loops. The inner loop will complete all its iterations for each iteration of the outer loop.
Syntax:
for outer_variable in outer_sequence:
for inner_variable in inner_sequence:
# code block to execute for each combination of outer and inner variables
Example:
for i in range(3):
for j in range(2):
print(f"i: {i}, j: {j}")
Loop with Else
An else
clause can be used with a for
or while
loop. The code inside the else
block is executed only if the loop is not terminated by a break
statement.
Syntax:
for variable in sequence:
# code block
else:
# code block to execute if the loop wasn't terminated by a break
Example:
for i in range(5):
if i == 3:
break
print(i)
else:
print("Loop completed without break")