๐Ÿ“ Python

if-else in Python: Making Your Program Smarter ๐Ÿค”

P
Author
PyLand Team
๐Ÿ“…
Published
30.03.2026
โฑ๏ธ
Reading time
3 min
๐Ÿ‘๏ธ
Views
391
๐ŸŒฑ
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
    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. ๐Ÿง 

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

๐Ÿ“

if __name__ == "__main__": the Python entry point

The if name == "main" guard defines a Python program's entry point: it distinguishes direct...

๐Ÿ“… 14.08.2026 ๐Ÿ‘๏ธ 44
๐Ÿ“

strip() and lower(): Preparing User Text ๐Ÿงน

A user may enter the correct word with extra spaces or different letter case. Python...

๐Ÿ“… 11.08.2026 ๐Ÿ‘๏ธ 50
๐Ÿ“

ord(), chr(), and Cyclic Letter Shifts in Python ๐Ÿ”

A string contains characters, but a computer stores every character as a numeric code. Python...

๐Ÿ“… 09.08.2026 ๐Ÿ‘๏ธ 89
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Python from Scratch: Build 7 Practical Projects Open course curriculum