Fixed Top Ad (Local Preview)800x90 • Slot 6608427872
Conditional Statements
✕1. 🚦 Conditional Statements
- Imagine a traffic light.
If the light is green, vehicles move.
If the light is red, vehicles stop.
If the light is yellow, vehicles slow down.
A Python conditional statement works in a similar way. It allows a program to make decisions based on conditions.
Conditional statements are used to control the flow of code execution. They help us run different blocks of code depending on whether a condition is True or False.
Example:
age = 18if age >= 18:print("You can vote.")Output: You can vote. - A condition is an expression that gives either True or False.
Examples:
print(10 > 5)# ➜ Trueprint(10 < 5)# ➜ Falseprint(18 >= 18)# ➜ Trueprint("pdf" == "pdf")# ➜ True 📌ifstatements use these True or False results to decide which code block to run. - Indentation means the space at the beginning of a line. In Python, indentation defines the code block. Usually, we use 4 spaces for indentation.
📌 Avoid mixing spaces and tabs in the same program.
Example:
age = 18if age >= 18:print("You are an adult.")Here, the print statement belongs to theifblock because it is indented. ⚠️ If indentation is wrong, Python gives an IndentationError. 📌 Code insideif,elif, andelsemust be properly indented.
1.1 What are Conditional Statements?
1.2 Boolean Conditions
1.3 Indentation
2. ✅ if Statement
ifis used when we want to run a block of code only when a condition is True. Syntax:if condition:code to run if condition is TrueExample:age = 20if age >= 18:print("You are eligible for voting.")Output: You are eligible for voting.num = 10if num > 0:print("Number is positive.")Output: Number is positive.file_name = "report.pdf"if file_name.endswith(".pdf"):print("This is a PDF file.")Output: This is a PDF file.
2.1 What is if?
2.2 if Example with Number
2.3 if Example with String
3. 🔀 if else Statement
if elseis used when we want to run one block if the condition is True and another block if the condition is False. Syntax:if condition:code to run if condition is Trueelse:code to run if condition is FalseExample:age = 16if age >= 18:print("You are an adult.")else:print("You are a minor.")Output: You are a minor.age = 18if age >= 18:print("You are eligible for voting.")else:print("You are not eligible for voting.")Output: You are eligible for voting.num_1 = 15num_2 = 3if num_1 % num_2 == 0:print("15 is divisible by 3")else:print("15 is not divisible by 3")Output: 15 is divisible by 3
3.1 What is if else?
3.2 Voting Example
3.3 Divisibility Example
4. 🪜 if elif else Ladder
if elif elseis used when we need to check multiple conditions one by one. Python checks conditions from top to bottom. When one condition becomes True, its block runs and the remaining conditions are skipped. Syntax:if condition_1:code to run if condition_1 is Trueelif condition_2:code to run if condition_2 is Trueelif condition_3:code to run if condition_3 is Trueelse:code to run if none of the above conditions are True📌elifmeans else if. 📌 We can use multipleelifblocks. 📌elseis optional.marks = 85if marks >= 90:print("Grade: A")elif marks >= 80:print("Grade: B")elif marks >= 70:print("Grade: C")elif marks >= 60:print("Grade: D")else:print("Grade: F")Output: Grade: B 📌 Since marks is 85, the conditionmarks >= 80becomes True.file_name = "report.pdf"if file_name.endswith(".pdf"):print("This is a PDF file.")elif file_name.endswith(".docx"):print("This is a Word document.")elif file_name.endswith(".csv"):print("This is a CSV file.")else:print("Unknown file type.")Output: This is a PDF file.age = 16if age <= 12:print("You are a child.")elif age <= 19:print("You are a teenager.")elif age <= 59:print("You are an adult.")else:print("You are a senior.")Output: You are a teenager.
4.1 What is if elif else?
4.2 Grade Example
4.3 File Type Example
4.4 Life Stage Example
5. 🔗 Multiple Conditions
andis used when all conditions must be True. 📌 If even one condition is False, the whole and condition becomes False. Example:age = 25salary = 35000if age >= 21 and age <= 60 and salary >= 30000:print("You are eligible for loan.")else:print("You are not eligible for loan.")Output: You are eligible for loan.oris used when at least one condition must be True. 📌 If any one condition is True, the whole or condition becomes True. Example:day = "Saturday"if day == "Saturday" or day == "Sunday":print("It is weekend.")else:print("It is weekday.")Output: It is weekend.notis used to reverse the condition. Example:is_raining = Falseif not is_raining:print("You can go outside.")else:print("Take an umbrella.")Output: You can go outside.
5.1 Using and
5.2 Using or
5.3 Using not
6. 🧠 Nested if
- A nested if means an if statement inside another if statement. It is useful when one condition should be checked only after another condition is True.
Example:
age = 25salary = 35000if age >= 21:if salary >= 30000:print("You are eligible for loan.")else:print("Salary is too low.")else:print("Age is too low.")Output: You are eligible for loan. username = "admin"password = "1234"if username == "admin":if password == "1234":print("Login successful.")else:print("Wrong password.")else:print("Invalid username.")Output: Login successful.
6.1 What is Nested if?
6.2 Nested if Example with Login
7. ⚠️ Common Mistakes
- Wrong:
age = 18if age >= 18print("Adult")⚠️ This gives a SyntaxError because colon:is missing. 📌 SyntaxError means Python cannot understand the structure of the code. Correct:age = 18if age >= 18:print("Adult") - Wrong:
age = 18if age >= 18:print("Adult")⚠️ This gives an IndentationError becauseprintis not indented. Correct:age = 18if age >= 18:print("Adult") - Condition order matters in
if elif else. Wrong:marks = 85if marks >= 60:print("Grade: D")elif marks >= 80:print("Grade: B")Output: Grade: D Here,marks >= 60becomes True first, so Python does not checkmarks >= 80. Correct:marks = 85if marks >= 80:print("Grade: B")elif marks >= 60:print("Grade: D")Output: Grade: B
7.1 Missing Colon
7.2 Wrong Indentation
7.3 Wrong Condition Order
