Loading samples...
Loading samples...
py-sample-6
Master if-else, elif, and nested conditions
Which keyword is used for 'else if' in Python?
How many elif blocks can follow an if?
What is mandatory after if statement?
What is the output of: True if 5 > 3 else False
Which is NOT a comparison operator?
What does pass do in an if block?
How to write 'if x is between 1 and 10'?
What is the ternary operator syntax?
Can if statements be nested?
What happens if condition is False and no else?
Explain the difference between if, elif, and else with a flow diagram.
[3 marks]What is nested if? When should it be used? Give an example.
[3 marks]Explain the inline if-else (ternary) operator with an example.
[3 marks]What is the difference between == and is operators? When to use each?
[3 marks]Explain the role of indentation in Python conditional statements.
[3 marks]How to check if a variable exists before using it in a condition?
[3 marks]Explain truthy and falsy values in Python with examples.
[3 marks]What is short-circuit evaluation in logical operators?
[3 marks]Explain match-case statement (Python 3.10+). How is it better than if-elif?
[3 marks]Write syntax for checking if a number is positive, negative, or zero.
[3 marks]Write a program to find the largest of three numbers using nested if-else.
[5 marks]Create a program to check if a year is a leap year.
[5 marks]Write a program to calculate electricity bill based on units consumed.
[5 marks]Create a simple calculator using if-elif to perform +, -, *, / operations.
[5 marks]Write a program to determine grade based on percentage using if-elif.
[5 marks]Create a program to check if a triangle is valid and its type (equilateral, isosceles, scalene).
[5 marks]Find error: if x > 5 print('Greater')
[2 marks]Explanation:Python requires a colon (:) after the if condition.
Predict output: x = 10 if x > 5: print('A') elif x > 8: print('B') else: print('C')
[2 marks]Explanation:Only the first true condition executes. x>5 is true, so 'A' prints. x>8 check is skipped.
Find error: if x = 10: print('Ten')
[2 marks]Explanation:Using assignment (=) instead of comparison (==). Should be: if x == 10:
Predict output: age = 20 status = 'Adult' if age >= 18 else 'Minor' print(status)
[2 marks]Explanation:Ternary operator: condition is true (20>=18), so 'Adult' is assigned.
Find error: if True: print('Yes') print('Definitely')
[3 marks]Explanation:Both print statements must have same indentation level to be in the same block.
Predict output: x = 5 if x > 10: print('A') if x > 3: print('B') else: print('C')
[3 marks]Explanation:Two separate if statements. First is false, second is true, so only 'B' prints.
Find error: if 'hello': print('Truthy') else print('Falsy')
[2 marks]Explanation:else statement also requires a colon (:) like if statement.
Predict output: num = 0 if num: print('Yes') else: print('No')
[2 marks]Explanation:0 is falsy in Python. The if condition evaluates to False, so else block executes.