Conditionals let a program make decisions! Different actions depending on the situation.
๐ฏ Why Do We Need Conditionals?
Without conditionals, a program always does the same thing. With conditionals โ it can change its behavior!
# Without conditionals โ always the same action
print('Welcome!')
# With conditionals โ different actions
age = 16
if age >= 18:
print('Welcome!')
else:
print('Access only for 18+')
โ Simple if
Runs code only when the condition is True.
age = 20
if age >= 18:
print('You are an adult')
print('You can vote')
print('Program finished')
Important: Indentation in Python is mandatory! 4 spaces.
# โ No indentation โ error!
if age >= 18:
print('Error!')
# โ
With indentation
if age >= 18:
print('Correct!')
๐ if-else
One action if True, another if False.
temperature = 25
if temperature > 20:
print('It\'s warm! ๐')
else:
print('It\'s cold! ๐ฅถ')
More examples
# Even or odd
number = 7
if number % 2 == 0:
print('Even')
else:
print('Odd')
# Access control
password = input('Password: ')
if password == 'secret123':
print('โ
Access granted')
else:
print('โ Wrong password')
๐ข Comparison Operators
x = 10
y = 5
# Equal
if x == y:
print('Equal')
# Not equal
if x != y:
print('Not equal') # Prints
# Greater than
if x > y:
print('x is greater') # Prints
# Less than
if x < y:
print('x is less')
# Greater than or equal
if x >= 10:
print('x >= 10') # Prints
# Less than or equal
if y <= 5:
print('y <= 5') # Prints
๐ All Operators
== # Equal
!= # Not equal
> # Greater than
< # Less than
>= # Greater than or equal
<= # Less than or equal
๐ฎ Practical Examples
Age check
print('๐ฎ Age Check\n')
age = int(input('How old are you? '))
if age >= 18:
print('โ
You can play this game')
print('All features unlocked')
else:
print('โ This game is for adults')
print('Try a different game')
Test score
print('๐ Test Results\n')
score = int(input('Your score (0-100): '))
if score >= 60:
print('โ
Passed!')
print('Congratulations!')
else:
print('โ Not passed')
print('Please try again')
Discount calculator
print('๐๏ธ Discount Calculator\n')
price = float(input('Item price: '))
is_vip = input('VIP customer? (yes/no): ').lower()
if is_vip == 'yes':
discount = price * 0.2 # 20% discount
final_price = price - discount
print(f'\n๐ VIP discount: {discount}')
print(f'Total: {final_price}')
else:
print(f'\nTotal: {price}')
print('Become a VIP for a 20% discount!')
Guess the number (simple version)
import random
secret = random.randint(1, 10)
guess = int(input('Guess a number from 1 to 10: '))
if guess == secret:
print('๐ Correct!')
else:
print(f'๐ข Wrong! It was {secret}')
โก Common Mistakes
Mistake 1: = instead of ==
# โ Assignment instead of comparison
age = 18
if age = 18: # SyntaxError!
print('test')
# โ
Correct
if age == 18:
print('test')
Mistake 2: Indentation
# โ No indentation
if age >= 18:
print('test') # IndentationError!
# โ Mixed indentation
if age >= 18:
print('test') # 4 spaces
print('test2') # 2 spaces โ error!
# โ
Consistent indentation
if age >= 18:
print('test')
print('test2')
Mistake 3: Forgot the colon
# โ No colon
if age >= 18 # SyntaxError!
print('test')
# โ
With colon
if age >= 18:
print('test')
Mistake 4: Comparing a string with a number
# โ input() returns a string!
age = input('Age: ') # '18' (string!)
if age >= 18: # TypeError!
print('test')
# โ
Convert to a number
age = int(input('Age: '))
if age >= 18:
print('test')
๐ก Nested Conditions
A condition inside another condition!
age = int(input('Age: '))
has_ticket = input('Do you have a ticket? (yes/no): ')
if age >= 12:
if has_ticket == 'yes':
print('โ
Come in!')
else:
print('โ You need a ticket')
else:
print('โ Must be at least 12')
๐ With Logical Operators
age = 16
has_permission = True
# and โ both conditions must be True
if age < 18 and has_permission:
print('You can enter with permission')
# or โ at least one must be True
if age >= 18 or has_permission:
print('You can enter')
# not โ inverts the condition
if not has_permission:
print('Permission required')
๐ Cheat Sheet
# Simple if
if condition:
# code if True
pass
# if-else
if condition:
# code if True
pass
else:
# code if False
pass
# Comparison operators
# == != > < >= <=
# Logical operators
# and or not
# Important:
# - Colon after the condition!
# - Indentation (4 spaces)
# - == for comparison, = for assignment
๐ Summary
- if โ runs code only when True
- else โ the alternative for False
- == โ comparison (don’t confuse with =)
- Indentation is mandatory!
- Use operators: >, <, >=, <=, !=
Conditionals are the core logic of any program! With them, your code becomes smarter. ๐ง
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!