Python quiz code
Python quiz code
Here is a Python quiz with 10 questions:
Quiz: "Python for Beginners"
What is the print() function used for?a) To read input from user
b) To display output to screen
c) To perform arithmetic operations
d) To execute a loop
Answer: b) To display output to screen
Which of the following variables are in scope in Python?a) x, y
b) only x
c) only y
d) neither x nor y
Answer: a) x, y
What is the purpose of theimport
statement?
a) To execute an external program
b) To load a Python module into memory
c) To define a variable's scope
d) To specify an input file
Answer: b) To load a Python module into memory
How do you add comments to your Python code?a) With #
symbol
b) With //
symbols
c) With /* */
symbols
d) With '''
triple quotes
Answer: a) With #
symbol
len()
function?
a) To convert a string to uppercase
b) To calculate the length of a sequence (such as string, list, or tuple)
c) To capitalize the first letter of a string
d) To concatenate two strings
Answer: b) To calculate the length of a sequence (such as string, list, or tuple)
Which Python type can hold multiple values?a) Int
b) Float
c) String
d) List
Answer: d) List
How do you create a new dictionary in Python?a) With dict()
function
b) With {}
syntax
c) With []
syntax
d) With ()
parentheses
Answer: b) With {}
syntax
for
loop?
a) To execute an arithmetic operation repeatedly
b) To repeat a block of code multiple times based on some condition
c) To sort a list of items
d) To filter out unwanted elements from a sequence
Answer: b) To repeat a block of code multiple times based on some condition
What is the purpose of theif
statement?
a) To execute a block of code when some condition is met
b) To skip to the next line if some condition is not met
c) To return from a function immediately
d) To pause program execution for a few seconds
Answer: a) To execute a block of code when some condition is met
What happens when you runprint("Hello, World!")
?
a) The program will display an error message
b) The program will execute the arithmetic operation 1+2
c) The program will print "Hello, World!" on screen
d) The program will create a new file named "Hello, World!"
Answer: c) The program will print "Hello, World!" on screen
Python conditional statements quiz with answers for loop
Here's a Python conditional statements and for loop quiz with answers:
Conditional Statements Quiz
Write a Python code that checks if a variablex
is equal to 5.
If the condition is true, print "The value of x is 5". Otherwise, print "The value of x is not 5".
Answer:
x = 5
if x == 5:
print("The value of x is 5")
else:
print("The value of x is not 5")
Write a Python code that checks if a variable y
is greater than 10.
If the condition is true, print "The value of y is greater than 10". Otherwise, print "The value of y is not greater than 10".
Answer:
y = 12
if y > 10:
print("The value of y is greater than 10")
else:
print("The value of y is not greater than 10")
Write a Python code that checks if a variable z
is less than or equal to 2.
If the condition is true, print "The value of z is less than or equal to 2". Otherwise, print "The value of z is greater than 2".
Answer:
z = 1.5
if z <= 2:
print("The value of z is less than or equal to 2")
else:
print("The value of z is greater than 2")
Write a Python code that checks if a variable w
is not equal to 7.
If the condition is true, print "The value of w is not equal to 7". Otherwise, print "The value of w is equal to 7".
Answer:
w = 9
if w != 7:
print("The value of w is not equal to 7")
else:
print("The value of w is equal to 7")
Write a Python code that checks if a variable v
is within the range 4 and 6 (inclusive).
If the condition is true, print "The value of v is between 4 and 6". Otherwise, print "The value of v is not between 4 and 6".
Answer:
v = 5
if 4 <= v <= 6:
print("The value of v is between 4 and 6")
else:
print("The value of v is not between 4 and 6")
For Loop Quiz
Write a Python code that uses a for loop to iterate over the range 1 to 5, and prints each number.Answer:
for i in range(1, 6):
print(i)
Write a Python code that uses a for loop to iterate over a list fruits
containing "apple", "banana", "cherry", and prints each fruit.
Answer:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Write a Python code that uses a for loop to iterate over the range 1 to 10, and sums up all the numbers.
Answer:
total = 0
for i in range(1, 11):
total += i
print(total)
Write a Python code that uses a for loop to iterate over a dictionary students
containing student names and grades, and prints each student's name and grade.
Answer:
students = {"John": 90, "Mary": 85, "Jane": 95}
for student, grade in students.items():
print(f"{student}: {grade}")
Write a Python code that uses a for loop to iterate over the range 1 to 20, and prints every other number (i.e., 2, 4, 6, ...).
Answer:
for i in range(0, 21, 2):
print(i)
I hope this quiz helps you practice your Python skills!