Python flashcards: first steps and loops
Flashcards for the first Python lessons, on how CPython runs code, installing, the shell and scripts, and for loops with range, enumerate and zip.
16 cards. Read the question, answer it in your head, then open the card to check.
-
What does CPython do with a .py file before it runs any line of it?
It compiles the whole file to bytecode for its virtual machine. A syntax error anywhere in the file stops it at this step, before anything runs.
From the lesson What Python is and how it runs your code
-
Where does CPython keep the bytecode of a module you import?
In a __pycache__ folder next to the module, in a file such as greeting.cpython-314.pyc. The script you run directly is not cached.
From the lesson What Python is and how it runs your code
-
How long is a Python feature release supported?
About two years of bugfix releases, then security fixes only, until about five years after its first release.
From the lesson What Python is and how it runs your code
-
What is Pyodide?
CPython compiled to WebAssembly, so Python can run inside a web browser, on your own device.
From the lesson What Python is and how it runs your code
-
How do you check which Python version a terminal runs?
python3 --version on macOS and Linux; py --version on Windows.
From the lesson Install Python 3.14 on Windows, macOS and Linux
-
Why should you never remove the system Python on Linux?
Parts of the system depend on it. Install the version you need alongside it instead.
From the lesson Install Python 3.14 on Windows, macOS and Linux
-
How do you find out which interpreter a command actually runs?
Ask the interpreter itself with python3 -c "import sys; print(sys.executable)", which prints the path of the one that answered. On macOS and Linux, which -a python3 (on Windows, where python) lists every match on your PATH, in the order the terminal tries them.
From the lesson Install Python 3.14 on Windows, macOS and Linux
-
What does _ hold in the interactive shell?
The last value the shell printed. It exists only in the shell, not in scripts.
From the lesson The Python REPL, scripts and the -m switch
-
Why is python3 -m pip safer than a bare pip command?
It runs pip for exactly that interpreter, so packages are installed for the Python you meant, even when another pip comes first on your PATH.
From the lesson The Python REPL, scripts and the -m switch
-
What type are the items of sys.argv?
Strings. sys.argv[0] is the script name; convert an argument with int() before doing arithmetic with it.
From the lesson The Python REPL, scripts and the -m switch
-
What does a for loop over a dictionary give on each pass?
Its keys, in the order they were added. Loop over .items() for each key with its value, or over .values() for the values alone.
From the lesson for loops with range, enumerate and zip
-
Which numbers does range(1, 5) give?
1, 2, 3 and 4. A range includes its start and never its stop value, so range(1, 11) is the way to write 1 to 10.
From the lesson for loops with range, enumerate and zip
-
How do you number the items of a list from 1 in a for loop?
for place, item in enumerate(items, start=1). enumerate() gives the number and the item together, so the loop needs no index arithmetic.
From the lesson for loops with range, enumerate and zip
-
What does zip() do when the lists have different lengths?
It stops at the shortest, without a word, and the rest is dropped. zip(..., strict=True) raises ValueError instead, once it reaches the end.
From the lesson for loops with range, enumerate and zip
-
Why does removing items from a list inside a for loop over that list skip items?
The loop reads by position. After a removal the next item moves into the position the loop has just read. Loop over a copy, kept.copy(), or build a new list.
From the lesson for loops with range, enumerate and zip
-
What does the loop variable hold after a for loop?
The last item it was given. A loop that never runs, over an empty list, never sets it, and using it then raises NameError.
From the lesson for loops with range, enumerate and zip