Python – if else statement

Introduction

Do you need to execute a code only under specific conditions?
Python if else statements are what you are looking for!

This post is about simple if, if else, and if elif else.
These are fundamental constructs in Python and in all other programming languages so it is important to understand them well.

Logical conditions

Before learning how if-else works in python it is good to know which logical conditions are supported.

Logical conditionPython expression
Equalsx == y
Not equalsx != y
Greater thanx > y
Greater than or equal tox >= y
Less thanx < y
Less than or equal tox <= y
Logical conditions supported by Python

Main operators

Before proceeding with the explanation of if else in Python, it might be useful to review the main operators that can be used.
The next sections show how the and, or and in operators work.

and

It is possible to combine different statements using the and keyword.

x = 10
y = 20
z = 5
print(y > x and y > z)

This example will print True only if both conditions are true.

or

It is possible to combine different statements using the or keyword.

x = 10
y = 20
z = 5
print(x > y or y > z)

This example will print True if either condition is true.

in

The in operator is mainly used with tuples and lists to check if an element is present or not.

a = [1, 2, 3, 4, 5]
print(6 in a)

This example will print True only if 6 is present in the list a.

Python if statement

Python like other programming languages, provides the if statement with which it is possible to execute code only if a certain condition is met.

n = int(input("Insert a number: "))
if n > 10:
    print("Number greater than 10")
print("Goodbye")

If you try to run this simple program you will see that it will ask you for a number and if it is greater than 10 it will print a message on the screen otherwise it will print just “Goodbye”.

Output

# example with 3
Insert a number: 3
Goodbye
# example with 44
Insert a number: 44
Number greater than 10
Goodbye

This is possible thanks to the if statement on line 3, but let’s see the general syntax:

if <logical_condition>:
      lines of code executed if the condition is verified
      ...

Of course you can execute a set of instructions if the condition match but beware of indentation! Everything must be under the if statment block.

n = int(input("Insert a number: "))
if n > 10:
    print("Number greater than 10")
    print("This is another message for the user")
    print("This is the last message for the user.... maybe")
print("Goodbye")

The above code shows you that if you want to do three different prints in case the number is greater than 10 you have to indent them correctly.

Python if else statement

In the previous section you learned how to execute code only if a codition is verified using the if statement.
But what if you want to execute specific code even if the condition isn’t true?
In these cases you have to use if else statement, let’s see how:

n = int(input("Insert a number: "))
if n > 10:
    print("Number greater than 10")
else:
    print("Number less than 10")
print("Goodbye")

Regardless of the number the user chooses, the program will print something.

Output

# example with 3
Insert a number: 3
Number less than 10
Goodbye
# example with 44
Insert a number: 44
Number greater than 10
Goodbye

As you can see, the else branch is executed only if condition is not verified.
Also in this case in the else statement it is possible to add as many lines of code as we want as long as the indentation is respected.

Since it is possible to add also the else statement, how can we rewrite the general syntax?

if <logical_condition>:
      lines of code executed if the condition is verified
      ...
else:
      lines of code executed if the condition is NOT verified
      ...

Python if elif else statement

The last thing you must never forget is that Python, like other programming languages, offers the possibility to create multiple branches with different conditions. This is made possible thanks to the elif instruction:

n = int(input("Insert a number: "))
if n >= 0 and n <= 10:
    print("Number between 0 and 10")
elif n > 10 and n <= 20:
    print("Number between 10 and 20")
elif n > 20 and n <= 30:
    print("Number between 20 and 30")
elif n > 30:
    print("Number greater than 30")
else:
    print("Negative number")
print("Goodbye")

It is possible to add as many elif as you like, basically one for each needed condition.

Output

# example with 32
Insert a number: 32
Number greater than 30
Goodbye
# example with 1
Insert a number: 1
Number between 0 and 10
Goodbye
# example with 2
Insert a number: 2
Number between 0 and 10
Goodbye
# example with 20
Insert a number: 20
Number between 10 and 20
Goodbye

The general syntax in this case is the following:

if <logical_condition>:
      lines of code executed if the condition is verified
      ...
elif <logical_condition>:
      lines of code executed if the condition is verified
      ...
else:
      lines of code executed if the condition is NOT verified
      ...

Nested if statements

At this point you may be wondering if it is possible to use nested if statements.
Of course it is possible, let’s see an example:

x = 4
my_list = [1, 2, 3, 4, 5, 6]
if x >= 2:
  print("Number greater than or equal to 2")
  if x in my_list:
    print("Number is present in the list")
  else:
    print("Number is NOT present in the list")
else:
    print("Number less than 2")

Output

Output for python nested if statements
Output for nested if statements

The pass statement

Since the if (but also elif and else) statement cannot be empty, you can use the pass statement to leave the if without code and avoid errors.

x = 10
y = 20
if x < y:
    pass

Conclusion

By reading this post you learned how to use if, if else, and if elif else statements with logical operators.
Let me know in the comments if the post was useful to you!

2 thoughts on “Python – if else statement”

  1. דירות דיסקרטיות ראשון לציון

    Itís nearly impossible to find educated people about this subject, however, you seem like you know what youíre talking about! Thanks

Leave a Comment

Your email address will not be published. Required fields are marked *