Python Programming
0Loading crowd target
0Loading crowd target
8correct to beat the crowd
Test your Python programming knowledge with our comprehensive free 96-question trivia quiz with answers. Whether you're a beginner learning the basics or an intermediate developer, this quiz covers essential Python concepts, syntax, and best practices. See how well you really know Python!
30 of 96 questions with answers and explanations. Play the quiz
Q 01Which keyword chains a second test onto an `if` statement in Python?
`elif`
`elif` is short for 'else if', and a ladder of elif branches was Python's stand-in for the switch statement of other languages until `match` arrived in version 3.10.
Q 02Which keyword ends a function call and hands a value back to the caller?
`return`
`return` ends the function and passes its value to the caller; a function that falls off the end without one returns None, and `exit` is not a keyword at all.
Q 03Van Rossum began Python as a hobby project to fill which holiday break in 1989?
Christmas
Christmas: with his office closed for the week, van Rossum wanted a 'hobby' project to keep him occupied, and he posted the first public code just over a year later.
Q 04Which Python Enhancement Proposal is the language's official style guide?
PEP 8
PEP 8 rests on van Rossum's insight that code is read much more often than it is written, so it fixes details such as a 79-character line limit and snake_case names to make everyone's code look alike.
Q 05How does Python define code blocks, unlike languages that use curly braces?
Indentation
Indentation marks out the blocks, and the interpreter is strict about it: Python 3 refuses to run a file whose tabs and spaces are mixed inconsistently, raising TabError.
Q 06What is the standard package installer for Python called?
pip
pip has shipped inside Python's own installers since version 3.4, so the tool that fetches packages rarely has to be fetched itself; npm, gem and apt do the same job for JavaScript, Ruby and Debian.
Q 07Which Python data type is mutable?
List
List: `nums[0] = 9` changes a list in place, while the same assignment on a tuple or a string raises TypeError, because those types can never be altered once created.
Q 08In CPython, what does the Global Interpreter Lock (GIL) actually do?
Lets only one thread run Python code at a time
The GIL lets only one thread run Python code at a time, which is why CPU-heavy programs spread work across separate processes instead; Python 3.13 added an experimental build that can switch the lock off.
Q 09Which OOP principle bundles data and the methods that operate on it into a single class?
Encapsulation
Encapsulation bundles attributes and methods into one class and hides the object's internal state; Python signals 'private' only by convention, with a leading underscore.
Q 10In Python, `bool` is a subclass of which other built-in type?
`int`
`int`: True and False are just the integers 1 and 0 in disguise, so `True + True` evaluates to 2 and adding up a run of comparisons counts how many were true.
Q 11Which 'fully loaded' Python web framework handles authentication, admin and RSS out of the box?
Django
Django ships so much because it was built in a newsroom, where developers had only hours to turn an idea into a live site; hence its tagline, 'the web framework for perfectionists with deadlines'.
Q 12What character is used to denote a single-line comment in Python?
# (hash sign)
The hash sign, #, makes the interpreter skip the rest of the line, with one exception: a comment such as `# -*- coding: latin-1 -*-` on the first or second line is read to learn the file's encoding.
Q 13Which of these aphorisms really is a line of 'The Zen of Python'?
Readability counts.
Q 21Which keyword is used in Python to create a generator function?
yield
`yield` hands back one value and freezes the function where it stands, resuming on the next request, so a generator can walk a huge or even endless sequence without holding it all in memory.
Q 22What is `super()` used for in Python?
Call a parent class's methods
`super()` lets a subclass call a parent class's methods without naming the parent, so swapping the base class later means editing one line rather than every call.
Q 23What does a Python decorator do?
Wraps a function to extend or alter its behaviour
Readability counts is one of 19 aphorisms Tim Peters posted in 1999; Perl's motto about more than one way is the line the Zen contradicts, asking for 'one-- and preferably only one --obvious way to do it'.
Q 14Which library supplies the fast multi-dimensional arrays that most of Python's scientific stack builds on?
NumPy
NumPy's arrays beat plain lists because their element-by-element loops run in pre-compiled C, and pandas and Matplotlib both lean on them rather than reinventing the array.
Q 15Why was Python 2's retirement postponed from 2015 to 2020?
Many users had not moved to Python 3
Many users had not moved to Python 3 by 2014, so the core team gave Python 2 five more years; support ended on 1 January 2020, with a last release, 2.7.18, bundling the final fixes that April.
Q 16Which keyword begins a function definition in Python?
`def`
`def` is an executable statement rather than a declaration: Python runs it when reached and binds the name to a new function object, so a function can be defined inside an `if` or inside another function.
Q 17What does the Python expression `5 + 2 * 3` evaluate to?
11
11, because * binds tighter than +, so Python works out 2 * 3 first; wrap the sum in parentheses, `(5 + 2) * 3`, and the same numbers give 21.
Q 18Which of these is a legal variable name in Python?
`total_2`
`total_2` is legal because digits are allowed anywhere except the first position; `2nd_total` starts with one, `my-total` reads as a subtraction, and `while` is a reserved keyword.
Q 19Which `open()` mode opens a file for writing and wipes any existing contents first?
'w'
'w' truncates an existing file and creates one if none exists, so opening the wrong path silently destroys data; 'a' also writes but appends, and 'x' refuses to touch a file that is already there.
Q 20What does a Python class's `__init__` method do?
Sets up a new object's attributes
`__init__` sets up a new object's attributes, but it does not create the object: `__new__` has already built the instance by the time `__init__` is called to fill it in.
A decorator wraps a function to extend or alter its behaviour: writing `@timer` above a function is shorthand for `func = timer(func)`, which is how the built-in staticmethod and classmethod are applied.
Q 24What does Python do at runtime if a function annotated `x: int` gets a string?
Ignores the hint and runs
Python ignores the hint and runs the call: annotations are stored on the function but never enforced, so catching the mismatch is left to outside checkers and editors.
Q 25Which built-in type lets a set be stored as an element of another set?
`frozenset`
`frozenset` is hashable because it can never change after creation, so Python lets it sit inside another set; put an ordinary set there and you get TypeError for an unhashable type.
Q 26What does `'hello'[-1]` evaluate to in Python?
`'o'`
`'o'`: a negative index counts back from the end, so -1 is always the last item and -2 the one before it; only an index beyond either end raises IndexError.
Q 27What does Python's `enumerate()` do to an iterable?
Pairs each item with a running count
`enumerate()` pairs each item with a running count, starting at 0 unless told otherwise, so a loop gets position and value together without keeping a counter of its own.
Q 28In Python's `asyncio` library, what is the role of an 'event loop'?
Runs tasks, callbacks and network I/O
The event loop runs tasks, callbacks and network I/O on a single thread, switching to another task whenever one pauses at an await, which is how one thread can serve many connections at once.
Q 29What is the purpose of `pass` in Python?
To stand in where a statement is required
`pass` does nothing at all: its only job is to stand in where a statement is required, so an empty function, class or loop body still parses while you work out the rest.
Q 30Which operator is used for exponentiation in Python?
** (double asterisk)
The double asterisk, **, raises to a power, so `2 ** 3` is 8; the caret is a trap, because in Python `^` means bitwise XOR and `2 ^ 3` quietly gives 1.