If else in Python | Syntax, Example

If else in Python is a two-way conditional statement or double selection statement. It tests a conditional expression and executes one of two blocks of code, depending on the result.

In other words, an if else statement first evaluates the ‘if test condition/expression’.

If the test condition is true, Python executes the statements inside the block of the ‘if statement’. Otherwise, if the condition is false, Python executes the statements inside the else block.

Python if…else statement decides the execution path based on whether the condition is true or false.

When to use If-else Statement in Python?


A one-way if statement in Python executes a statement if and only if the specified test condition is true. If the condition is false, Python will do nothing.

But, assume we need to take an alternative action when the specified test condition is false. In this case, we will use a two-way selection if-else statement. Let’s understand it with the help of an example.

num = 11
if num % 2 == 0:
    print(num, "is divisible by 2.")
else:
    print(num, "is not divisible by 2.")
Output:
      10 is not divisible by 2.

In the above code, the specified test condition is false. Therefore, Python execute else block statement and print the message “10 is not divisible by 2”.

Thus, a two-way selection if else statement in Python routes the execution through two different paths based on the result of the condition.

Syntax of If else Statement in Python


The general syntax of using if else statement in Python is as follows:

if condition or expression:
    statement to be executed if the condition is true.
else:
    statement to be executed if the condition is false.

In the above syntax, an optional else statement can follow an if statement. The if condition is a boolean expression that may be any comparison or logical expression that returns the boolean value either true or false.

Each statement represents a single statement or a block of statements intended by two or four white spaces.

If the boolean expression in the if statement returns true, Python interpreter executes the statements inside the if block. If it returns false value, the interpreter executes statements inside else block.


The else block is an optional part. It means that else block part can be omitted if not required. You can follow this convention in ​all control statements in Python.

Flowchart Diagram of If else Statement


The flowchart for two-way if-else statement in Python is as shown below in the figure.

Flowchart diagram of Python if else statement

Let’s understand with the help of a flowchart diagram how two-way if-else statements work in the Python language.

If the test condition is true, then the if code will execute. If the condition is false, the else code will execute. In no case, both statements will execute at a time.

For example, consider the following below code.

passMarks = 40
if passMarks >= 40:
    print("Passed")
else:
    print("Failed")
Output:
      Passed

In the above code, if the boolean expression evaluates to true, i.e., if the passMarks is greater than or equal to 40, is true, and it will print a message “Passed” on the console. If it evaluates to false, the message “Failed” will print.

Substitution of If-else statement with Ternary operator


We can also replace if-else statement with ternary operator in Python. Consider the following statement.

print("Paased" if passMarks >= 40 else "Failed")

This statement contains a conditional expression that prints the string “Passed” if the condition evaluates to true. If the conditional expression evaluates to false, it prints the string “Failed”.


Thus, this statement with ternary operator performs the same function as the preceding if-else statement.

Some valid Example of If-else statements


Example 1:

x, y = 20, 40
if x > y:
    print(x, "is greater than ",y)
else:
    print(y, "is greater than ", x)
Output:
      40 is greater than  20

In the above code, Python evaluates the conditional expression x > y to false. Therefore, it executes else block statement and print the message “40 is greater than 20”.

Example 2:

gender = 'F'
if((gender == 'M') or (gender == 'm')):
    print("You are a male")
else:
    print("You are a female")
Output:
      You are a female

In the above code, Python evaluates the conditional expression to false. Therefore, it executes else block statement and print the message “You are a female”.

Python If else Example Program for Best Practice


Let’s practice some example programs based on if-else statement in Python.

Example 1:

Let’s write a program to check a number is even or odd using if…else statement.

# Taking an integer number from the user.
number = int(input('Enter a number: '))
if number % 2 == 0:
    print(number, "is an even number.")
else:
    print(number, "is an odd number.")
Output:
      Enter a number: 20
      20 is an even number.

In the above code, we have entered an integer number 20 as a user and store it into a variable number. Python evaluates the if conditional expression (number % 2 == 0) to true. Therefore, it executes if block statement and print the message “20 is an even number”.


Example 2:

Let’s write a Python program in which we will take marks of three subjects from the user and then calculate the total marks, percentage, and grade using if else statement.

chem = int(input('Enter your chemistry marks: '))
phy = int(input('Enter your physics marks: '))
maths = int(input('Enter your maths marks: '))

total = chem + phy + maths
per = total / 3
print("Total marks obtained: ", total)
print("Percentage: ", per)

if per >= 85:
    print("Grade A")
else:
    print("Grade B")
Output:
      Enter your chemistry marks: 67
      Enter your physics marks: 89
      Enter your maths marks: 80
      Total marks obtained:  236
      Percentage:  78.66666666666667
      Grade B

Example 3:

Let’s write the Python code to check whether a number is divisible with another number or not. Print appropriate message on the console.

num1, num2 = 20, 7
if num1 % num2 == 0:
    print(num1, "is divisible by", num2)
else:
    print(num1, "is not divisible by", num2)
Output:
      20 is not divisible by 7

Example 4:

Let’s write a Python program to take a number from the user and check whether it is a Buzz number. A number is a buzz number when it ends with 7 or is divisible by 7.

num = int(input('Enter an integer number to check buzz: '))
if (num % 10 == 0) or (num % 7 == 0):
    print(num, "is a Buzz number")
else:
    print(num, "is not a Buzz number")
Output:
      Enter an integer number to check buzz: 777
      777 is a Buzz number

Example 5:

Let’s write Python code in which we will increment the salary of employee 10% if the salary is greater than 8000. If the salary of employee is less than 8000, will increment 15%.

salary = 7500
if salary >= 8000:
    salary = salary + (salary * 0.1)
    print("Employee salary: ", salary)
else:
    salary = salary + (salary * 0.15)
    print("Employee salary: ", salary)
Output:
      Employee salary:  8625.0

In the above code, the variable salary has initialized with the value 7500. Then, the if else statement will check that the salary is greater than 8000 or not.

If the salary is greater than 8000, it will increment by 10% and display the increment salary on the console. However, if the condition does not satisfy, the else block will execute. In this case, salary will increment by 15% and display increment salary on the console.

Since the employee’s salary is 7500, which is less than 8000, Python executes else block statements and displays the increment salary.


In this tutorial, you have learned about a two-way conditional if else statement in Python with the help of various example programs. Hope that you will have understood the basic points of if-else statement.
Thanks for reading!!!
Next ⇒ If-elif-else statement in Python⇐ Prev Next ⇒

Please share your love