The if-else statement in Python is used for decision-making. It allows your program to execute certain blocks of code based on specific conditions.
Executes a block of code if a condition is True
.
x = 10 if x > 5: print("x is greater than 5")
Executes one block if the condition is True
, another if False
.
age = 16 if age >= 18: print("You can vote") else: print("You are too young to vote")
Used when you have multiple conditions to test.
marks = 85 if marks >= 90: print("Grade A") elif marks >= 75: print("Grade B") elif marks >= 60: print("Grade C") else: print("Fail")
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!