Introduction to Python statements- If | If – else| Nested if

If you are interested to learn about the python tuples

Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. An if else statement in programming is a conditional statement that runs a different set of statements depending on whether an expression is true or false. A typical if else statement would appear similar to the one below (this example is JavaScript, and would be very similar in other C-style languages). Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making. In python, decision making is performed by the following statements.

StatementDescription
If StatementThe if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed.
If – else StatementThe if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed.
Nested if StatementNested if statements enable us to use if ? else statement inside an outer if statement.

Indentation in Python

For the ease of programming and to achieve simplicity, python doesn’t allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block. Generally, four spaces are given to indent the statements which are a typical amount of indentation in python.

Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation. We will see how the actual indentation takes place in decision making and other stuff in python.

The if statement

The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if-block. The condition of if statement can be any valid logical expression which can be either evaluated to true or false.

Python If-else statements

The syntax of the if-statement is given below.

  1. if expression:  
  2.     statement  

Example 1

num = int(input("enter the number?")) 
if num%2 == 0:   
print("Number is even")  

Output:

enter the number?10
Number is even

Example 2 : Program to print the largest of the three numbers.

a = int(input("Enter a? "));
b = int(input("Enter b? "));
 c = int(input("Enter c? "));
 <strong>if</strong> a>b <strong>and</strong> a>c:    
  <strong>print</strong>("a is largest"); 
<strong>if</strong> b>a <strong>and</strong> b>c:
<strong>print</strong>("b is largest");  
i<strong>f</strong> c>a <strong>and</strong> c>b:   <strong>print</strong>("c is largest");  

Output:

Enter a? 100
Enter b? 120
Enter c? 130
c is largest

The if-else statement

The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.

Python If-else statements

The syntax of the if-else statement is given below.

  1. if condition:  
  2.     #block of statements   
  3. else:   
  4.     #another block of statements (else-block)   

Example 1 : Program to check whether a person is eligible to vote or not.

age = int (input("Enter your age? ")) <strong>if</strong> age>=18:   
   <strong>print</strong>("You are eligible to vote !!"); 
 <strong>else</strong>:      <strong>print</strong>("Sorry! you have to wait !!");  

Output:

Enter your age? 90
You are eligible to vote !!

Example 2: Program to check whether a number is even or not.

num = int(input("enter the number?"))   <strong>if</strong> num%2 == 0:     
<strong>print</strong>("Number is even...")   <strong>else</strong>:   
<strong>print</strong>("Number is odd...")  

Output:

enter the number?10
Number is even

The elif statement

The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. However, using elif is optional. The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.

The syntax of the elif statement is given below.

  1. if expression 1:   
  2.     # block of statements   
  3.   
  4. elif expression 2:   
  5.     # block of statements   
  6.   
  7. elif expression 3:   
  8.     # block of statements   
  9.   
  10. else:   
  11.     # block of statements  
Python If-else statements

Example 1

number = int(input("Enter the number?")) <strong>if</strong> number==10:
<strong>print</strong>("number is equals to 10")  <strong>elif</strong> number==50: 
  p<strong>rint</strong>("number is equal to 50");
  <strong>elif</strong> number==100:   
  <strong>print</strong>("number is equal to 100");
   <strong>else</strong>:  
    <strong>print</strong>("number is not equal to 10, 50 or 100");  

Output:

Enter the number?15
number is not equal to 10, 50 or 100

Example 2

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")

Output

Positive or Zero

In the above example, when num is equal to 3, the test expression is true and the body of if is executed and the body of else is skipped.

If num is equal to -5, the test expression is false and the body of else is executed and the body of if is skipped.

If num is equal to 0, the test expression is true and body of if is executed and body of else is skipped.

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

Enter a number: 5
Positive number

Output 2

Enter a number: -1
Negative number

Output 3

Enter a number: 0
Zero

Python if…elif…else Statement

Syntax of if…elif…else

if test expression:
    Body of if
elif test expression:
    Body of elif
else: 
    Body of else

The elif is short for else if. It allows us to check for multiple expressions.

If the condition for if is False, it checks the condition of the next elif block and so on.

If all the conditions are False, the body of else is executed.

Only one block among the several if...elif...else blocks is executed according to the condition.

The if block can have only one else block. But it can have multiple elif blocks.

Flowchart of if…elif…else

Flowchart of if...elif....else in Python programming
Flowchart of if…elif….else statement in Python

Example of if…elif…else

'''In this program, 
we check if the number is positive or
negative or zero and 
display an appropriate message'''

num = 3.4

# Try these two variations as well:
# num = 0
# num = -4.5

if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

When variable num is positive, Positive number is printed. If num is equal to 0, Zero is printed. If num is negative, Negative number is printed.

Introduction to Python statements- If | If – else| Nested if
Show Buttons
Hide Buttons