Home » Python

Conditional statements (if, if...else, if...elif...else, and nested if) in Python

Python conditional statements: In this tutorial, we are going to learn about the conditional statements (if, if...else, if...elif...else, and nested if) with examples.
Submitted by Bipin Kumar, on November 01, 2019

Conditional statements decide the flow of program execution. In programming whenever we need to make execute any special blocks based on the decision then we use the conditional statements.

No need to be confused about it because we are using it in our daily life when we need to make some decisions and based on these decisions, we decide what should we have to do next. Similar situations can occur while programming also where we need to make some decisions based on conditions given and based on these decisions we will execute the block of code.

Conditional statements available in the Python are,

  1. if statements
  2. if...else statements
  3. if...elif...else statements
  4. Nested if statements

1) Python if statement

It is one of the most common conditional statements in which some conditions are provided and if the condition is true then block under the if the condition will be executed.

Syntax:

    if condition:
        # what we want to execute here.

Example:

# input age
age=int(input('what is your age: '))

# checking the condition
if age>=18:
    print('You are Grown-up now !')

Output

RUN 1:
what is your age: 21
You are Grown-up now !

RUN 2:
what is your age: 15

2) Python if...else statement

In the above, we have seen that if the condition is true then block under if will execute then one thing is, comes to our mind that what happens when the condition will be false. So, to overcome this problem we are using if...else statements.

Syntax:

    if condition:
        # what we want to execute here.
    else:
        # what we want to execute here.

If the condition is true, then it will execute the block of if statements otherwise else statement.

Example:

# input age
age=int(input('what is your age: '))

# checking the condition
if age>=18:
    print('You are Grown-up now !')
else:
    print('You are Young!')

Output

RUN 1:
what is your age: 21
You are Grown-up now !

RUN 2:
what is your age: 15
You are Young!

3) Python if...elif...else statement

These conditional statements use where we have to check multiple conditions in the program. If these will not true that is false then the else blocks only execute.

Syntax:

    if condition:
        # what we want to execute here.
    elif conditions:
        # what we want to execute here.
    else:
        # what we want to execute here.

Example:

# input the age
n=int(input('Enter marks: '))

# checking the conditions
if n>=90:
    print('Excellent')
elif n<90 and n>=75:
    print('Passed')
else:
    print('Fail')

Output

RUN 1:
Enter marks: 95
Excellent

RUN 2:
Enter marks: 80
Passed

RUN 3:
Enter marks: 63
Fail

4) Python Nested if statement

As we all have familiar with the word nested which means one statement inside another statement same in the programming nested if statements mean an if statement inside another if statement that is we can place one if statements inside another if statements.

Syntax:

    if condition:
        # what we want to execute here.
        if condition:
            # what we want to execute here.
        else:
            # what we want to execute here.

Note: In Python, true and false are written as True and False. Since Python follow the indentation rule so the statements must write under the indentations of if statements or other.

Now, we will see an example based on the above conditional statements which will show us the grade of students.

Example:

# input the age
n=int(input('Enter marks: '))

# checking the conditions
if  n>=75:
    if n >=95:
        print('Excellent')
    else:
        print('Pass')
else:
    print('Fail')

Output

RUN 1:
Enter marks: 96
Excellent

RUN 2:
Enter marks: 89
Pass

RUN 3:
Enter marks: 69
Fail





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.