Learn Python, Microsoft 365 and Google Workspace
What is the syntax for a simple if statement in Python? [Python Quiz #50] - A) if x == 10: - B) for x in range(10): - C) while x < 10: - D) if (x == 10) then:
x = 10
y = 5
if x == y * 2:
print("True")
else:
print("False")
- A) True
- B) False
- C) Error
- D) Nothing
Watch this video for the answer: https://youtube.com/shorts/ExDu2lwjd3c?si=VVq47sCNgRWd7l_i
x = 3
y = 5
if x == 3 and y != 5:
print("True")
else:
print("False")
- A) True
- B) False
- C) Syntax error
- D) Name error
Watch the video for the answer: https://youtube.com/shorts/7uAgT3_P4Oc?si=evBHSymYx2kVaEEk
x = 10
if 5 < x < 15:
print("x is between 5 and 15")
else:
print("x is not between 5 and 15")
# fixes the error
x = 10
if x == 10
print("x is 10")
- A) Remove the comment.
- B) Add a colon after `if x == 10`.
- C) Add parentheses around `x == 10`.
- D) Indent the print statement correctly.
Watch the video for the answer: https://youtu.be/3010E1WuHd8
What is the purpose of the else block in an if statement?
What happens when none of the conditions in an if-elif-else chain are True, and there is no else block?
x = 10
y = 5
if x < y:
print("x is greater than y")
- A) "x is greater than y"
- B) "x is less than y"
- C) Nothing will be printed
- D) An error will occur
What happens if none of the conditions in an if-elif chain are True, and there is no else block?
What will be the output of the following code?
number = 10
if number % 2 == 0:
print("Number is even")
else:
print("Number is odd")
- A) Number is odd
- B) Number is even
- C) Nothing
- D) Error
- A) Prints "Positive" if x is greater than 0, "Negative" if x is less than 0, and "Zero" if x is 0.
- B) Prints "Positive" if x is less than 0, "Negative" if x is greater than 0, and "Zero" if x is 0.
- C) Prints "Positive" if x is 0, "Negative" if x is greater than 0, and "Zero" if x is less than 0.
- D) Causes an error because the conditions are conflicting.
11. **What is the correct syntax for an if-else statement?**
- A) if condition:
code block
- B) if condition:
code block
else:
code block
- C) if condition:
code block
elif condition:
code block
- D) if condition:
code block
else:
code block
elif condition:
code block
12. **What happens if none of the conditions in an if-elif-else block are true?**
- A) The program terminates
- B) The else block is executed
- C) An error occurs
- D) The program continues without executing any block
14. **What will be the output of the following code?**
```python
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
- A) Even
- B) Odd
- C) Error
- D) No output
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
- A) Grade: A
- B) Grade: B
- C) Grade: C
- D) Grade: F
x = 5
if x > 10:
print("Greater than 10")
elif x > 5:
print("Greater than 5 but not greater than 10")
else:
print("5 or less")
- A) Greater than 10
- B) Greater than 5 but not greater than 10
- C) 5 or less
- D) No output
Answer: C
Which of the following is true about the elif
statement?
elif
stands for “else if” and is used to check additional conditions after the if
statementelif
is the same as else
elif
must be followed by an else
statementelif
statements in one blockAnswer: A, D
y = 20
if y < 20:
print("Less than 20")
elif y == 20:
print("Equal to 20")
else:
print("Greater than 20")
- A) Less than 20
- B) Equal to 20
- C) Greater than 20
- D) None of the above
Answer: B
a = 10
b = 5
if a > b:
if a > 0:
print("a is positive")
else:
print("a is non-positive")
else:
print("a is less than or equal to b")
- A) a is positive
- B) a is non-positive
- C) a is less than or equal to b
- D) No output
Answer: A
Which of the following statements is false about if-elif-else
statements in Python?
elif
statements are optional in an if-elif-else
constructif
statement can be followed by multiple elif
statementselse
statement is mandatory in an if-elif-else
constructif
statement can exist without elif
or else
Answer: C
x = 0
if x:
print("True")
else:
print("False")
- A) True
- B) False
- C) No output
- D) Error
Answer: B
Truthy and Falsy Values in Python In Python, certain values are considered False when evaluated in a boolean context (like in an if statement). These values include:
Which of the following expressions can be used in an if
condition to check if a number n
is between 1 and 100 inclusive?
if n > 1 and n < 100:
if n >= 1 and n <= 100:
if 1 <= n <= 100:
if n == 1 or n == 100:
Answer: B, C
z = 7
if z < 5:
print("Less than 5")
elif z == 10:
print("Equal to 10")
elif z > 5:
print("Greater than 5 but not 10")
else:
print("Something else")
- A) Less than 5
- B) Equal to 10
- C) Greater than 5 but not 10
- D) Something else
Answer: C
What is the purpose of the else
statement in an if-elif-else
structure?
if-elif-else
structureAnswer: B
x = 10
if x > 5:
if x == 10:
print("x is 10")
else:
print("x is greater than 5 but not 10")
else:
print("x is 5 or less")
- A) x is 10
- B) x is greater than 5 but not 10
- C) x is 5 or less
- D) No output
Answer: A
if-elif-else
statement in Python?A)
if x > y:
print("x is greater")
elif x == y:
print("x is equal to y")
else:
print("x is less")
B)
if x > y:
print("x is greater")
elif x == y:
print("x is equal to y")
else:
print("x is less")
C)
if (x > y):
print("x is greater")
elif (x == y):
print("x is equal to y")
else:
print("x is less")
D)
if x > y:
print("x is greater")
elif x == y
print("x is equal to y")
else
print("x is less")
Answer: B, C
a = 3
b = 4
if a > b:
print("a is greater")
elif a == b:
print("a and b are equal")
else:
print("b is greater")
- A) a is greater
- B) a and b are equal
- C) b is greater
- D) No output
Answer: C
n = 7
?if n < 5:
print("n is less than 5")
elif n < 10:
print("n is less than 10")
else:
print("n is 10 or more")
- A) n is less than 5
- B) n is less than 10
- C) n is 10 or more
- D) None of the above
Answer: B
Which keyword is used to check an additional condition if the initial if
statement is False
?
Answer: B
x = 0
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
- A) Positive
- B) Zero
- C) Negative
- D) No output
Answer: B
How many elif
clauses can be included in an if-elif-else
statement?
Answer: C
age = 20
if age < 18:
print("Minor")
elif age >= 18 and age < 65:
print("Adult")
else:
print("Senior")
- A) Minor
- B) Adult
- C) Senior
- D) No output
Answer: B