If Flashcards
(26 cards)
What keyword is used to start an if statement in Python?
if
True or False: The condition in an if statement must always evaluate to a boolean value.
True
Fill in the blank: An if statement can be followed by an ______ statement to execute code if the condition is false.
else
What is the purpose of an if statement?
To execute a block of code conditionally based on whether a specified condition is true.
What will be the output of the following code: if 5 > 3: print(‘Hello’)?
‘Hello’
What keyword is used to introduce an alternative block in an if statement?
else
True or False: You can have multiple elif statements in a single if-else structure.
True
What is the syntax for an if statement with an elif clause?
if condition1: # code block 1 elif condition2: # code block 2
What will happen if none of the conditions in an if-elif-else structure are true?
The code in the else block will be executed if it is present.
In the following code, what will be printed? if x = 10: print(‘Ten’)
Syntax error (should use ‘==’ for comparison)
What is the output of the following code? x = 10; if x < 5: print(‘Low’); else: print(‘High’)
‘High’
True or False: Indentation is not important in Python when writing if statements.
False
What is the correct way to write a nested if statement?
if condition1: if condition2: # code block
Which of the following is a valid if statement? A) if (x > 10) B) if x > 10: C) if x > 10 {}
B) if x > 10:
What will this code output? if True: print(‘Always runs’)
‘Always runs’
How do you check if a variable ‘a’ is equal to 5 in an if statement?
if a == 5:
Fill in the blank: The ______ statement is used to check multiple expressions for truthiness in a compact form.
elif
What is the correct format for an if statement that checks if a number is positive?
if number > 0:
True or False: An if statement can directly check for multiple conditions using logical operators.
True
What does the ‘and’ operator do in an if statement?
It evaluates to True if both conditions are True.
What does the ‘or’ operator do in an if statement?
It evaluates to True if at least one of the conditions is True.
What will be the output of the following code? if False and True: print(‘This will not run’)
No output
What is the output of this code? if 2 + 2 == 4: print(‘Math works’)
‘Math works’
What is the difference between ‘==’ and ‘=’ in Python?
’==’ is used for comparison, while ‘=’ is used for assignment.