Home » Python Basics » Python if else

Python if else

python if else

Python if else statement is a part of control structures (sometimes also known as Python flow control). Python if statement helps in making decisions in the program. This is probably the simplest yet most frequently used in Python for decision making. Python if else statement works by evaluating a condition and subsequently making the decision. If a condition is met then execute a certain portion of the code otherwise execute other portion (which is specified in else or elif sections). In this tutorial, you will get detailed knowledge on Python if-else statement along with the code snippet. After reading this tutorial, you can work proficiently and implement your logic in python using if-else control flow.

Table of Contents

  1. Why do we need if-else in Python program?
  2. Python simple if statement
  3. Need for else block in Python
  4. Need for elif in python
  5. Nested if statement in Python
  6. Switch case in Python using if-elif-else statement
  7. Implementing Python if-else in a single line (one-liner)
  8. Python if-else exercise
  9. Conclusion
  10. Quiz – Python if else

Need for Python if else

In every programming language, we need some structure (or provision) to make a decision based on a certain condition. Let say, You need to write a program that checks for user’s age and based on the age decide whether he/she is eligible for voting or not. In this scenario (and plenty others), you will need a helping block and Python if else will support you to deal with. 

In the subsequent section, you will learn about if-else syntax (way to declare) and program code.

Python’s simple if statement

The syntax for a simple if statement,

if (condition):
    code for execution      # when the condition's result is True

The above code will check the condition first, if the condition evaluates to True then the body part will be executed. The output of the condition section should always be either True or False. 

Control Flow Diagram “if” structure:

Python if statement

Important Tips

Always indent the line after the “if (condition):” statement. Indentation means, a four-space gap or one Tab space from the beginning of the block. Indentation is necessary otherwise python interpreter will throw an error. As you can see in the above syntax, code starts after 4 space. This ensures that the code is inside the if block and it will execute only when the condition is true. A colon (:) must be placed at the end of if (condition) when writing Python if else statement.

Summary of the Tips

  • Put a colon (:) at the end of if (condition) line
  • Start the next line (aka Indentation) with 4-space or 1-Tab

You can learn more about comparison operators and logical operators in Python operators section.

Question: Write a Python code which prints “Number is bigger than 5” if the provided number is greater than ( >) 5?

num1 = 8        # create a variable with name "num1" and with value of 8.
if (num1 > 5):  # Bracket is optional here
    print ("Number is bigger than 5")

Learn more about Python variables.

Bracket around the condition: If there is only one condition then bracket ( ) is optional but when there are more than one conditions, the bracket is required. For example, two conditions are validated with “logical and” operator

if (a != 0) and (a > 20):
    # code to execute

Need for “else” block in Python

In the above program, when you provide any number greater than (>) 5, you will get the result as “Number is bigger than 5”  but what will happen if “num1” is less than 5. Here comes the role of else block.

The syntax for an Python if else statement,

if (condition):
    code for execution   # when the condition's result is True
    code for execution  # when the condition's result is False

As you know from the previous section, if the condition evaluates to True then the first block will execute but if the condition is not True (i.e False) then the second block will execute. 

Control Flow Diagram “if-else” structure:

python if-else

Question: Write a Python code which prints “Number is bigger than 5” if the provided number is greater than ( >) 5 otherwise print “Number is smaller than 5”?

num1 = 3
if (num1 > 5):
    print ("Number is bigger than 5")
    print ("Number is smaller than 5")

Need for “elif” in python

Let’s modify the scenario a little bit. Now the program requirement will be, print “Number is bigger than 5” if provided number (in this case num1) is greater than 5, else “Number is smaller than 5” if the provided number is less than 5 and add another case to print “Number is equal to 5” if provided number is equal to 5. Here comes the role of “elif” block

To add more condition apart from if and else, you can use elif.

Syntax ->Python if-elif-else

if (condition):
    code to execute
elif (condition):
    code to execute
elif (condition):  # if 2 or more elif is required.
    code to execute
    code to execute

Control Flow Diagram “if-elif-else” structure:

python if-elif-else

Important Tips Code flow (if-elif-else)

When you are implementing code which contains “if, else and elif” together then always start with if block, followed by all elif block and finally else block. There could be one or more elif block possible but it should always have else at the end.

As you would have observed that “else” line does not need any condition because it will execute all other remaining conditions which were not executed by if and else block, as shown above.

Do not use “elseif” in-place of “elif” in Python. There is no keyword “elseif” and Python don’t know what it is. It has been observed that even the pro-programmers are making this mistake unknowingly.

pif (num1 > 5):
    print ("Number is bigger than 5")
elif (num1 < 5):
    print ("Number is smaller than 5")
    print ("Number is equal to 5")

if num1 = 7 -> Number is bigger than 5
if num1 = 4 -> Number is smaller than 5
if num1 = 5 -> Number is equal to 5

Python If-elif-else complete tutorial

Nested if statement in Python

Nested if is an if statement inside another if block. Python facilitates the use of these structure in the native code. A “nested if” statement is particularly helpful in implementing micro condition check (i.e sub condition of the main condition).

Question: Write a code which prints “Number is less than 5” if the provided number is smaller than 5. 
– check if the given “Number is even less than 2”
– Check if the given “Number is bigger than 5”
– Check if the given “Number is equal to 5”

num1 = 1
if (num1 < 5):
    print ("Number is less than 5")
    if (num1 < 2):
	print("Number is even less than 2")
elif (num1 > 5):
    print ("Number is bigger than 5")
    print ("Number is equal to 5")

Pro-Tips for Nested if in Python:

  • Be careful about the indentation. The first print statement is indented inside 1st if block.
  • The second print statement is indented inside the second if block.
  • Indentation decides the code belongingness to a particular section and the length is always 4-space (1-Tab) from the beginning of if block.

Switch case – Python for “if-elif-else” statement

You might have heard of the Switch case statement in other programming languages like C, C++ or Java. Switch case is primarily used to jump to a specific case and execute that block.

There is no concept of Switch case in Python. However, by using if-elif-else statement one can implement the same.

Let’s understand it with an example.
Question: Create a switch case in Python to output the Month name if the user enters a month number. such as 1 -> January, 2 -> February, 3 -> March etc…

# Python code for switch case using if-elif-else

mon_num = 3
if (mon_num) == 1:
elif (mon_num) == 3:
elif (mon_num) == 4:
elif (mon_num) == 5:
elif (mon_num) == 6:
elif (mon_num) == 7:
elif (mon_num) == 8:
elif (mon_num) == 9:
elif (mon_num) == 10:
elif (mon_num) == 11:

Pro-Tip: This code can be improved further by implementing handlings such as handling invalid user inputs and more. This is not the scope of this tutorial and I don’t want to mix all the contents here.

Python if else one line

In general, the conventional method to use if-else or if-elif-else is discussed in the previous section of this tutorial. Python’s multiple “if-else” statements can be converted into one line using the Ternary operator in Python also known as python if ternary. Condense if/else into one line (one line if statement) looks like below,

print ("Bigger than 5") if num1 > 5 else print("Smaller than 5")

Normal version of above code is,

if num1>5:
    print ("Bigger than 5")
    print("Smaller than 5")

Learn in detail about Python Ternary operators.

Real-world example – Python if else

There are various solutions (programming codes) implemented using if-else statement. A collection of the code can be found at Python Basic Code page.


After reading this tutorial, you have learnt about simple if statement. You have also understood the need for “else” and “elif” in this control structure. Various syntax, examples and flow diagram were provided to make your understanding better. Additionally, you have also learned to implement a switch case using python’s if-elif-else block as well also learned to condense multiple lines of code into one line using ternary operators. There are many career opportunity available for fresher as well as an experienced candidate.

Keep Learning and Keep Growing !!

How did you like the content

Scroll to Top