Python Conditional Statements Examples

Explore how to use if, else, and elif statements in Python. These beginner-friendly examples show how to make decisions in your code using conditions, comparisons, and logical expressions.


1. Basic if statement
python
# Example 1: Simple if statement
x = 6
if x > 5:
    print("x is greater than 5")

2. if-else statement
python
# Example 2: if-else statement
x = 1
if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")

3. if-elif-else chain
python
# Example 3: if-elif-else
x = 6
if x < 5:
    print("x is less than 5")
elif x == 5:
    print("x is equal to 5")
else:
    print("x is greater than 5")

4. Nested if statements
python
# Example 4: Nested if statements
x = 10
if x > 5:
    print("x is greater than 5")
    if x > 8:
        print("x is also greater than 8")

5. Multiple conditions using and
python
# Example 5: Using 'and' in conditions
x = 7
if x > 5 and x < 10:
    print("x is between 5 and 10")

6. Multiple conditions using or
python
# Example 6: Using 'or' in conditions
x = 3
if x < 0 or x > 5:
    print("x is either less than 0 or greater than 5")
else:
    print("x is between 0 and 5")

7. Using not in condition
python
# Example 7: Using 'not' in condition
is_raining = False
if not is_raining:
    print("You can go outside")

8. Check if a number is even or odd
python
# Example 8: Check if number is even or odd
num = 6
if num % 2 == 0:
    print("Even number")
else:
    print("Odd number")

9. Ternary conditional operator
python
# Example 9: Ternary operator
age = 18
status = "Adult" if age >= 18 else "Minor"
print("Status:", status)

10. Use if to check membership
python
# Example 10: Check if element exists in list
colors = ["red", "blue", "green"]
if "blue" in colors:
    print("Blue is in the list")

11. Use if to check if a number is positive
python
# Example 11: Check if number is positive

number = 7
if number > 0:
    print("The number is positive")

12. Find the largest of three numbers
python
# Example 12: Largest of three numbers

a = 5
b = 8
c = 3
if a >= b and a >= c:
    print("a is the largest")
elif b >= a and b >= c:
    print("b is the largest")
else:
    print("c is the largest")

13. Check if a year is a leap year
python
# Example 13: Check leap year

year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print("Leap year")
else:
    print("Not a leap year")

14. Check if a character is a vowel
python
# Example 14: Vowel checker

char = 'e'
if char.lower() in 'aeiou':
    print("It's a vowel")
else:
    print("It's not a vowel")

15. Simulate a traffic light system
python
# Example 15: Traffic light simulation

light = "green"

if light == "green":
    print("Go")
elif light == "yellow":
    print("Slow down")
elif light == "red":
    print("Stop")
else:
    print("Invalid light color")

16. Determine eligibility to vote
python
# Example 16: Voting eligibility

age = 17
citizen = True

if age >= 18 and citizen:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

17. Check if a user has access to a restricted area
python
# Example 17: Access control

is_logged_in = True
has_access = False

if is_logged_in and has_access:
    print("Access granted.")
else:
    print("Access denied.")

18. Check shipping eligibility based on location
python
# Example 18: Shipping eligibility

country = "USA"

if country == "USA" or country == "Canada":
    print("Shipping available.")
else:
    print("Shipping not available in your region.")

19. Determine if a number is within a valid range
python
# Example 19: Range check

temperature = 72

if 60 <= temperature <= 80:
    print("Temperature is in the comfortable range.")
else:
    print("Temperature is outside the comfortable range.")

20. Validate input length (e.g., password)
python
# Example 20: Password length check

password = "abc123"

if len(password) >= 6:
    print("Password length is valid.")
else:
    print("Password too short. Must be at least 6 characters.")

21. Simulate a login system with username and password
python
# Example 21: Login system simulation

username = "admin"
password = "1234"

entered_username = "admin"
entered_password = "1234"

if entered_username == username:
    if entered_password == password:
        print("Login successful!")
    else:
        print("Invalid Password.")    

else:
    print("Invalid Username.")

22. Ask the user for a password and check if it's correct
python
# Example 22: Password check

correct_password = "secret123"
entered_password = input("Enter your password: ")

if entered_password == correct_password:
    print("Access granted.")
else:
    print("Access denied.")

23. Ask the user to input a number and check if it's even or odd
python
# Example 23: Even or odd with input

number = int(input("Enter a number: "))

if number % 2 == 0:
    print("It's an even number.")
else:
    print("It's an odd number.")

24. Ask the user for a temperature and give clothing advice
python
# Example 24: Clothing advice based on temperature

temp = float(input("Enter the temperature in °C: "))

if temp > 30:
    print("It's hot, wear light clothes.")
elif temp >= 15:
    print("Nice weather, wear something comfortable.")
else:
    print("It's cold, wear a jacket!")

25. Ask the user for a grade percentage and print the letter grade
python
# Example 25: Grade calculator

score = float(input("Enter your percentage score: "))

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

26. Ask the user to enter their age and check voting eligibility
python
# Example 26: Voting eligibility with user input

age = int(input("Enter your age: "))

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")


What's Next?

After learning how to make decisions in code using conditional statements, you're ready to learn how to repeat actions using loops such as for and while.