Python Reinforcement Practice

Here are a set of problems designed to help you reinforce and retain some useful Python knowledge. If you are an Anki or Quizlet fan, consider adding some of these questions into a deck. 😀


Who created Python and in what year?Guido van Rossum, who started work on the language in 1989 but first released it to the world in 1991
How long was GvR’s tenure as the BDFL?From the language’s inception to July, 2018
Python doesn’t use curly braces; instead, structure is shown using indentation, with something known as the _________________ rule.Offside
How do you write a line of text to standard output?
print(sometext)
How do you access the third command line argument of a script, run on the command line?sys.argv[3]
How do you iterate through the lines of standard input?
for line in sys.stdin:
    # do something with line
What is the difference between 21 / 8 and 21 // 8?The former does true division, producing 2.625; the later does floor division, producing 2
Why do we not have to worry about the expression range(1, 10000000) taking up a lot of memory?A range object is not a fully-realized sequence; it is a small, compact, tiny object that produces objects only on demand
How do you write the (conditional) expression that evaluates to y if x is truthy and to z if x is falsy?y if x else z
How do you write an assignment statment to swap the bindings of x and y?
x, y = y, x
How do you write the Python value whose square is -1?1j
How do you get the type of a Python expression at runtime?type(e)
Why does Python not have primitive and reference types?The distinction is not necessary; the language philosophy values simplicity
If everything in Python is an object, does that mean even integers and booleans get allocated on the heap? What allows them to not have to be heap-allocated?Because they are immutable, there is no difference between allocating on the heap (i.e., accessing them through references) and just allocating their values directly
Does Python have distinct types for integers and floating point numbers?Yes, int and float
Does Python have distinct types for small integers and unbounded integers?Nope, the basic type int is unbounded
What are the types of the expressions [x**2 for x in range(10)] and (x**2 for x in range(10))?list; generator
What is the difference between is and ==?The former compares the identity of two objects; the latter compares their values
How can you tell whether string s is a substring of string t?s in t
How do you do string interpolation in Python?
f"text {expression} more text"
Why is 2 and 3 equal to 3, but 2 & 3 equal to 2?The former is short-circuit logical conjunction; since 2 is truthy, 3 is produced. The latter is binary AND, 010 /\ 011 ⇒ 010.
Name all the falsy values in Python.
How do you write a one-element tuple?(x, )
How are tuples compared with <, <=, >, and >=? Are lists compared the same way?
If {3, 5} is a legal set expression, why is {} not a legal set expression?Python uses {} for the empty dict
How do you write the mathematical set operators ∈, ∉, ⋃, ⋂, –, ⊖, ⊂, ⊆, ⊃, ⊇ in Python?
MathPythonMeaning
inelement of
not innot element of
|union
&intersection
-difference
^symmetric difference
<=subset
<proper subset
>=superset
>proper superset
How do you “declare” a local variable in Python?
What is the difference between p.x and p[x]?
If x == 'y' then what is { x: 3, 'x': 5 } ?
Does Python have an analog to JavaScript’s spread operator? If so, give an example of its use.
You might often see the expression a[:], for some list a. What does this expression do, exactly?
Explain why a[::-1] is the idiom for a reversed list.
How does one best create an array of size 100 in which every element is 0? (Do not write a loop.)[0] * 100
What is the difference between a+b and [*a,*b] where a and b are lists?
a.append(x) mutates a. How do you do a non-mutating append?[*a, x]
How do you add an element to the front of a list, in a mutating fashion? Is this efficient? If not, what should you do instead?
Write an IIFE that applies, to the argument 100, the anonymous arrow function that squares its argument.(lambda x: x ** 2)(100)
What important limitations does Python put on anonymous functions?
In the function definition def f(x, y): return [x, y], what do we call x and y?Parameters
In the function call f(y, z), what do we call y and z?Arguments
What are kwargs?
Can positional arguments ever appear after keyword arguments? Why or why not?
Can a function definition have multiple “args’ parameters? Why or why not?
Does Python allow assignments with array patterns on the left-hand side?
Does Python allow assignments with dictionary patterns on the left-hand side?
How do you force a function to take its arguments positionally (Python 3.8 and above)?
How do you force a function to take its arguments as kwargs?
Why should you be scared of using default arguments that are lists? Do you have to worry about this in JavaScript?
In sense are Python anonymous functions limited compared to those of JavaScript?Python anonymous functions are limited to bodies that are single expression
Python has global scope and function scope, but does it have block scope?Nope
What is the difference between global and nonlocal?
When do you use nonlocal and when do you use global?
Does Python have an analog of JavaScript’s temporal dead zone? If so, how does it work? If not, why is such a concept incoherent in Python?
Write a function that takes in a number and returns a function that adds that number to its argument. Is the function you wrote higher-order? Why or why not? Does that function illustrate a closure? Why or why not? Does that function illustrate currying? Why or why not?
Why are map and filter rarely used in Python?List comprehensions are favored by the community over map and filter
What is a list comprehension for all pairs (2-tuples) of integers where the first element is in the range 1..5 inclusive and the second is in the range 1..3 inclusive?
What is a list comprehension for a 10x10 multiplication table?
What is the first line of that block at the end of a module that allows you to “run” the module from the command line?
if __name__ == '__main__':
What is the difference between import M and from M import e?The former makes everything in module M avaiable to the current module, but they must be qualified (e.g., M.e). The latter makes only e available, without qualification (i.e., you just say e).
Instead of throw, Python uses ________________.raise
The things inside of a class are called the ________________ of the class.Attributes
How do you create an instance of a class?Classname(args)
What is the difference between a function and a method?A method is a function declared in a class
What is the first parameter of a method for?For instance methods, it’s the receiver. For class methods it’s the class object itself. For static methods, whatever you want.
Suppose class Circle has a method area and c is an instance of Circle. The invocation c.area() is actually sugar for what expression?Circle.area(c)
What is the method __init__ used for?Construction
What is the methods __str__ used for?
Is it possible to make an attribute of a class inaccessible to methods outside the class (like Java’s private)?
What is the syntax for declaring a class that is a subtype of another class?Just use a colon, for example class Dog: Animal
What is a class attribute?
How are class attributes defined in Python?
How do you allow the operators + and - to apply to instances of a class you write yourself?Define __add__ and __sub__
Which two methods are required for an object to be an iterator? What behavior must these methods have?
Iterators can be used in what Python statement?for
Rather than writing our own iterator object, we would write a ____________ instead.Generator
JavaScript defines generators with the syntax function*. How does Python define generators?Just have a yield expression in it somewhere.
Given def f(): yield 1; yield 2; yield 3, what is wrong with writing for i in f: print(i)? What should we write instead?
What is a generator expression? Give an example.
What does the @property decorator do?
What does the @classmethod decorator do?
What is PEP-8?The Python Style Guide
What is PEP-20?The Zen of Python