Conditional Statements in Python
Conditional statements in Python allow you to make decisions in your code. They control the flow of execution depending on whether a given condition is True or False.
The if Statementโ
The simplest conditional statement is the if statement.
Syntax:
if condition:
# block of code
Example:
x = 10
if x > 5:
print("x is greater than 5")
The if...else Statementโ
The else block runs when the if condition is False.
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Output:
x is less than or equal to 5
The if...elif...else Statementโ
elif stands for "else if". It lets you check multiple conditions.
score = 85
if score >= 90:
print("Grade A")
elif score >= 75:
print("Grade B")
elif score >= 60:
print("Grade C")
else:
print("Grade D")
Nested if Statementsโ
You can put an if statement inside another if statement.
x = 15
if x > 10:
if x < 20:
print("x is between 10 and 20")
Conditional Expressions (Ternary Operator)โ
Python has a shorter way to write if...else using ternary expressions.
age = 18
status = "Adult" if age >= 18 else "Minor"
print(status)
Output:
Adult
Logical Operators in Conditionsโ
You can combine multiple conditions using and, or, and not.
x = 7
if x > 5 and x < 10:
print("x is between 5 and 10")
if x < 5 or x > 10:
print("x is outside 5 to 10")
if not x == 8:
print("x is not 8")
Comparing Multiple Valuesโ
You can check if a value exists in a sequence using in.
fruits = ["apple", "banana", "cherry"]
if "apple" in fruits:
print("Apple is in the list")
Indentation Rulesโ
In Python, indentation is important for defining code blocks.
if True:
print("This is indented correctly")
print("Still inside if block")
print("Outside if block")
Summary Tableโ
| Statement Type | Description |
|---|---|
if | Executes a block if condition is True |
if...else | Executes else block if condition is False |
if...elif...else | Checks multiple conditions |
Nested if | if inside another if |
| Ternary Expression | Short form of if...else |
Practice Questionsโ
1. Positive / Negative / Zero Checkerโ
Write a Python program that takes a number as input and checks whether it is positive, negative, or zero.
2. Odd or Evenโ
Write a Python program to check whether a number is even or odd.
3. Age Eligibility for Votingโ
Write a program to take a personโs age as input and check if they are eligible to vote (18 years or older).
4. Largest of Two Numbersโ
Write a Python program that takes two numbers as input and prints the larger number using conditional statements.
5. Largest of Three Numbersโ
Write a Python program to find the largest among three numbers entered by the user using if, elif, and else.
6. Grading Systemโ
Write a Python program to take a student's marks as input and print the grade according to the following criteria:
- Marks โฅ 90 โ Grade A
- Marks โฅ 75 and < 90 โ Grade B
- Marks โฅ 50 and < 75 โ Grade C
- Marks < 50 โ Grade F
7. Leap Year Checkerโ
Write a program to check whether a given year is a leap year or not. (Hint: A year is leap if divisible by 4 but not 100, or divisible by 400)
8. Nested If โ Number Range Checkerโ
Write a program that takes a number as input and:
- Checks if it's positive.
- If positive, further checks if it is less than 10, between 10 and 50, or greater than 50.
9. Character Classificationโ
Write a program to input a single character and check whether it is:
- a vowel,
- a consonant,
- a digit, or
- a special character.
10. Login Authentication (Simple)โ
Write a program that asks the user to enter a username and password.
- If the username is
"admin"and the password is"12345", print โLogin Successfulโ. - Otherwise, print โInvalid credentialsโ.
Conclusionโ
Conditional statements are essential for decision-making in programs. Mastering if, elif, and else allows you to control your program's logic effectively.