Conditional Logic in Python: If, elif, & else Statements

Conditional statements, or decision-making statements, are fundamental building blocks in programming. They allow you to control the flow of your program’s execution based on certain conditions.

Think of them like if-else statements in everyday life. For example:

  • If it’s raining, I’ll bring an umbrella.
  • If I’m over 18, I can vote.

In Python, the most commonly used conditional statements are if, elif, and else statements. In this article, you will learn the syntax and usage of if, elif, and else statements. We will practice some examples to help you understand how to use these statements in your programs.

The If Statement

The if statement is considered the most fundamental control flow statement. It allows you to execute a set of statements if a condition is true. Here’s the syntax for an if statement in Python:

if condition:
    # do something

In the above code, ‘condition‘ is a Boolean expression that evaluates to either True or False. If it is True, the statements within the if block are executed. Otherwise, they are skipped.

For example, let’s say you want to write a program that prints “Hello, World!” if the value of a variable ‘x‘ is greater than 10. Here’s how you can use an if statement to do that:

x = 15

if x > 10:
    print("Hello, World!")

In the above code, x has a value of 15, which is greater than 10. Therefore, the if condition is true, and the print statement is executed. The output is: “Hello, World!”.

The Elif Statement

The elif statement is short for “else if“. It allows you to test multiple conditions and execute a different set of statements for each condition. Here’s the syntax for an elif statement in Python:

if condition1:
    # do something
elif condition2:
    # do something else

In the above code, ‘condition1‘ is the first tested condition. If it is True, then the statements within the first if block are executed, and the elif block is skipped. If it is False, the second condition ‘condition2‘ is tested. If it is True, the statements within the elif block are executed. Otherwise, both the if and elif blocks are skipped.

For example, let’s say you want to write a program that prints “Positive” if a variable ‘x‘ is positive, “Negative” if it is negative, and “Zero” if it is zero. Here’s how you can use an if-elif statement to do that:

x = 5

if x > 0:
    print("Positive")
elif x < 0:
    print("Negative")
else:
    print("Zero")

In the above code, the value of x is 5, which is greater than 0. Therefore, the first if the condition is true, and the print statement “Positive” is executed. If x had a value of -5, then the second condition x < 0 would have been true, and the print statement “Negative” would have been executed instead.

The Else Statement

The else statement allows you to execute a set of statements if none of the previous conditions were true. Here’s the syntax for an else statement in Python:

if condition1:
    # do something
elif condition2:
    # do something else
else:
    # do something different

In the above code, if neither condition1 nor condition2 is True, then the statements within the else block are executed.

Here’s an example of an if-elif-else statement:

x = -10
if x > 0:
    print("x is positive")
elif x == 0:
    print("x is zero")
else:
    print("x is negative")

In the above example, the if statement checks if x is positive, the elif statement checks if x is zero, and the else statement is executed if neither condition is True (i.e. if x is negative). Since x is negative, the code inside the else statement is executed, and the output is “x is negative”.

Exercises

Try some exercises for practicing Conditional Logic with If, elif, and else statements:

  1. Write a program that takes an integer input from the user and checks if it’s even or odd using if-else statements. Print “Even” if the input is even and “Odd” if it’s odd.
  2. Write a program that takes two integer inputs from the user and finds the maximum value using if-else statements. Print the maximum value.
  3. Write a program that takes three integer inputs from the user and finds the minimum value using if-elif-else statements. Print the minimum value.
  4. Write a program that takes an integer input from the user and checks if it’s positive, negative, or zero using if-elif-else statements. Print “Positive” if the input is greater than zero, “Negative” if it’s less than zero, and “Zero” if it’s equal to zero.
  5. Write a program that takes a string input from the user and checks if it’s a palindrome using if-else statements. A palindrome is a word or phrase that reads the same backward as forward. Print “Palindrome” if the input is a palindrome and “Not Palindrome” if it’s not.
  6. Write a program that takes an integer input from the user and checks if it’s a prime number using if-else statements. Print “Prime” if the input is a prime number and “Not Prime” if it’s not.
  7. Write a program that takes two string inputs from the user and checks if they are anagrams using if-else statements. Anagrams are two words or phrases that contain the same letters but are in a different order. Print “Anagram” if the inputs are anagrams and “Not Anagram” if they are not.
Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 194