30 Python Quiz Questions and Answers

Python is a versatile and popular high-level programming language known for its simplicity, readability, and extensive range of libraries and frameworks. It was created by Guido van Rossum and released in 1991, designed with a focus on code readability and ease of use.

Python offers a clean and intuitive syntax, making it beginner-friendly and widely adopted by programmers of all levels. It emphasizes code readability by utilizing indentation and a minimalistic approach, which allows developers to write clear and concise code.

One of Python’s key strengths is its extensive standard library, which provides a vast collection of pre-built modules and functions for various tasks. Additionally, Python’s open-source nature has led to a vibrant community that contributes to an ever-growing ecosystem of third-party libraries, making it suitable for a wide range of applications, including web development, data analysis, scientific computing, artificial intelligence, machine learning, and automation.

Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming styles. This versatility allows developers to choose the most appropriate approach for their projects.

Overall, Python’s simplicity, readability, extensive libraries, and broad community support have made it a go-to language for developers across various domains, contributing to its widespread adoption and popularity in the programming community.

Just to let you know

Sign up for a free OnlineExamMaker account to create an interactive online quiz in minutes – automatic grading & mobile friendly.

In this article

Part 1: 30 Python quiz questions & answers

1. What is Python?
a) A type of snake
b) A high-level programming language
c) A mathematical equation
d) A computer operating system
Answer: b) A high-level programming language

2. What is the syntax used for defining a function in Python?
a) def
b) fun
c) function
d) define
Answer: a) def

3. Which of the following is not a valid variable name in Python?
a) my_variable
b) 123_variable
c) _variable
d) variable123
Answer: b) 123_variable

4. What is the output of the following code snippet?
print(3 + 4 * 2)
a) 14
b) 11
c) 10
d) 21
Answer: b) 11

5. How do you comment a single line in Python?
a) // This is a comment
b) /* This is a comment */
c) # This is a comment
d)
Answer: c) # This is a comment

6. What is the output of the following code snippet?
my_string = “Hello, World!”
print(my_string[7:])
a) Hello
b) World
c) Hello, World
d) Error
Answer: b) World

7. What is the correct way to create an empty list in Python?
a) my_list = []
b) my_list = {}
c) my_list = ()
d) my_list = [“”]
Answer: a) my_list = []

8. Which data type is used to store a sequence of characters in Python?
a) int
b) float
c) str
d) bool
Answer: c) str

9. What is the output of the following code snippet?
print(len(“Python”))
a) 5
b) 6
c) 7
d) Error
Answer: a) 5

10. How do you convert a string to lowercase in Python?
a) string.upper()
b) string.lower()
c) string.capitalize()
d) string.swapcase()
Answer: b) string.lower()

11. Which keyword is used to exit a loop prematurely in Python?
a) exit
b) return
c) continue
d) break
Answer: d) break

12. What is the result of the following code snippet?
my_tuple = (“apple”, “banana”, “cherry”)
print(len(my_tuple))
a) 3
b) 5
c) 6
d) Error
Answer: a) 3

13. Which operator is used for exponentiation in Python?
a) ^
b) **
c) %
d) //
Answer: b) **

14. How do you check if a key exists in a dictionary?
a) key.exists()
b) key.in()
c) key.isin()
d) key in dictionary
Answer: d) key in dictionary

15. What is the output of the following code snippet?
my_list = [1, 2, 3, 4, 5]
print(my_list[2:4])
a) [1, 2]
b) [3, 4]
c) [2, 3]
d) [3, 4, 5]
Answer: b) [3, 4]

Part 2: Download Python quiz questions & answers for free

Download questions & answers for free

16. Which module is used for date and time manipulation in Python?
a) datetime
b) math
c) random
d) os
Answer: a) datetime

17. How do you open a file in Python?
a) open(file_name, “r”)
b) open(“r”, file_name)
c) open(“r”, “file_name”)
d) open(file_name)
Answer: a) open(file_name, “r”)

18. What is the output of the following code snippet?
x = 5
y = 2
print(x % y)
a) 2
b) 3
c) 0
d) 2.5
Answer: a) 1

19. What is the purpose of the “pass” statement in Python?
a) To end the program
b) To skip a loop iteration
c) To print a message
d) To assign a value to a variable
Answer: b) To skip a loop iteration

20. How do you add an element to the end of a list in Python?
a) list.insert(0, element)
b) list.append(element)
c) list.add(element)
d) list.extend(element)
Answer: b) list.append(element)

21. What is the output of the following code snippet?
x = 10
y = 5
print(x > y)
a) True
b) False

c) Error
d) None
Answer: a) True

22. Which function is used to find the maximum value in a list in Python?
a) min()
b) max()
c) sum()
d) len()
Answer: b) max()

23. How do you remove an element from a list in Python?
a) list.remove(element)
b) list.delete(element)
c) list.pop(element)
d) list.clear(element)
Answer: a) list.remove(element)

24. What is the output of the following code snippet?
x = True
y = False
print(x and y)
a) True
b) False
c) Error
d) None
Answer: b) False

Pro Tip

Want to assess your learners online? Create an online quiz for free!

25. Which module is used for regular expression operations in Python?
a) math
b) datetime
c) re
d) sys
Answer: c) re

26. How do you convert a string to an integer in Python?
a) str()
b) float()
c) int()
d) bool()
Answer: c) int()

27. What is the output of the following code snippet?
my_set = {1, 2, 3, 4}
my_set.add(5)
print(my_set)
a) {1, 2, 3, 4}
b) {1, 2, 3, 4, 5}
c) {2, 3, 4, 5}
d) Error
Answer: b) {1, 2, 3, 4, 5}

28. How do you round a floating-point number to the nearest integer in Python?
a) round(number)
b) ceil(number)
c) floor(number)
d) int(number)
Answer: a) round(number)

29. What is the purpose of the “else” statement in a try-except block in Python?
a) To handle an exception
b) To specify the type of exception
c) To execute a block of code if no exception occurs
d) To terminate the program
Answer: c) To execute a block of code if no exception occurs

30. How do you convert a list to a tuple in Python?
a) tuple()
b) list()
c) set()
d) dict()
Answer: a) tuple()

Part 3: Free online quiz creator – OnlineExamMaker

OnlineExamMaker’s secure, powerful web-based quiz maker is an easy-to-use, intelligent online testing software tool for business, training & educational to create exams & quizzes with ease. With its user friendly interface and extensive range of features, OnlineExamMaker simplifies the process of creation and distributing online quizzes to engage learners, improve knowledge retention, and assess performance.

Create Your Next Quiz/Exam with OnlineExamMaker

SAAS, free forever
100% data ownership