Python if else statement

Table of Contents

Introduction

The if – else statement is a fundamental construct in Python and in all other programming languages.
Using them is it possible to execute a code only under specific conditions.

This post presents the simple if, but also the if else, and if elif else.
Last but not least we will see also the nested if statements and the pass keyword.

Before starting, however, it is good to remember what the logical conditions are and what the related operators are.

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

if-else statements are based on logical conditions.
In fact, a specific piece of code will be executed only if the logical condition is verified.

As you can see in the table above, it is possible to represent each type of condition with symbols.

In the next chapter we will see how it is possible to join several logical conditions together.

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.
Using these operators it is possible to join more logical conditions following some logics.

The operators are:

  • and
  • or
  • in

The next sections show how the and, or and in operators work.

and

It is possible to combine different logical conditions 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 logical conditions 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.

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

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

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 keyword

Python provides a keyword which is used to avoid any action. The keyword name is pass.

Basically, 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

Here we are at the end of this post! We’ve seen how to use if, if-else, and nested ifs in Python. We also did a little in-depth analysis on the logical conditions and the operators that can be used.
I hope this is all clear to you, but if not, feel free to contact me or write a comment below.

If everything is clear, move on to the next article to continue learning!

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 *