Dremendo Tag Line

if Statement in Python Programming

Decision Making in Python

In this lesson, we will understand what If statement is and the rules of indentation in Python programming along with some example.

What is if Statement

The if statement is the most simple decision making statement in Python programming. Using if statement we test some condition, if the condition is true then a block of statements is executed otherwise not.

video-poster

if Statement Syntax

if condition:
    # Statements to execute if
    # condition is true. You can use tab as indentation in python

In the above syntax after if statement we will write our condition with a colon : at the end. If the condition is true then the statements written below the if statement (using tab indentation) will execute otherwise not. We can write our condition using Arithmetic, Relational, Logical, Identity and Membership operators.

Rules of Indentation in Python

Programming languages like C, C++ and Java use curly braces { } to define a block of code, but Python uses indentation to define a block of code.

An indentation refers to the use of one or more spaces at the beginning of a line to define a block of code in Python. Let's see the rules of proper indentation in Python.

  • Use only fixed (consistent) number of spaces or tab to indent the block of codes.
  • Throughout your entire Python code, use either space or tab but not a mixture of both to indent the line of codes. If you use both space and tab then it will create IndentationError in your code.

Now let's see some examples of if statement for more understanding.

Example 1 (Condition using Relational operator)

Python program to check if an integer variable's value is greater than 10.

a=25
if a>10:
    print('Yes',a,'is greater than 10')

Output

Yes 25 is greater than 10

Here you can see that the condition a>10 is true because the value of a is greater than 10. So the block of code written using tab indentation just below if statement has executed and the output is printed on the screen.

Example 2 (Condition using Arithmetic and Relational operator)

Python program to check if the sum of 2 integer variable's value is greater than 10.

a=10; b=5
if a+b>10:
    print('Yes',a+b,'is greater than 10')

Output

Yes 15 is greater than 10

Here you can see that we have used Arithmetic and Relational operator in the condition a+b>10 and the condition is also true because the value of a+b is greater than 10. So the block of code written using tab indentation just below if statement has executed and the output is printed on the screen.

Example 3 (Condition using Arithmetic, Relational and Logical operator)

Python program to check if an integer variable's value is an even number and is also greater than 10.

a=18
if a%2==0 and a>10:
    print(a,'is an even number and is also greater than 10')

Output

18 is an even number and is also greater than 10

Here you can see that we have used Arithmetic, Relational and Logical operator in the condition a%2==0 && a>10 and both the conditions a%2==0 and a>0 are also true because the remainder of the modulus division a%2 is equal to 0 and also value of a is greater than 10. So the block of code written using tab indentation just below if statement has executed and the output is printed on the screen.