Conditional statements inPythonare used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations.
If Conditional Statement in Python
If statement is the simplest form of a conditional statement. It executes a block of code if the given condition is true.
If StatementExample:
Pythonage=20ifage>=18:print("Eligible to vote.")
Short Hand if
Short-hand if statement allows us to write a single-line if statement.
Example:
Pythonage=19ifage>18:print("Eligible to Vote.")
This is a compact way to write an if statement. It executes the print statement if the condition is true.
If else Conditional Statements in Python
Else allows us to specify a block of code that will execute if the condition(s) associated with an if or elif statement evaluates to False. Else block provides a way to handle all other cases that don't meet the specified conditions.
If Else StatementExample:
Pythonage=10ifage<=12:print("Travel for free.")else:print("Pay for ticket.")
Short Hand if-else
The short-hand if-else statement allows us to write a single-line if-else statement.
Example:
Pythonmarks=45res="Pass"ifmarks>=40else"Fail"print(f"Result:{res}")
Note: This method is also known as ternary operator. Ternary Operator essentially a shorthand for the if-else statement that allows us to write more compact and readable code, especially for simple conditions.
elif Statement
elif statement in Python stands for "else if." It allows us to check multiple conditions , providing a way to execute different blocks of code based on which condition is true. Using elif statements makes our code more readable and efficient by eliminating the need for multiple nested if statements.
Example:
Pythonage=25ifage<=12:print("Child.")elifage<=19:print("Teenager.")elifage<=35:print("Young adult.")else:print("Adult.")
The code checks the value of age using if-elif-else. Since age is 25, it skips the first two conditions (age <= 12 and age <= 19), and the third condition (age <= 35) is True, so it prints "Young adult.".
Nested if..else Conditional Statements in Python
Nested if..else means an if-else statement inside another if statement. We can use nested if statements to check conditions within conditions.
Nested If ElsePythonage=70is_member=Trueifage>=60:ifis_member:print("30% senior discount!")else:print("20% senior discount.")else:print("Not eligible for a senior discount.")
Output30% senior discount!
Ternary Conditional Statement in Python
Aternary conditional statement is a compact way to write an if-else condition in a single line. It’s sometimes called a "conditional expression."
Example:
Python# Assign a value based on a conditionage=20s="Adult"ifage>=18else"Minor"print(s)
Here:
- If age >= 18 is True, status is assigned "Adult".
- Otherwise, status is assigned "Minor".
Match-Case Statement in Python
match-case statement is Python's version of a switch-case found in other languages. It allows us to match a variable's value against a set of patterns.
Example:
Pythonnumber=2matchnumber:case1:print("One")case2|3:print("Two or Three")case_:print("Other number")
Output:
Two or Three
Quiz:
Related Posts:
Recommended Problems: