Conditions
In Python, conditionals are instructions that let you execute code blocks selectively, depending on whether certain conditions are met. These code blocks can be executed if the condition is true, or omitted if the condition is false.
How do they work?
Section titled “How do they work?”This statement evaluates a condition. If the condition is true, the code block that follows the if statement is executed.
This statement is executed if the condition of the if statement is false.
age = 25if age >= 18:print('You are an adult')else:print('You are a minor')
# If age is greater than or equal to 18, it will print 'You are an adult'.# If age is less than 18, it will print 'You are a minor'.
elif (else if):
Section titled “elif (else if):”Allows you to evaluate multiple conditions in sequence. If the if condition is false, the elif condition is evaluated. If the elif condition is true, the corresponding code block is executed.
age = 25if age > 18:print('You are over 18')elif age == 18:print('You are 18 years old')else:print('You are under 18')
# If age is greater than 18, it will print 'You are over 18'.# If age is equal to 18, it will print 'You are 18 years old'.# If age is less than 18, it will print 'You are under 18'.
Handling Logical Operators
Section titled “Handling Logical Operators”As we saw in the Operators section, logical operators are used to combine multiple conditions and get a boolean result, either True
or False
.
age = 25has_identification = True
if age >= 18 and has_identification:print('You can enter')else:print('You cannot enter')
# Both conditions must be met. If age is greater than or equal to 18 **and** has identification,# it prints 'You can enter'.
is_student = Falseis_teacher = True
if is_student or is_teacher:print('You have access to campus')else:print('Access denied')
# It's enough for one of the conditions to be true for it to print 'You have access to campus'.
has_fines = False
if not has_fines:print('You can renew your license')else:print('You must pay the fines first')
# 'not' inverts the value. If there are no fines, it prints 'You can renew your license'.
Chained Logical Operations
Section titled “Chained Logical Operations”In Python, you can chain logical operations, but you must consider their precedence. Precedence defines the order of evaluation.
In Python: first not
, then and
, and finally or
. Therefore, it is recommended to use parentheses to group operations.
has_identification = Trueis_student = Truehas_fines = Trueage = 20
# Without parenthesesif not has_fines and age >= 18 and has_identification or is_student:print('You can enter')else:print('You cannot enter')# This prints 'You can enter'
# With parenthesesif not has_fines and (age >= 18 and (has_identification or is_student)):print('You can enter')else:print('You cannot enter')# This prints 'You cannot enter'
Nested Conditions
Section titled “Nested Conditions”In Python, nested conditionals are if statements inside other if, elif or else statements. This allows evaluating several conditions hierarchically, creating a more complex execution flow based on the combination of conditions.
is_member = Falseage = 15
if is_member:if age >= 15:print('You have access since you are a member and over 15')else:print('You have access but are under 15')else:print('You are not a member')
# In this case: not a member therefore no access, despite meeting the condition of being 15 years old.