By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
By the end of this topic, students will be able to:
Conditional statements in Python allow the program to make decisions based on certain conditions. The if statement is used to check if a condition is true, and if it is, the code inside the if block is executed. The elif statement is used to check another condition if the first one is false, and the else statement is used to execute code if all conditions are false.
Loops in Python allow the program to repeat a task multiple times. The for loop is used to iterate over a sequence (such as a list or string), and the while loop is used to repeat a task while a certain condition is true.
if condition: code
elif condition: code
else: code
for variable in sequence: code
while condition: code
x = 5 if x > 10: print("x is greater than 10") elif x == 5: print("x is equal to 5") else: print("x is less than 10")
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
Write a Python program that asks the user for their age and prints out a message based on their age.
age = int(input("Enter your age: ")) if age >= 18: print("You are an adult.") elif age >= 13: print("You are a teenager.") else: print("You are a child.")
Write a Python program that prints out the numbers from 1 to 10.
for i in range(1, 11): print(i)
What is the purpose of the if statement in Python?
A) To repeat a task multiple times B) To make a decision based on a condition C) To iterate over a sequence D) To print a message to the user
Correct answer: B) To make a decision based on a condition
Why the distractors fail: A) The if statement is not used to repeat a task, but rather to make a decision.C) The for loop is used to iterate over a sequence, not the if statement.D) The print statement is used to print a message to the user, not the if statement.
What is the syntax of a for loop in Python?
A) for variable in sequence: code B) while condition: code C) if condition: code D) else: code
Correct answer: A) for variable in sequence: code
Why the distractors fail: B) The while loop is used to repeat a task while a condition is true, not the for loop.C) The if statement is used to make a decision based on a condition, not the for loop.D) The else statement is used to execute code if all conditions are false, not the for loop.
What is the purpose of the else statement in Python?
A) To make a decision based on a condition B) To repeat a task multiple times C) To iterate over a sequence D) To execute code if all conditions are false
Correct answer: D) To execute code if all conditions are false
Why the distractors fail: A) The if statement is used to make a decision based on a condition, not the else statement.B) The while loop is used to repeat a task while a condition is true, not the else statement.C) The for loop is used to iterate over a sequence, not the else statement.
What is the syntax of a while loop in Python?
A) while condition: code B) for variable in sequence: code C) if condition: code D) else: code
Correct answer: A) while condition: code
Why the distractors fail: B) The for loop is used to iterate over a sequence, not the while loop.C) The if statement is used to make a decision based on a condition, not the while loop.D) The else statement is used to execute code if all conditions are false, not the while loop.
What is the purpose of the break statement in Python?
A) To repeat a task multiple times B) To make a decision based on a condition C) To iterate over a sequence D) To exit a loop prematurely
Correct answer: D) To exit a loop prematurely
Why the distractors fail: A) The while loop is used to repeat a task while a condition is true, not the break statement.B) The if statement is used to make a decision based on a condition, not the break statement.C) The for loop is used to iterate over a sequence, not the break statement.
Explain the purpose of the if, elif, and else statements in Python.
Explain the purpose of the for and while loops in Python.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.