
Starting the Interpreter On most systems, the Python interpreter is installed at /usr/local/bin/python3.13
. If this directory is in your shell’s PATH, you can launch Python simply by typing:
bashpython3.13
However, the exact location may vary depending on how Python was installed. For instance, it might be located at /usr/local/python
. If you’re unsure, check with your system administrator or Python expert. On Windows, if you’ve installed Python via the Microsoft Store, the python3.13
command will be available in your command line. Alternatively, if the Python launcher (py.exe
) is installed, you can use:
bashpy
For more options, refer to the Excursus: Setting Environment Variables section. To exit the interpreter, you can:
- Press
Ctrl+D
(on Unix) orCtrl+Z
(on Windows) at the prompt. - Or simply type
quit()
. If supported by your system, the interpreter provides line-editing features such as command history, interactive editing, and auto-completion via the GNU Readline library. To check if command-line editing is available, pressCtrl+P
at the prompt: - If it beeps, the feature is available.
- If it displays
^P
, it’s not supported. Python behaves similarly to the Unix shell: - If run in interactive mode (with standard input connected to a terminal), it executes commands interactively.
- If run with a script file, it executes the script. You can also run Python commands directly from the terminal using:
bashpython -c "command"
For example:
bashpython -c "print('Hello, World!')"
To run a Python module as a script:
bashpython -m module_name
If you want to run a script and then enter interactive mode, use the -i
option:
bashpython -i script.py
A complete list of command-line options is available in the Command line and environment section.
Passing Arguments When you run a script, any arguments passed are stored in a list called sys.argv
, which is part of the sys
module. Example:
pythonimport sys
print(sys.argv)
- If no arguments are given,
sys.argv[0]
is an empty string. - If the script is run from standard input (
-
),sys.argv[0]
is set to'-'
. - If you use
-c
, it becomes'-c'
. - If you use
-m
, it’s set to the full module name. Arguments passed after-c
or-m
are not processed by Python itself—they’re left insys.argv
for your script or module to handle.
Interactive Mode When used interactively (i.e., commands are typed directly into the prompt), Python provides:
- A primary prompt:
>>>
- A secondary prompt:
...
(used for continuation lines) Here’s what it might look like:
bashpython3.13
Python 3.13 (default, April 4 2023, 09:25:04)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Example of a multi-line input:
python>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
...
Be careful not to fall off!
For more details, refer to the Interactive Mode section.
The Interpreter and Its Environment
Source Code Encoding By default, Python treats source files as UTF-8 encoded. This allows the use of characters from many languages in string literals, identifiers, and comments. However, most of the standard library uses only ASCII characters for identifiers—something you should follow for portable code. To use a different encoding, add a special comment at the top of your file:
python# -*- coding: encoding -*-
For example, for Windows-1252 encoding:
python# -*- coding: cp1252 -*-
If your file starts with a shebang (used to specify the script’s interpreter), place the encoding declaration on the second line, like this:
python#!/usr/bin/env python3
# -*- coding: cp1252 -*-
Make sure your text editor recognizes and properly supports the encoding you’re using.