In Python, the if statement is used for conditional execution of code. It allows you to specify a block of code that will be executed only if a certain condition is met.
The basic syntax of an if statement in Python is as follows:
if condition: # indented code to be executed if the condition is True
You can also include an else block to specify code that should be executed when the condition is not met
Example
mood = input("Are you happy?") if mood == "happy": print("Clap your hands")
Note how what we are checking equivalence we use double equals, not single.
Else
You can also include an else block to specify code that should be executed when the condition is not met.
if condition: # code to be executed if the condition is True else: # code to be executed if the condition is False
Example
if name == "Richard": print("You have a good name") else: print("Meh!")
In a Python if-else statement, what happens when the condition evaluates to False?
{"answers": {"A": "Neither the if nor the else block executes.", "B": "The code inside the if block executes.", "C": "The code inside the else block executes.", "D": "Both the if and else blocks execute."}, "question_text": "In a Python if-else statement, what happens when the condition evaluates to False?", "correct_answer": "C"}
What is the primary purpose of an if-else statement in Python?
{"answers": {"A": "To define a function that can be called later", "B": "To store multiple values in a single variable", "C": "To conditionally execute different blocks of code based on a boolean expression", "D": "To repeat a block of code multiple times"}, "question_text": "What is the primary purpose of an if-else statement in Python?", "correct_answer": "C"}
What is the function of a nested if-else statement in Python?
{"answers": {"A": "It executes code only if a single condition is true, otherwise it skips the code.", "B": "It executes code if a condition is true, and terminates the program if the condition is false.", "C": "It evaluates a condition, and if false, evaluates another condition inside the else block, executing code based on these conditions.", "D": "It executes code if a condition is true and does nothing if the condition is false."}, "question_text": "What is the function of a nested if-else statement in Python?", "correct_answer": "C"}
What is the purpose of a chained if-else statement in Python?
{"answers": {"A": "To execute conditions in a random order", "B": "To execute only the first condition that is true", "C": "To execute the else block if no conditions are true", "D": "To execute all conditions that are true"}, "question_text": "What is the purpose of a chained if-else statement in Python?", "correct_answer": "B"}
IF ELIF ELSE
The if-elif-else statement is used when you have multiple conditions to check in a sequence. It allows you to test each condition one by one, and as soon as one of the conditions is satisfied, the corresponding block of code will be executed, and the rest of the branches will be skipped.
x = 10 if x > 10: print("x is greater than 10") elif x == 10: print("x is equal to 10") else: print("x is less than 10")
This is known as a chained if. The second if will only execute if the first if evaluated as false.
What role does the else statement play in a nested if-else structure in Python?
{"answers": {"A": "It repeats the if condition to verify its truth value.", "B": "It provides an alternative set of code to execute if the preceding if condition is false.", "C": "It executes code only if all previous if and elif conditions are false.", "D": "It terminates the program if the if condition is false."}, "question_text": "What role does the else statement play in a nested if-else structure in Python?", "correct_answer": "B"}
In Python, how many conditions can be evaluated in a chained if-elif-else statement?
{"answers": {"A": "Up to five conditions", "B": "Exactly two conditions", "C": "Only one condition", "D": "An unlimited number of conditions"}, "question_text": "In Python, how many conditions can be evaluated in a chained if-elif-else statement?", "correct_answer": "D"}
Nested If Statement
Nested if statements are used when you have an if statement inside another if statement. This allows you to create more complex conditions and control flows.
Each nested if statement is indented to indicate its level within the hierarchy.
x = 10 y = 5 if x > 0: if y > 0: print("Both x and y are positive") else: print("x is positive, but y is not") else: print("x is not positive")
In Python, how can you write an if statement to check if a variable 'number' is greater than 5?
{"answers": {"A": "if number >= 5:", "B": "if number > 5:", "C": "if number == 5:", "D": "if number < 5:"}, "question_text": "In Python, how can you write an if statement to check if a variable 'number' is greater than 5?", "correct_answer": "B"}
What is the output of the following Python code? x = 9 if x == 10: print('x is equal to 10') else: print('x is not equal to 10')
{"answers": {"A": "SyntaxError", "B": "x is equal to 9", "C": "x is not equal to 10", "D": "x is equal to 10"}, "question_text": "What is the output of the following Python code?\nx = 9\nif x == 10:\n print('x is equal to 10')\nelse:\n print('x is not equal to 10')", "correct_answer": "C"}
Which comparison operator is used to check if two numbers are equal?
{"answers": {"A": "<=", "B": "<", "C": "==", "D": ">"}, "question_text": "Which comparison operator is used to check if two numbers are equal?", "correct_answer": "C"}
Switch Statements
Switch states are a really useful tool for structured pattern matching. From Python 3.10 onwards it is supported using the match - case statement.
match expression: case value1: # Code executed if expression matches value1 break case value2: # Code executed if expression matches value2 break # ... (additional cases) case _: # Code executed if no case matches
When a switch statement is encountered, the expression value is evaluated once. Each case constant is compared against the expression value. Upon finding a match, the corresponding code block is executed until a break statement is encountered or the switch statement concludes. If none of the cases match the expression value, the code within the _ case is executed.
value = 'case2'
match value:
case 'case1':
print('This is case 1')
case 'case2':
print('This is case 2')
case 'case3':
print('This is case 3')
case _:
print('This is the default case')
What is the purpose of a switch statement in programming?
{"answers": {"A": "To execute different blocks of code based on the value of a variable", "B": "To declare and initialize variables", "C": "To repeat a block of code multiple times", "D": "To handle input and output operations"}, "question_text": "What is the purpose of a switch statement in programming?", "correct_answer": "A"}
In Python, when would you use a match-case statement (similar to a switch statement in other languages)?
{"answers": {"A": "When you want to execute different blocks of code based on the value of a variable", "B": "When you want to repeat a block of code multiple times", "C": "When you want to read input from the user", "D": "When you want to define a function"}, "question_text": "In Python, when would you use a match-case statement (similar to a switch statement in other languages)?", "correct_answer": "A"}
Switch statements < Python 3.10
Before Python 3.10 was released there wasn't a switch statement, so programmers had to emulate it using if statements.
value = 'case2'
if value == 'case1':
print('This is case 1')
elif value == 'case2':
print('This is case 2')
elif value == 'case3':
print('This is case 3')
else:
print('This is the default case')
In Python, how many 'case' clauses can a match statement have?
{"answers": {"A": "Only two case clauses", "B": "Exactly one case clause", "C": "No case clauses", "D": "Multiple case clauses"}, "question_text": "In Python, how many 'case' clauses can a match statement have?", "correct_answer": "D"}
How do you define multiple cases in a match statement in Python?
{"answers": {"A": "Define each case using the 'case' keyword within a 'match' statement", "B": "Separate each case with a comma inside a single case clause", "C": "Python does not support multiple cases in a match statement", "D": "Use the 'match' keyword followed by multiple 'case' clauses"}, "question_text": "How do you define multiple cases in a match statement in Python?", "correct_answer": "A"}
7. The [[switch]] statement is used when there are multiple possible cases and a different action needs to be taken for each case.
{"cloze_text": "7. The [[switch]] statement is used when there are multiple possible cases and a different action needs to be taken for each case."}
Review: Fill in the Blanks
When programming, there are different control structures that allow for the execution of specific code based on different s. Two common control structures are and .
Selection, on the other hand, allows the to be determined based on a condition. One way to achieve selection in programming is through the use of s. An if statement evaluates a condition and, if it is true, executes a specific block of code. If the condition is false, the block of code is skipped.
Another type of selection structure is the . A switch statement provides a convenient way to select one of many code blocks to be executed. It compares the condition with multiple cases and executes the block of code associated with the first true case. If none of the cases are true, a can be provided to execute code when none of the cases match.
Conditions are expressions that evaluate to a value, which is either true or false. A Boolean is a data type that can only have two values, representing true or false. Conditions are used within if statements, switch statements, and other control structures to determine which execution path to take based on their value.
In some cases, you may encounter s. These are if statements that are contained within another if statement's block of code. The inner if statement is only evaluated if the outer if statement's condition is true. This allows for more complex decision-making in programming.
Click the keywords below to fill in the blanks:
Switch Statement
Selection
Default Case
If Statement
Execution Path
Sequencing
Nested If Statement
Condition
Boolean