Python If…Else Statement

Welcome to The Coding College, your trusted resource for mastering programming concepts! In this article, we’ll dive into the Python If…Else statement, a fundamental concept for decision-making in Python programming.

What is the Python If…Else Statement?

The If…Else statement allows Python programs to make decisions based on conditions. It enables your code to take different actions depending on whether a given condition evaluates to True or False.

Why Learn If…Else?

Learning and mastering If…Else statements helps you:

  • Create dynamic programs that adapt to varying conditions.
  • Solve problems requiring conditional logic.
  • Understand the foundation of advanced decision-making constructs.

Syntax of Python If…Else

if condition:  
    # code to execute if condition is True  
else:  
    # code to execute if condition is False  
  • condition: An expression that evaluates to True or False.
  • The if block executes if the condition is True.
  • The else block executes if the condition is False.

Examples of Python If…Else

Example 1: Basic If…Else

age = 18  

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

Output:

You are eligible to vote.

Example 2: Checking Even or Odd Numbers

number = 7  

if number % 2 == 0:  
    print(f"{number} is even.")  
else:  
    print(f"{number} is odd.")  

Output:

7 is odd.

Example 3: Nested If…Else

You can nest If…Else statements for more complex decision-making.

score = 85  

if score >= 90:  
    print("Grade: A")  
else:  
    if score >= 75:  
        print("Grade: B")  
    else:  
        print("Grade: C")  

Output:

Grade: B

Python If…Else with Logical Operators

Logical operators like and, or, and not can combine multiple conditions.

Example: Eligibility Check

age = 20  
citizenship = "US"  

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

Output:

You are eligible to vote.

One-Line If…Else (Ternary Operator)

Python allows a concise syntax for simple If…Else statements.

age = 18  
message = "Eligible to vote." if age >= 18 else "Not eligible to vote."  
print(message)  

Output:

Eligible to vote.

Exercises to Practice Python If…Else

Exercise 1: Check Greater Number

Write a program that accepts two numbers and prints the greater one using If…Else.

Exercise 2: Leap Year Check

Write a program that checks if a year is a leap year.
Hint: A year is a leap year if it is divisible by 4, but not divisible by 100 unless it is also divisible by 400.

Exercise 3: Pass or Fail

Write a program that accepts a student’s score and prints “Pass” if the score is 50 or higher; otherwise, print “Fail”.

Exercise 4: Temperature Check

Write a program that accepts a temperature in Celsius:

  • Print "Cold" if the temperature is below 20.
  • Print "Warm" if the temperature is between 20 and 30.
  • Print "Hot" if the temperature is above 30.

Why Practice with The Coding College?

At The Coding College, we aim to simplify programming concepts with hands-on examples and exercises. Mastering If…Else statements is your first step toward building smart and dynamic Python programs.

Conclusion

The Python If…Else statement is a cornerstone of decision-making in programming. By understanding its syntax, exploring examples, and practicing exercises, you’ll gain confidence in writing conditional logic for your programs.

Leave a Comment