Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python If...Else


Python Conditional Statements

A conditional statement allows to check several conditions and execute instruction based on whether it will be true or false.

The "if statement" is a conditional statement in Python that checks a condition.

  1. Simple if
  2. if-else
  3. Nested if
  4. if-elif-else

1. Simple if

The simple if statement block executes only when satisfied with a specific condition.

Created with Raphaël 2.3.0Start if conditionIf condition True / FalseIf condition blockEnd if conditionTrueFalse

Simple if condition flowchart

Example
age = 18
if age >= 18:
    print("The person is eligible to vote.")
The person is eligible to vote.

The above example, we declared a variable age and assigned 18 as a numeric value. Then after checking the condition with the "if keyword." If the age value is greater than or equal to eighteen(18) age >= 18, it prints the text "The person is eligible to vote."

Here age value is eighteen(18) because, for this reason, it prints "The person is eligible to vote."


2. If-else

The if-else checks the condition if it is true, then it executes the "if block" otherwise it executes the "else block."

Created with Raphaël 2.3.0Start if conditionIf condition True / FalseIf condition blockEnd if conditionElse condition blockTrueFalse

If-else condition flowchart

Example
age = 15
if age >= 18:
    print("The person is eligible to vote.")
else:
    print("The person is not eligible to vote.")
The person is not eligible to vote.

The above example, we declared a variable age and assigned 15 as a numeric value. Then after checking the condition with the "if keyword." If the age value is greater than or equal to eighteen(18) age >= 18, then print the text "The person is eligible to vote." otherwise, in the else block that print "The person is not eligible to vote."

Here age value is fifteen(15) because, for this reason, it prints "The person is not eligible to vote."


3. Nested if

An if statement inside another if statement.

Created with Raphaël 2.3.0Start if conditionIf condition True / FalseInner if condition True / FalseInner if condition blockEnd if conditionInner else condition blockElse condition blockTrueFalseTrueFalse

Nested if condition flowchart

Example
A = 20
B = 40
C = 60
if A > B:
    if A > C:
        print("A value is greater.")
    else:
        print("C value is greater.")
else:
    print("Either the B or C value is greater.")
Either the B or C value is greater.

In the above example, variables A, B, and C declared and assigned values of 20, 40, and 60. Now, check the condition whether A is greater than B (A > B). If the first condition is true, then check if A is greater than C (A > C). If the inner condition is true, then print "A value is greater." otherwise, print "C value is greater." or first condition is false (A > B), then print "Either the B or C value is greater."

This example prints the text "Either the B or C value is greater." because both (A > B) and (A > C) conditions are not satisfied. (A > C) is not checked because the first condition (A > B) is not true, so it goes directly to the else part and prints "Either the B or C value is greater."


4. If-elif-else

Using if-elif-else, you can check multiple conditions, and if none match, then the else statements will execute.

Created with Raphaël 2.3.0Start if conditionIf conditionTrue / FalseIf condition blockEnd if conditionElif conditionTrue / FalseElif condition blockElsecondition blockTrueFalseTrueFalse

If-elif-else condition flowchart

Example
number = 1
if number == 0:
    print("The number is zero.")
elif number == 1:
    print("The number is one.")
else:
    print("The number is neither zero nor one.")
The number is one.

In the above example, we declared a variable number and assigned the value 1. Check that the number is equal to 0 (number == 0), if that condition is true then print "The number is zero" or if that condition is false then move to the second condition, and check that the number is equal to 1 (number == 1), if that condition is true then print "The number is one" or if that condition is false then move to the else block and print "The number is neither zero nor one."

In the above example, the number value is 1 since it satisfies the number == 1 condition and prints the text "The number is one."


Here are a few important points about if statements.

    1. Else part is optional in the if statement.
    2. The keyword elif is short for else if.
    3. You can declare one or more elif parts with an if statement.
    4. If you compare the same value to several constants or check for specific types or attributes, you may also find the match statement is helpful in Python.
    5. Zero (0) treated as false, greater than zero or less than zero (-1 > 0 < 1) treated as true in python programming language.