Python Fundamentals · Python Runtime and Core Syntax
22 cards
Interpreter and Program Basics
-
Quick check
You start the interpreter by giving it the name of a Python file on the command line. What happens?
AIt opens an interactive session and leaves the file untouched until you import it
Interactive execution is what you get when standard input is a terminal and no file was supplied; a file name argument selects the other mode.
BIt reads that file and executes the script it contains
Right. A file name argument, like a file supplied as standard input, makes the interpreter read and execute the script in that file.
CIt passes each line of the file to the shell as a command
The interpreter executes Python statements from the file; the lines are not handed to the shell.
3 / 22
-
Quick check
Which command form finds a module by name and then runs that module's source as a script?
A`python -m module [arg] ...`, with the option placed just before the module name
Right. The `-m` option precedes the module name, and the interpreter locates that module and executes its source as a script.
B`python -c module [arg] ...`, since any bare name written after `-c` is treated as a module
`-c` treats the value that follows it as Python statements to execute, so a bare module name would be run as code, not located.
C`python module -m [arg] ...`, since the option follows the name
An interpreter option has to come before the name it applies to; written afterwards it no longer selects module execution.
5 / 22
-
Quick check
A developer must run `inspect.py` from disk and then immediately examine the values it produced, at a prompt. Which choice does both?
APass `-` as the script name so the file arrives through standard input
A script name of `-` only changes where the script comes from; the session still ends when the script ends.
BWrite `-m` after the script name so the file is located as a module
`-m` belongs before a module name and would not run an already named file and then open a prompt.
CPass `-i` before the script name so the prompt opens once it finishes
Right. `-i` placed before the script runs that script and then enters interactive mode, keeping its state available.
7 / 22
-
Keep your progress in the app
That’s 3 of 9 quick checks. In the app they stay answered, and every lesson remembers where you left off.
-
Quick check
How does Python hand a script its name and the arguments typed after it?
AAs string elements of the list `sys.argv`, reachable after `import sys`
Right. The script name and later arguments become a list of strings assigned to `argv` in the `sys` module.
BAs numbers in a tuple, converted by the interpreter first
Arguments stay strings and the container is a list; no numeric conversion happens on the way in.
CAs one global variable per argument, created automatically in the program's namespace
Arguments are elements of a single list, not separate names invented in the program's namespace.
9 / 22
-
Quick check
A module is started with `python -m tools.report weekly --quiet`. Which description of `sys.argv` is correct?
AElement zero is `-m`, and the interpreter consumes the two trailing values as its own options
The `-m` marker itself never becomes element zero, and values after the module name are left alone by option processing.
BElement zero is the located module's full name, and both trailing values stay available to the module
Right. With `-m`, element zero is the full name of the located module, and later values remain in the list for that module to handle.
CElement zero is an empty string, so only `weekly` reaches the module as an argument
An empty element zero describes the case where no script and no arguments were given at all.
12 / 22
-
Quick check
While typing a multi-line `if` interactively, which prompt appears, and what ends the command?
A`>>>` appears for each extra line, and the command ends when the indentation returns to the margin
Three greater-than signs are the primary prompt, shown when the interpreter is ready for a brand-new command.
B`---` appears for each extra line, and the command ends when a closing keyword is typed
Three hyphens are not one of the interpreter's prompts, and no closing keyword terminates the construct.
C`...` appears for each extra line, and the command ends when a blank line is entered
Right. Continuation lines use the secondary prompt of three dots, and a blank line completes a multi-line command entered interactively.
15 / 22
-
Quick check
At the prompt, pressing Left arrow prints `^[[D` instead of moving the cursor. The session must continue. What does this tell you?
AInteractive mode failed to start, so the interpreter must be restarted
The visible prompt proves interactive mode is already running, so nothing about the session needs restarting.
BThe file's source encoding is wrong, so a coding declaration has to be typed in
Source encoding governs how a file's bytes are read, which has nothing to do with arrow-key support at a prompt.
CCommand-line editing is unavailable here, so only Backspace edits the current line
Right. A printed escape sequence means the environment offers no command-line editing, leaving Backspace as the way to remove characters from the current line.
17 / 22
-
Quick check
A script begins with a Unix shebang line and needs the `cp1252` codec. Where does the encoding declaration belong?
AOn the second line, immediately after the shebang
Right. A shebang on line one is the documented exception, so the encoding comment moves to the second line.
BOn the last line, after every Python statement
An encoding declaration is read at the top of the file; placed after the statements it would come far too late.
CInside the program's first string literal
The declaration is a special comment line, not text stored inside a string literal.
20 / 22
-
Quick check
A source file carries no encoding declaration at all. How is it read?
AAs ASCII, the encoding the standard library uses for its identifiers
ASCII identifiers are a portability convention followed by the standard library, not the encoding applied to source files.
BAs UTF-8, the default encoding for Python source files
Right. Python treats source files as UTF-8 by default; any other supported codec has to be declared explicitly.
CAs the codec the editor last saved with, which Python detects on opening
Python applies the default encoding or an explicit declaration; it does not adopt whatever the editor happened to use.
22 / 22
-
9 quick checks · then the test
In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.
The whole course, on your phone
Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.