Skip to content
🐍

Python Basics

150 exercises

Practice exercises for Python.

  1. 1

    Python Variable Naming Quiz

    Test your knowledge of Python variable naming rules and conventions.

    Multiple Choice Easy
  2. 2

    Assign a Python Variable

    Practice using the correct assignment operator to set a Python variable.

    Fill in the Blank Beginner
  3. 3

    Python Dynamic Typing

    Check your understanding of Python's dynamic typing system.

    Concept Check Medium
  4. 4

    Python Function Definition Quiz

    Test your knowledge of the keyword used to define functions in Python.

    Multiple Choice Beginner
  5. 5

    Predict Python Function Output

    Predict the output of a simple Python function that squares a number.

    Predict Output Beginner
  6. 6

    Predict Python if/elif/else Output

    Predict the output of a Python if/elif/else chain that classifies a number.

    Predict Output Beginner
  7. 7

    Complete the Python for Loop

    Fill in the blank to make a Python for loop iterate over a range of numbers 0 to 4.

    Fill in the Blank Beginner
  8. 8

    Python Integer Division Result

    Quiz on the result of the // floor-division operator in Python.

    Multiple Choice Easy
  9. 9

    Python Modulo Operator

    Quiz on the % modulo operator and remainder calculation.

    Multiple Choice Easy
  10. 10

    Python Boolean Type

    Quiz on the bool type and truthy/falsy values in Python.

    Multiple Choice Easy
  11. 11

    Python String Immutability

    Quiz on whether Python strings are mutable or immutable.

    Multiple Choice Medium
  12. 12

    Python List Slicing

    Quiz on list slicing syntax and behavior.

    Multiple Choice Easy
  13. 13

    Python List append vs extend

    Quiz on the difference between append and extend for lists.

    Multiple Choice Medium
  14. 14

    Python Dictionary Key Types

    Quiz on which types can be used as dictionary keys.

    Multiple Choice Medium
  15. 15

    Python Default Argument

    Quiz on default parameter values in Python functions.

    Multiple Choice Easy
  16. 16

    Python Mutable Default Argument Pitfall

    Quiz on the classic mutable default argument bug.

    Multiple Choice Medium
  17. 17

    Python while Loop Condition

    Quiz on while loop termination conditions.

    Multiple Choice Easy
  18. 18

    Python for Loop with range

    Quiz on range() with two arguments.

    Multiple Choice Easy
  19. 19

    Python String Methods

    Quiz on string methods like upper and strip.

    Multiple Choice Easy
  20. 20

    Python Tuple Unpacking

    Quiz on tuple unpacking into multiple variables.

    Multiple Choice Easy
  21. 21

    Python Set Properties

    Quiz on set behavior — uniqueness and ordering.

    Multiple Choice Medium
  22. 22

    Python try/except Behavior

    Quiz on how try/except blocks work.

    Multiple Choice Easy
  23. 23

    Python self Parameter

    Quiz on the self parameter in Python methods.

    Multiple Choice Medium
  24. 24

    Python __init__ Method

    Quiz on the constructor method __init__.

    Multiple Choice Easy
  25. 25

    Python Import Statement

    Quiz on importing modules in Python.

    Multiple Choice Easy
  26. 26

    Python List Comprehension Syntax

    Quiz on reading and understanding list comprehensions.

    Multiple Choice Medium
  27. 27

    Python Lambda Syntax

    Quiz on anonymous lambda functions in Python.

    Multiple Choice Medium
  28. 28

    Python File Opening Mode

    Quiz on file open modes like 'r', 'w', 'a'.

    Multiple Choice Easy
  29. 29

    Python datetime Module

    Quiz on getting the current date and time.

    Multiple Choice Easy
  30. 30

    Python JSON Parsing

    Quiz on converting a JSON string to a Python object.

    Multiple Choice Easy
  31. 31

    Python Generator Function

    Quiz on generators and the yield keyword.

    Multiple Choice Medium
  32. 32

    Python Decorator Purpose

    Quiz on what decorators do in Python.

    Multiple Choice Medium
  33. 33

    Python Inheritance Basics

    Quiz on class inheritance and method resolution.

    Multiple Choice Medium
  34. 34

    Python with Statement

    Quiz on the with statement and context managers.

    Multiple Choice Easy
  35. 35

    Python Type Hints

    Quiz on type hint syntax for function signatures.

    Multiple Choice Medium
  36. 36

    Python Regex Module

    Quiz on the re module and pattern matching.

    Multiple Choice Medium
  37. 37

    Python PEP 8 Style

    Quiz on PEP 8 naming conventions.

    Multiple Choice Easy
  38. 38

    Predict Variable Reassignment

    Predict the output after variable reassignment.

    Predict Output Beginner
  39. 39

    Predict String Concatenation

    Predict the output of string concatenation with +.

    Predict Output Beginner
  40. 40

    Predict f-string Output

    Predict the output of an f-string with an expression.

    Predict Output Easy
  41. 41

    Predict List Indexing

    Predict the output of list indexing with positive and negative indices.

    Predict Output Easy
  42. 42

    Predict List Method Output

    Predict the output after pop and append operations.

    Predict Output Easy
  43. 43

    Predict Dictionary Access

    Predict the output of dictionary key access and get().

    Predict Output Easy
  44. 44

    Predict for Loop Sum

    Predict the output of a for loop that accumulates a sum.

    Predict Output Easy
  45. 45

    Predict Function with Default Arg

    Predict the output when calling a function with and without arguments.

    Predict Output Easy
  46. 46

    Predict String Split

    Predict the output of splitting a string.

    Predict Output Easy
  47. 47

    Predict Nested if/else

    Predict the output of nested conditional logic.

    Predict Output Medium
  48. 48

    Predict List Comprehension with Filter

    Predict the output of a list comprehension with an if clause.

    Predict Output Medium
  49. 49

    Predict Tuple Packing and Unpacking

    Predict the output of tuple packing and unpacking.

    Predict Output Easy
  50. 50

    Predict Set Operations

    Predict the output of union and intersection on sets.

    Predict Output Medium
  51. 51

    Predict Multiple Return Values

    Predict the output of a function returning a tuple.

    Predict Output Medium
  52. 52

    Predict try/except/finally

    Predict the output when an exception is caught.

    Predict Output Medium
  53. 53

    Predict Class Instance Output

    Predict the output of creating and using a class instance.

    Predict Output Medium
  54. 54

    Predict Lambda with sorted

    Predict the output of sorting with a lambda key.

    Predict Output Medium
  55. 55

    Predict Generator Output

    Predict the output of consuming a generator with list().

    Predict Output Medium
  56. 56

    Predict Dict Comprehension

    Predict the output of a dictionary comprehension.

    Predict Output Medium
  57. 57

    Predict String Repetition

    Predict the output of multiplying a string by an integer.

    Predict Output Beginner
  58. 58

    Fill: List Append

    Fill in the method to add an element to the end of a list.

    Fill in the Blank Beginner
  59. 59

    Fill: String Length

    Fill in the function to get the length of a string.

    Fill in the Blank Beginner
  60. 60

    Fill: Dict Get with Default

    Fill in the method to safely access a dictionary key.

    Fill in the Blank Easy
  61. 61

    Fill: String Uppercase

    Fill in the method to convert a string to uppercase.

    Fill in the Blank Beginner
  62. 62

    Fill: Break from Loop

    Fill in the keyword to exit a loop early.

    Fill in the Blank Beginner
  63. 63

    Fill: Function Return

    Fill in the keyword to return a value from a function.

    Fill in the Blank Beginner
  64. 64

    Fill: List Sort

    Fill in the method to sort a list in place.

    Fill in the Blank Easy
  65. 65

    Fill: String Replace

    Fill in the method to replace a substring.

    Fill in the Blank Easy
  66. 66

    Fill: Except Clause

    Fill in the keyword to catch an exception.

    Fill in the Blank Easy
  67. 67

    Fill: Class Definition

    Fill in the keyword to define a class.

    Fill in the Blank Beginner
  68. 68

    Fill: From Import

    Fill in the keyword to import a specific name from a module.

    Fill in the Blank Easy
  69. 69

    Fill: List Insert

    Fill in the method to insert an element at a specific position.

    Fill in the Blank Easy
  70. 70

    Fill: Dict Items Iteration

    Fill in the method to iterate over key-value pairs.

    Fill in the Blank Medium
  71. 71

    Fill: String Join

    Fill in the method to join a list of strings with a separator.

    Fill in the Blank Medium
  72. 72

    Fill: Continue Keyword

    Fill in the keyword to skip the rest of the current iteration.

    Fill in the Blank Easy
  73. 73

    Fill: Open File for Reading

    Fill in the mode to open a file for reading.

    Fill in the Blank Easy
  74. 74

    Fill: JSON Dumps

    Fill in the function to convert a Python object to a JSON string.

    Fill in the Blank Easy
  75. 75

    Fill: Lambda Keyword

    Fill in the keyword to create an anonymous function.

    Fill in the Blank Medium
  76. 76

    Fill: Set Add

    Fill in the method to add an element to a set.

    Fill in the Blank Easy
  77. 77

    Fill: Raise Exception

    Fill in the keyword to manually trigger an exception.

    Fill in the Blank Medium
  78. 78

    Find Mistake: Missing Colon

    Find the syntax error in an if statement.

    Find the Mistake Beginner
  79. 79

    Find Mistake: Missing Indentation

    Find the indentation error in a function body.

    Find the Mistake Beginner
  80. 80

    Find Mistake: String + Int Concatenation

    Find the type error in string concatenation.

    Find the Mistake Easy
  81. 81

    Find Mistake: Off-by-One Index

    Find the IndexError in list access.

    Find the Mistake Easy
  82. 82

    Find Mistake: Missing Key Access

    Find the KeyError in dictionary access.

    Find the Mistake Easy
  83. 83

    Find Mistake: Using a Keyword as Variable

    Find the syntax error from using a reserved keyword.

    Find the Mistake Easy
  84. 84

    Find Mistake: Division by Zero

    Find the ZeroDivisionError in the code.

    Find the Mistake Easy
  85. 85

    Find Mistake: Modifying List During Iteration

    Find the logical error of removing items while iterating.

    Find the Mistake Medium
  86. 86

    Find Mistake: Mutable Default Argument

    Find the bug with a mutable default argument.

    Find the Mistake Medium
  87. 87

    Find Mistake: Comparing with =

    Find the error from using = instead of == for comparison.

    Find the Mistake Easy
  88. 88

    Find Mistake: Missing self Parameter

    Find the error in a method definition missing self.

    Find the Mistake Medium
  89. 89

    Find Mistake: File Not Closed

    Find the resource leak from not closing a file.

    Find the Mistake Medium
  90. 90

    Find Mistake: Infinite While Loop

    Find the bug causing an infinite loop.

    Find the Mistake Easy
  91. 91

    Find Mistake: Wrong Import Name

    Find the ImportError from a wrong module name.

    Find the Mistake Easy
  92. 92

    Find Mistake: Bare Except Clause

    Find the bad practice of a bare except that catches everything.

    Find the Mistake Medium
  93. 93

    Concept: Interpreted vs Compiled

    Check understanding of Python being an interpreted language.

    Concept Check Medium
  94. 94

    Concept: Dynamic Typing

    Check understanding of Python's dynamic type system.

    Concept Check Medium
  95. 95

    Concept: Class vs Instance

    Check understanding of the difference between a class and an instance.

    Concept Check Medium
  96. 96

    Concept: Generator vs List

    Check understanding of why generators are memory-efficient.

    Concept Check Medium
  97. 97

    Concept: Why Use Modules

    Check understanding of the purpose of modules.

    Concept Check Easy
  98. 98

    Concept: Why Catch Exceptions

    Check understanding of the purpose of exception handling.

    Concept Check Easy
  99. 99

    Concept: Inheritance Purpose

    Check understanding of why we use inheritance.

    Concept Check Medium
  100. 100

    Concept: Readability Principle

    Check understanding of Python's readability philosophy.

    Concept Check Easy
  101. 101

    Python Indentation

    How does Python define code blocks?

    Multiple Choice Beginner
  102. 102

    Python Variable Types

    How are variable types determined in Python?

    Multiple Choice Beginner
  103. 103

    List vs Tuple

    What is the key difference between a list and a tuple?

    Multiple Choice Beginner
  104. 104

    Dictionary Access

    What does {} use to store values?

    Multiple Choice Beginner
  105. 105

    String Formatting

    Which is the modern way to format strings in Python?

    Multiple Choice Easy
  106. 106

    List Comprehension

    What does [x*2 for x in range(3)] produce?

    Multiple Choice Easy
  107. 107

    Lambda Syntax

    What is a lambda in Python?

    Multiple Choice Easy
  108. 108

    Def vs Lambda

    When should you use def instead of lambda?

    Multiple Choice Easy
  109. 109

    Exception Handling

    How do you catch exceptions in Python?

    Multiple Choice Beginner
  110. 110

    With Statement

    What does the with statement do?

    Multiple Choice Medium
  111. 111

    Python None

    What is None in Python?

    Multiple Choice Beginner
  112. 112

    Mutable Default Argument

    Why is def f(x=[]) dangerous?

    Multiple Choice Medium
  113. 113

    Global vs Local

    How does Python determine variable scope?

    Multiple Choice Medium
  114. 114

    Slicing Syntax

    What does [1,2,3,4,5][1:4] return?

    Multiple Choice Easy
  115. 115

    Dict Get Method

    What does {}.get("missing") return?

    Multiple Choice Easy
  116. 116

    For Else Construct

    When does the else clause of a for loop run?

    Multiple Choice Medium
  117. 117

    Yield Keyword

    What does yield do in Python?

    Multiple Choice Medium
  118. 118

    Decorators Purpose

    What is a Python decorator?

    Multiple Choice Medium
  119. 119

    Class Init Method

    What is __init__ in a Python class?

    Multiple Choice Beginner
  120. 120

    Self Parameter

    What is self in a Python method?

    Multiple Choice Beginner
  121. 121

    Import Syntax

    What does from math import sqrt do?

    Multiple Choice Easy
  122. 122

    String Methods

    What does "hello".upper() return?

    Multiple Choice Beginner
  123. 123

    Range Function

    What does list(range(2, 10, 2)) produce?

    Multiple Choice Easy
  124. 124

    Type Hints Purpose

    What are Python type hints for?

    Multiple Choice Medium
  125. 125

    File Read Lines

    How do you read a file line by line safely?

    Multiple Choice Easy
  126. 126

    In Operator

    What does "a" in ["a", "b"] return?

    Multiple Choice Beginner
  127. 127

    Set Data Structure

    What is unique about a Python set?

    Multiple Choice Medium
  128. 128

    Zip Function

    What does list(zip([1,2], ["a","b"])) return?

    Multiple Choice Medium
  129. 129

    Map Function

    What does list(map(str, [1, 2, 3])) return?

    Multiple Choice Medium
  130. 130

    Enumerate Function

    What does list(enumerate(["a", "b"])) return?

    Multiple Choice Easy
  131. 131

    Python Print

    Predict the output.

    Predict Output Beginner
  132. 132

    Variable Assignment

    Predict the output.

    Predict Output Beginner
  133. 133

    List Operations

    Predict the output.

    Predict Output Beginner
  134. 134

    String Methods

    Predict the output.

    Predict Output Easy
  135. 135

    Dict Access

    Predict the output.

    Predict Output Easy
  136. 136

    If Elif Else

    Predict the output.

    Predict Output Easy
  137. 137

    For Loop Sum

    Predict the output.

    Predict Output Easy
  138. 138

    List Comprehension

    Predict the output.

    Predict Output Easy
  139. 139

    Tuple Unpacking

    Predict the output.

    Predict Output Easy
  140. 140

    String Format

    Predict the output.

    Predict Output Easy
  141. 141

    Boolean Logic

    Predict the output.

    Predict Output Easy
  142. 142

    List Slicing

    Predict the output.

    Predict Output Easy
  143. 143

    Dict Items

    Predict the output.

    Predict Output Medium
  144. 144

    Sorted Function

    Predict the output.

    Predict Output Medium
  145. 145

    String Split

    Predict the output.

    Predict Output Easy
  146. 146

    Max Function

    Predict the output.

    Predict Output Easy
  147. 147

    Abs Function

    Predict the output.

    Predict Output Beginner
  148. 148

    Type Function

    Predict the output.

    Predict Output Easy
  149. 149

    Set Operations

    Predict the output.

    Predict Output Medium
  150. 150

    String Join

    Predict the output.

    Predict Output Medium

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects