You are currently viewing Python if else Statement with Examples

Python if-else statement is similar to an if statement in any other language that is used for implementing conditional logic. Conditional statements are a fundamental component of programming, allowing you to make decisions based on specific conditions. With if-else statements, you can execute different blocks of code based on whether a condition is true or false.

Advertisements

1. Syntax of “if else” Statement

The syntax of if-else statements in Python is straightforward, making it easy to write conditional code. Below is the basic syntax of if-else statements in Python.


# Syntax of if else
if condition:
    statement(s)
else:
    statement(s)

The condition is the expression that is evaluated, and statement(s) are the block of code that is executed if the condition is true. If the condition is false, the block of code inside the else block is executed.

1.1 Operators used with if-else statements

Python supports several operators that can be used with if-else statements. The commonly used are:

Comparison operators

OperatorDescription
==Checks if two values or conditions are equal
!=Checks if two values or conditions are not equal
>Checks if the left operand is greater than the right operand
<Checks if the left operand is less than the right operand
>=Checks if the left operand is greater than or equal to the right operand
<=Checks if the left operand is less than or equal to

Logical Operators

OperatorDescription
AndReturns True if both conditions are True
OrReturns True if at least one condition is True
NotReverses the result of a condition

For more operators, refer to Python operators.

Below is a very basic Python example of the statement:


# Prompt the user to enter a number
num = int(input("Enter a number: "))

# Check if the number is greater than or equal to zero
if num >= 0:
    # If the number is greater than or equal to zero
    print("The number is positive")
else:
    # If the number is less than zero
    print("The number is negative")

2. Syntax of “elif” Statement

Python also provides the elif statement for implementing more complex conditional logic. The elif statement is short for “else if” and allows you to test multiple conditions in a single if-else block.

When the if condition is false, the elif condition is evaluated. If it is true, the corresponding block of code is executed, and if not, the next elif or else block is checked.

The syntax of an elif statement is similar to that of an if statement, but it is placed after the initial if statement and before the else statement.


# Syntax of if elif else
if condition1:
    statement(s)
elif condition2:
    statement(s)
elif condition3:
    statement(s)
else:
    statement(s)

We can have multiple elif blocks. Below is a basic example of elif statement in Python:


# Example of if elif
num = int(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num == 0:
    print("Zero")
else:
    print("Negative")

3. Nested if else statements

To have a nested if-else Python statement, you simply put an if-else statement inside another if-else statement. The syntax looks like this:


# Nested if else
if condition:
    # execute this block of code if condition is true
    
    if condition:
    	  # execute this block of code if condition is true
    
    else:
        # execute this block of code if condition is false
        
else:
    # execute this block of code if condition is false

Now let us look at the below code that demonstrates a nested if else statement:


age = 20

if age >= 18:
    if age >= 21:
        print("You are old enough to drink and vote.")
    else:
        print("You are old enough to vote but not old enough to drink.")
else:
    print("You are not old enough to vote or drink.")

The code snippet checks if the age is greater than or equal to 18, and if so, it checks if it’s also greater than or equal to 21. If it is, then it prints “You are old enough to drink and vote.” If not, it prints “You are old enough to vote but not old enough to drink.” If the age is less than 18, it simply prints “You are not old enough to vote or drink.”

4. Comparison Operators and “if else” Statements

You can use comparison operators to compare values and return a Boolean value (True or False) based on whether the comparison is true or false. In Python, comparison operators include <, >, <=, >=, ==, and !=. We can use these operators with if-else statements to test multiple conditions.

4.1 Example: Comparing Two Strings

In the below example, user enters two strings are user and compares it using the == operator. If the strings are equal, the message will be “The strings are equal”. Otherwise, the message will be “The strings are not equal”.


# Compare two string with if statement
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
if string1 == string2:
    print("The strings are equal")
else:
    print("The strings are not equal")

4.2 Example: Check if a Number is Even or Odd

This example also makes use of Python if-else Statement. We have used the modulo operator % to check if the number is even or odd. If the remainder of the number divided by 2 is 0, the number is even. Otherwise, it is odd.


# Use if with expression
num = int(input("Enter a number: "))
if num % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

5. Logical Operators and “if else” Statements

We can use the Logical operators to combine multiple conditions in a single if-else statement. The three logical operators in Python are and, or, and not.

  • and operator: It returns True if both the conditions are True.
  • or operator: It returns True if any one of the conditions is True.
  • not operator: It reverses the result of the condition.

5.1 Example: “and” Operator with “if else”

We are using and operator to check if a number is between two values:


# Using logical operator and
num = int(input("Enter a number: "))
if num > 0 and num < 10:
    print("Number is between 0 and 10")
else:
    print("Number is not between 0 and 10")

5.2 Example: “or” Operator with “if else”

In this example, we have used the or operator to check if a number is either positive or even.


# Using logical operator or
num = int(input("Enter a number: "))
if num > 0 or num % 2 == 0:
    print("The number is either positive or even")
else:
    print("The number is negative and odd")

6. “if else” Statements and List Comprehension

7. Summary and Conclusion

In this article, we have covered the basic syntax of if-else and elif statements in Python, and provided examples of how to use them with comparison and logical operators. By using if-else statements with comparison and logical operators, you can control the flow of your program and make it more dynamic. If you have any questions, please leave them in the comment section.

Happy coding!

Related Articles