Python if-else Statement
The if-else statement in Python is used to make a decision based on a condition.
If the condition is True, the statements inside the if block are executed.
If the condition is False, the statements inside the else block are executed.
Syntax of if-else Statement
if condition:
statement(s)
else:
statement(s)
The if and else blocks are identified using
indentation. The colon : is required after the condition.
Simple if-else Example
age = 20
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Output:
Checking Even and Odd Numbers
The if-else statement can be used to determine whether a number
is even or odd.
num = 15
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Output:
Taking Input from the User
marks = int(input("Enter your marks: "))
if marks >= 40:
print("Pass")
else:
print("Fail")
Here, the condition marks >= 40 determines whether the student
has passed or failed.
if-else with Comparison Operators
Comparison operators such as ==, !=,
>, <, >= and
<= can be used in conditions.
a = 25
b = 30
if a > b:
print("a is greater")
else:
print("b is greater or equal")
if-else with Strings
password = input("Enter password: ")
if password == "python123":
print("Access granted")
else:
print("Access denied")
if-else with Logical Operators
Logical operators such as and, or and
not can be used to combine conditions.
age = 25
if age >= 18 and age <= 60:
print("Eligible")
else:
print("Not eligible")
if-else with Membership Operator
fruits = ["apple", "banana", "mango"]
if "mango" in fruits:
print("Mango is available")
else:
print("Mango is not available")
Finding the Greater of Two Numbers
a = 45
b = 30
if a > b:
print("Greater number:", a)
else:
print("Greater number:", b)
Checking Positive or Negative Number
num = -10
if num >= 0:
print("Positive number")
else:
print("Negative number")
if-else with Boolean Values
is_logged_in = True
if is_logged_in:
print("Welcome")
else:
print("Please login")
if-else and Indentation
Python uses indentation to define the body of if and
else. Incorrect indentation results in an error.
num = 10
if num > 5:
print("Number is greater than 5")
else:
print("Number is 5 or less")
Multiple Statements inside if-else
marks = 75
if marks >= 40:
print("Result: Pass")
print("Congratulations!")
else:
print("Result: Fail")
print("Better luck next time.")
if-else with a List
numbers = [10, 20, 30]
if len(numbers) > 0:
print("List is not empty")
else:
print("List is empty")
Difference between if and if-else
| if | if-else |
|---|---|
| Executes a block only when the condition is True. | Executes one block when True and another block when False. |
| Does not require an else block. | Contains both if and else blocks. |
| Used when no action is required for False. | Used when an alternative action is required. |
Important Points
ifchecks a condition.elseexecutes when theifcondition is False.- A colon
:is required afterifandelse. - Python uses indentation to define blocks.
- The condition must produce a Boolean result.
- Comparison, logical and membership operators can be used in conditions.
- Only one of the
iforelseblocks executes in a simple if-else statement.