Lesson 18 of 70 – Python If Else
26%

Python If Else

In Python, if else statements are used to make decisions in a program. They allow the program to execute different blocks of code depending on whether a condition is True or False.

Note: Python uses indentation to define the block of code that belongs to an if, elif, or else statement.
What is an if Statement?

The if statement executes a block of code only when its condition is True.

age = 20

if age >= 18:
    print("You are an adult.")
Output:
You are an adult.
Syntax of if Statement
if condition:
    statement

The important parts are:

  • if is the keyword.
  • The condition is written after if.
  • A colon : is placed after the condition.
  • The code inside the block must be indented.
Simple if Example
number = 10

if number > 5:
    print("Number is greater than 5")
Output:
Number is greater than 5
Condition is False

If the condition is False, the code inside the if block is not executed.

number = 3

if number > 5:
    print("Number is greater than 5")

There is no output because the condition is False.

if Else Statement

The else statement is used when you want to execute another block of code if the if condition is False.

age = 16

if age >= 18:
    print("You can vote.")
else:
    print("You cannot vote.")
Output:
You cannot vote.
Syntax of if Else
if condition:
    statement
else:
    statement

If the condition is True, the if block executes. Otherwise, the else block executes.

Checking Even and Odd Numbers

The modulus operator % can be combined with if else to check whether a number is even or odd.

number = 10

if number % 2 == 0:
    print("Even number")
else:
    print("Odd number")
Output:
Even number
Positive and Negative Number
number = -5

if number >= 0:
    print("Positive number")
else:
    print("Negative number")
Output:
Negative number
Comparing Two Numbers
a = 20
b = 10

if a > b:
    print("A is greater")
else:
    print("B is greater or equal")
Output:
A is greater
Using if Else with User Input
age = int(input("Enter your age: "))

if age >= 18:
    print("You are eligible.")
else:
    print("You are not eligible.")

If the user enters 20:

Output:
You are eligible.
Checking Password
password = input("Enter password: ")

if password == "12345":
    print("Login successful")
else:
    print("Invalid password")

If the user enters 12345:

Output:
Login successful
Comparing Strings

Strings can also be compared using conditions.

name = input("Enter your name: ")

if name == "Rahul":
    print("Welcome Rahul")
else:
    print("Welcome")
Using Multiple Conditions with and

The and operator can be used when both conditions must be True.

age = 25

if age >= 18 and age <= 60:
    print("Working age")
else:
    print("Outside working age")
Output:
Working age
Using Multiple Conditions with or

The or operator is used when at least one condition should be True.

day = "Sunday"

if day == "Saturday" or day == "Sunday":
    print("Weekend")
else:
    print("Weekday")
Output:
Weekend
Using not with if

The not operator reverses a Boolean condition.

logged_in = False

if not logged_in:
    print("Please login first")
Output:
Please login first
Nested if

An if statement can be placed inside another if statement. This is called a nested if.

age = 20
has_id = True

if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("ID required")
else:
    print("Entry not allowed")
Output:
Entry allowed
Indentation in if Else

Indentation is very important in Python. The statements inside an if or else block must be indented.

age = 20

if age >= 18:
    print("Adult")
else:
    print("Minor")
Important: Incorrect indentation can cause an IndentationError or change the meaning of your program.
if Else with Marks
marks = 65

if marks >= 40:
    print("Pass")
else:
    print("Fail")
Output:
Pass

This is a simple example of decision-making using marks.

if Else with Temperature
temperature = 35

if temperature > 30:
    print("It is hot")
else:
    print("It is cool")
Output:
It is hot
Practical Example
marks = int(input("Enter your marks: "))

if marks >= 40:
    print("Congratulations!")
    print("You passed the exam.")
else:
    print("You failed the exam.")
    print("Please try again.")

If the user enters 75:

Output:
Congratulations!
You passed the exam.
Key Points
  • The if statement is used to make decisions.
  • The condition after if must produce a Boolean result.
  • The else block executes when the if condition is False.
  • A colon : is required after the condition.
  • Python uses indentation to define code blocks.
  • Comparison operators are commonly used with if statements.
  • and, or, and not can combine conditions.
  • An if statement can be nested inside another if statement.
  • if else statements are commonly used for decision-making.

🧠 Quick Quiz

Question: Which keyword is used when the if condition is False?