📝 Python

if-else in Python: Making Your Program Smarter 🤔

0
Author
04e5cc8b-58ac-4bdc-bdee-661bbb
📅
Published
30.03.2026
⏱️
Reading time
3 min
👁️
Views
103
🌱
Level
Beginner

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

# if-else
if condition:
    # code if True
else:
    # code if False

# 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. 🧠

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

Setting Up Your Environment: Python, pip, and VS …

Before writing code locally, you need to set up three tools: Python, pip, and VS...

📅 04.06.2026 👁️ 17
📝

The datetime Module: Working with Dates and Times

datetime is Python's standard module for working with dates and times. It's part of the...

📅 08.05.2026 👁️ 67
📝

.env Files and Environment Variables: Keeping Sec…

Imagine you wrote a program with an API key hardcoded in the source and pushed...

📅 08.05.2026 👁️ 76

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!