Python If Else Statement - Explained with Examples

Making decisions is important in our everyday lives. So also is it in the world of programming as decisions is necessary in order to run the next code. These decisions are know as conditions because they determine the execution flow of the program.

In Python, if…else statement are used to make decisions.

Here at LinuxAPT, we shall look into how to use the if … else statement in Python. 


What is if...else statement in Python ?

Decision making is required when we want to execute a code only if a certain condition is satisfied.

The if…elif…else statement is used in Python for decision making.

Python if Statement Syntax:

if test expression:
    statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the test expression is True.

If the test expression is False, the statement(s) is not executed.

In Python, the body of the if statement is indicated by the indentation. The body starts with an indentation and the first unindented line marks the end.

Python interprets non-zero values as True. None and 0 are interpreted as False.


1. Python Conditions

  • i. equals: a == b
  • ii. not equals: a != b
  • iii. less than: a < b
  • iv. less than or equal to: a <= b
  • v. greater than: a > b
  • vi. greater than or equal to: a >= b

In if and loops, these conditions are commonly used.

For Example:

a = 50
b = 100
if a < b:
print("a is less than b")

Output will give:

a is less than b


2. Indentation

Python defines the scope in code based on indent.

Example of a code snippet without indents:

a = 50
b = 100
if a < b:
print("a is less than b") # This will cause an error.


3. Elif

The elif keyword indicates that if the above condition is not true, then consider this condition.

Example:

a = 50
b = 50
if a < b:
print("a is less than b")
elif a == b:
print("a = b")

Output will give:

a = b


4. Else

The else keyword considers a completely different condition from the previous conditions.

Example:

a = 50
b = 70
if a > b:
print("a is greater than b")
elif a == b:
print("a = b")
else:
print("a < b")

Output will give:

a < b

Here, the if and elif conditions are not true, so it considers the else condition.


5. Short hand If

You can let the statement execute on the same line as if it’s only 1 line.

Example:

a = 50
b = 70
if a < b: print("a is less than b")

Output will give:

a is less than b


6. Short hand If … Else

Example:

a = 70
b = 50
print("a < b") if a < b else print("a > b")

Output will give:

a > b


7. Nested If

Example:

x = 20
if x > 5:
print("Above 5")
if x > 10:
print("And above 10")
else:
print("But not above 10")

Output will give:

Above 5
And above 10


[Need fix to Python issues ? We can help you. ]

This article covers how to use the if else statement in Python. In fact, An if else Python statement evaluates whether an expression is true or false. If a condition is true, the "if" statement executes. Otherwise, the "else" statement executes. Python if else statements help coders control the flow of their programs.


Python Conditions and If statements

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b


Python Nested if statements

We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.


Python Nested if Example

'''In this program, we input a number

check if the number is positive or

negative or zero and display

an appropriate message

This time we use nested if statement'''


num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Output 1 will give:

Enter a number: 5
Positive number


Output 2 will give:

Enter a number: -1
Negative number


Output 3 will give:

Enter a number: 0
Zero

Related Posts