Python if Statement
The if statement is used to execute a block of code only when a specified condition is True. It is one of the basic decision-making statements in Python.
The if statement evaluates a condition. If the condition
is true, the statements inside the if block are executed.
If the condition is false, the block is skipped.
Syntax of if Statement
if condition:
statement
The colon : after the condition indicates the beginning
of the if block. Python uses indentation to identify the
statements belonging to the block.
Simple if Statement
age = 20
if age >= 18:
print("You are eligible to vote.")
You are eligible to vote.
Since age >= 18 is True, the
print() statement is executed.
if Statement with a False Condition
age = 15
if age >= 18:
print("You are eligible to vote.")
print("Program completed.")
Program completed.
The condition is false, so the statement inside the if
block is not executed. The statement outside the block is executed.
if Statement with Comparison Operators
Comparison operators can be used to create conditions in an
if statement.
marks = 75
if marks >= 40:
print("Pass")
Pass
The condition marks >= 40 evaluates to
True, so the message is displayed.
if Statement with Equality Operator
number = 10
if number == 10:
print("Number is 10")
Number is 10
== to compare two values. The single
= operator is used for assignment.
if Statement with Logical Operators
Logical operators such as and, or and
not can be used to combine conditions.
Using and
age = 25
if age >= 18 and age <= 60:
print("Eligible")
Eligible
Using or
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Weekend")
Weekend
Using not
is_raining = False
if not is_raining:
print("You can go outside.")
You can go outside.
if Statement with User Input
The input() function can be used to accept a value
from the user and then apply a condition.
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
Enter your age: 21 You are an adult.
Checking Positive Number
number = int(input("Enter a number: "))
if number > 0:
print("Positive number")
Enter a number: 25 Positive number
Checking Even Number
The modulus operator % can be used to determine whether
a number is even.
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
Enter a number: 20 Even number
Checking Whether a String is Empty
An empty string evaluates to False in a Boolean context.
name = input("Enter your name: ")
if name:
print("Name entered:", name)
Enter your name: Rahul Name entered: Rahul
if Statement with Membership Operator
The in operator can be used to check whether an item
exists in a sequence.
fruits = ["apple", "banana", "mango"]
if "mango" in fruits:
print("Mango is available.")
Mango is available.
Nested if Statement
An if statement inside another if statement is called a
nested if statement.
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry allowed.")
Entry allowed.
The inner if statement is checked only when the outer
condition is true.
Multiple Statements inside if Block
More than one statement can be placed inside an if
block. All statements must have the same indentation.
marks = 85
if marks >= 40:
print("Result: Pass")
print("Congratulations!")
print("You have cleared the examination.")
Result: Pass Congratulations! You have cleared the examination.
Importance of Indentation
Python uses indentation to define a block of code. Statements
belonging to the if block must be indented.
age = 20
if age >= 18:
print("Adult")
print("Eligible")
In this example, both print() statements belong to the
if block because they have the same indentation.
IndentationError
or change the logical structure of the program.
if Statement with Multiple Conditions
marks = 85
if marks >= 40 and marks <= 100:
print("Valid passing marks")
Valid passing marks
Checking a Number in a Range
Python allows comparison expressions to be chained.
number = 50
if 1 <= number <= 100:
print("Number is between 1 and 100.")
Number is between 1 and 100.
Practical Example: Discount Eligibility
amount = float(input("Enter purchase amount: "))
if amount >= 5000:
print("You are eligible for a discount.")
Enter purchase amount: 6500 You are eligible for a discount.
Practical Example: Pass Check
marks = int(input("Enter marks: "))
if marks >= 40:
print("Pass")
Enter marks: 72 Pass
if Statement vs if-else Statement
| if Statement | if-else Statement |
|---|---|
| Executes a block only when the condition is true. | Provides blocks for both true and false conditions. |
| No alternative block is provided. | An else block provides an alternative. |
| Useful when an action is required only for a true condition. | Useful when one of two alternatives must be selected. |
The if-else statement is discussed in the next tutorial.
Important Points
- The
ifstatement is used for decision making. - The condition is evaluated as either
TrueorFalse. - The colon
:is required after the condition. - Statements inside the
ifblock must be indented. - Comparison operators can be used to create conditions.
- Logical operators can combine multiple conditions.
- An
ifstatement can be nested inside anotherifstatement. - The
ifstatement does not provide an alternative block when the condition is false.
if statement executes a block of code only
when its condition evaluates to True. Python uses
indentation to define the statements belonging to the if
block.