codingstreets
Search
Close this search box.

Introduction to Python If Else Statement with Practical Examples

In this article you will learn about Python If else statement.

Python If Else Statement – Before moving ahead, let’s know a little bit about Python Indentation

If statement – We use ‘if’ statement when it is necessary to give conditions to code. For example – It installs application, if it gets internet access. So here is a condition which is depend on ‘if’ statement.

In other words, a work or task that is depend on a condition to be done. If ‘this happens’ then ‘that happens’.  

In Python there are some mathematical operators which will be necessary to use with if statement.

1. Equal – x == y

2. Not equal – x != y

3. Greater than – x > y

4. Less than – x < y

5. Greater than or equal to – x >= y

6. Less than or equal to – x <= y

Note:  An if statement is written with ‘if’ keyword.

Note:  These operators can also be used with loop.

If keyword – It is used to give condition to code.

Example 1- Use of ‘if’ keyword to give a condition.

x = 78
y = 6

if x != y:
    print('It is unequal')              
Python if else statement

Explanation – In above example, x and y is two variables. Which is going to be used as if… Statement to know that whether x is equal to y or not. As x and y holds 78 and 6 respectively. It is clear that x is not equally to y therefore, if statement execute.

Indentation – It is the whitespace in the beginning of the code or statement. In Python, there is no declaration of indentation. By default it set to 4 spaces as one indentation. We can give indent to code by pressing ‘tab’ keyword or ‘space’ keyword for 4 times.

Example 2- Use of statement without indentation.

x = 78
y = 6

if x != y:
print('It is unequal') 
Python if else statement

Explanation – In above example, ‘print statement’ should be written after one indentation or 4 spaces in the beginning of the line but line not followed by indentation, therefore Python will give us an indentation error.

elif – It is used to give one more condition inside ‘if statement’. Python reads elif statement when previous all condition is false.

Example 3- Use of elif keyword.

x = 78
y = 6

if x == y:
   print('It is equal')
elif x != y:
     print('It is unequal') 
Python if else statement

Explanation – In this example – As we can see first statement is false, therefore Python will skip it and checks elif or another condition. It is clear that elif condition is true, therefore Python will execute statement.

else – Elif statement is used when previous all conditions are false. In this case Python will execute ‘else statement’.

Example 4- Use of else keyword with elif.

x = 78
y = 6

if x < y:
     print('It is equal')
elif y > x:
     print('It is unequal')
else:
     print('None of statement is True') 
Python if else statement

Explanation – In this example, ‘if’ and ‘elif’ condition is false therefore Python will skip them and executes ‘else’ statement because Python has no more to check.

— else keyword can be used without elif keyword.

Example 5- Use of else keyword without elif.

x = 78
y = 6

if x == y:
    print('It is equal')
else:
    print('It is not equal') 
Python if else statement

Explanation – In above example, it is shown clearly that ‘if’ condition is not True therefore, it returned ‘else’ statement.           

Short Hand If – If there is only one statement to execute, then it can be put on the same line as if statement.

Example 6- One line if statement.

x = 78
y = 6

if x != y: print('It is not equal')
Python if else statement

Explanation – In above example, it is shown clearly that ‘if’ condition is True therefore, it returned ‘if’ statement.

Short Hand If… Else – If there is only one statement to execute for ‘if’ and ‘else’, then it can all be put on the same line as if statement.

Example 7- One line if statement with else statement.

x = 78
y = 6

print('x is less than y') if x < y else print('x is greater than y')
Python if else statement

Explanation – In above example, it is shown clearly that ‘if’ condition is false therefore, it returned ‘else’ statement.

— It is possible to write more than one ‘else’ statement in the ‘if’ statement line.

Example 8-  Use of multiple else statement on the if statement line.

x = 78
y = 6

print('x is less than y') if x < y else print('x is equal to y') if x == y else print('x is greater than y') 
Python if else statement

Explanation – First ‘if x < y’, condition is false therefore, Python ignored it, and it goes for its else condition where it got ‘if… else’ condition inside it, then it checks if condition ‘if x == y’ is True, but as it is also false therefore, Python ignored it too and finally checked the last condition i.e., else, and executed it as Python did not have any other condition left to check.

Nested if – ‘if’ statement inside ‘if’ statement is known as nested if.

Example 9- Use of nested if statement.

x = 78

if x > 40:
     if x < 100:
         print(' x is less than 100')
else:
     print('Assumptions were wrong')                                                            
 
Python if else statement

Explanation – First condition ‘if x > 40’ is false therefore, Python ignored it and checked next condition that is condition ‘else’ and as it was last condition left therefore, Python executed it.

The pass Statement – if statement will not work without any ‘print’ statement but in case we need to leave it empty then we use ‘pass’ keyword to get an error out.

Example 10- Use of ‘pass’ keyword.

x = 20

if x == 20:
    pass
python-if-else-statement

Explanation – As it is shown clearly that condition ‘if x == 20’ is True but Python did not execute anything because if ‘if condition’ is True, then it will pass out the statement.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Follow Us

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on