All featured quizzes

Python - Basics

String methods, immutable data types, list comprehensions, string concatenation, and the range function.

Sample Questions

A preview of what's in this quiz. Answer them interactively for instant scoring and full explanations.

What does the `len()` function do in Python?

1. What does the `len()` function do in Python?

  • A.Returns the character count of a file
  • B.Returns the number of items in an object
  • C.Calculates the memory size of an object
  • D.Finds the maximum value in a list
What is the correct way to declare a variable in Python?

2. What is the correct way to declare a variable in Python?

  • A.var x = 5
  • B.x = 5
  • C.int x = 5
  • D.let x = 5
Which of the following is an immutable data type in Python?

3. Which of the following is an immutable data type in Python?

  • A.list
  • B.tuple
  • C.dict
  • D.set
How do you write 'Else If' in a Python conditional statement?

4. How do you write 'Else If' in a Python conditional statement?

  • A.else if
  • B.elif
  • C.elseif
  • D.elsif
How do you start a single-line comment in Python?

5. How do you start a single-line comment in Python?

  • A.// This is a comment
  • B.# This is a comment
  • C./* This is a comment */
  • D.<!-- This is a comment -->
Which method is used to add an item to the end of a list in Python?

6. Which method is used to add an item to the end of a list in Python?

  • A.add()
  • B.append()
  • C.insert()
  • D.push()
What does the `range(5)` function produce in a loop?

7. What does the `range(5)` function produce in a loop?

  • A.1, 2, 3, 4, 5
  • B.0, 1, 2, 3, 4
  • C.0, 1, 2, 3, 4, 5
  • D.5, 4, 3, 2, 1
How do you create a function in Python?

8. How do you create a function in Python?

  • A.function my_function()
  • B.def my_function():
  • C.create my_function()
  • D.define my_function():
Which operator is used for exponentiation (raising to a power) in Python?

9. Which operator is used for exponentiation (raising to a power) in Python?

  • A.^
  • B.**
  • C.pow
  • D.*
Which collection type in Python does NOT allow duplicate elements?

10. Which collection type in Python does NOT allow duplicate elements?

  • A.list
  • B.set
  • C.tuple
  • D.dictionary
What is the result of `10 // 3` in Python?

11. What is the result of `10 // 3` in Python?

  • A.3.3333333333333335
  • B.3
  • C.1
  • D.4
What is the result of `10 % 3` in Python?

12. What is the result of `10 % 3` in Python?

  • A.3
  • B.1
  • C.0
  • D.2
Which of the following is NOT a valid variable name in Python?

13. Which of the following is NOT a valid variable name in Python?

  • A.my_var2
  • B.2my_var
  • C._my_var
  • D.myVar
How do you access the value associated with the key 'name' in dictionary `d = {'name': 'Alice', 'age': 25}`?

14. How do you access the value associated with the key 'name' in dictionary `d = {'name': 'Alice', 'age': 25}`?

  • A.d.name
  • B.d['name']
  • C.d(name)
  • D.d.get(0)
How do you open a file named 'data.txt' for reading in Python?

15. How do you open a file named 'data.txt' for reading in Python?

  • A.open('data.txt', 'w')
  • B.open('data.txt', 'r')
  • C.read('data.txt')
  • D.file('data.txt')
In a `try-except` block, when does the `finally` block execute?

16. In a `try-except` block, when does the `finally` block execute?

  • A.Only when an exception is raised
  • B.Always, whether an exception occurred or not
  • C.Only when no exception is raised
  • D.Only when the program exits successfully
What is the recommended statement to open files in Python to ensure they are closed automatically?

17. What is the recommended statement to open files in Python to ensure they are closed automatically?

  • A.try open('file.txt') as f:
  • B.with open('file.txt') as f:
  • C.using open('file.txt') as f:
  • D.open('file.txt').auto_close()
What does the `break` statement do in a loop?

18. What does the `break` statement do in a loop?

  • A.Skips the current iteration and goes to the next
  • B.Terminates the loop immediately
  • C.Exits the entire Python program
  • D.Pauses the loop execution for a set time
What is the output of `print(type(5.0))`?

19. What is the output of `print(type(5.0))`?

  • A.<class 'int'>
  • B.<class 'float'>
  • C.<class 'str'>
  • D.<class 'double'>
Which function is used to convert a string to a floating-point number in Python?

20. Which function is used to convert a string to a floating-point number in Python?

  • A.str()
  • B.float()
  • C.int()
  • D.double()

Looking for a different topic? Browse all subjects