Pythondex
Learn Python

Conditional Statements In Python

Conditional statements in Python are fundamental for controlling the flow of your program based on certain conditions. In this tutorial, we’ll cover the basics of conditional statements, including the if, elif (else if), and else statements.

Indentation


if condition:
    # This code is indented and part of if statement
    statement1
    statement2

# This code is not indented and is outside the if statement

In Python, think of indentation as a way to group related code together. Indentation helps Python understand which code is part of an if statement or other structures.

1. If Statement


if condition:
  # code to execute if condition is true

The if statement allows you to execute a block of code only if a specified condition is true. If the condtion inside if statement is false then it will not execute the code under it.

Example


num = 21

if num > 20:
    print("Number is greater then 20.")

In this example, the code inside the if block will only run if the num is greater than 20. If you change the num value less then 20 then it will print nothing.

Shorthand


x = 4
y = 2

if x > y: print("x is greater than y")

If you have only one statement to execute in if statement then you can put it in the same line like shown in above example.

2. If-Else Statement


if condition:
  # code to execute if condition is true
else:
  # code to execute if condtion is false

The if-else statement provides an alternative block of code to execute when the condition in the if statement is false so if the condition in the if statement is false then it will execute the code inside else block.

Example


num = 21

if num > 20:
    print("Number is greater then 20.")
else:
    print("Number is smaller then 20")

Here in the above example, if the num is greater than 20 then the code inside the if block will be executed and if the num is less than 20 then the code inside else block will get executed.

Shorthand


x = 12
y = 10

print("X") if x > y else print("Y")

You can also use the if else shorthand if you have just one statement to execute in if-else, In the above example it will print X if x > y and it will print Y if the condition is false.

3. Elif Statement


if condition:
  # code to execute if condition is true
elif condtion:
  # code to execute if first if condition is false
else:
  # code to execute if both if and elif condtion are false

The elif statement allows you to check multiple conditions in sequence. If the condition in the if statement is false, then it checks the conditions in the elif statements one by one until a true condition is found, and if all conditions are false then the else block will get executed.

Example


num = 21

if num > 20:
    print("Number is greater then 20.")
elif num == 20:
    print("Number is equal")
else:
    print("Number is smaller then 20")

In the above example, we added a elif block with a condition if num equals to 20 so if this condition is met then the code inside elif will get executed.

Nested If-Else


# Nested if statements for checking a person's age
age = 15

if age >= 13:
    print("You are a teenager.")

    if age <= 19:
        print("You are a teenager in the range 13-19.")
    else:
        print("You are older than 19.")
else:
    print("You are not a teenager.")

Nested if statements allow you to handle more intricate conditions by adding one decision within another. The above example uses nested if statements to determine whether a person is a teenager and falls within a specific age range.

So this was everything you needed to know about conditional statements in python, they are very important so make sure you understand them properly.