basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    Chapter 1PythonBasics

    Running Python: the interactive mode and your own .py file

    There are three ways to run Python: interactively — type a line, see the answer right away; as a .py file — the code is saved and runs as many times as you like; as a module by name — python -m. Work out in the terminal how much an uncompressed video frame weighs, and save the calculation as your first script.

    New hereRunning pythonModules and python -mA .py file
    You needThe terminal and cd

    What you’ll get

    If Python isn’t installed yet, install it per the first step of Files to your phone. From here on, type python; on Ubuntu and macOS, python3.

    Type python in the terminal without arguments. Python prints its version and waits for code after >>>. Work out how much an uncompressed Full HD frame weighs: 1920 × 1080 pixels at 3 bytes each, in megabytes.

    >>> 1920 * 1080 * 3 / 1024 / 1024
    5.9326171875
    >>> 2 ** 64
    18446744073709551616
    >>> exit()

    The answer appears right away; no print needed. ** is a power: 2 ** 64 is how many values a 64-bit number has, and Python computes it in full, without rounding.

    Save the same calculation to a file called frame.py. Any editor that shows plain text will do: VS Code, Notepad++, Notepad.

    # How much an uncompressed frame weighs: width × height × 3 bytes per pixel
    print("Full HD:", 1920 * 1080 * 3 / 1024 / 1024, "MB")
    print("4K:", 3840 * 2160 * 3 / 1024 / 1024, "MB")
    cd D:\scripts
    python frame.py
    # Full HD: 5.9326171875 MB
    # 4K: 23.73046875 MB

    One second of 4K video at 60 frames is about 1.4 GB uncompressed, which is why video is stored compressed.

    What will it print?
    # two.py — run it: python two.py
    2 + 2
    print(3 * 3)
    What will it print?
    Answer:
    9

    In interactive mode Python prints the value of every line itself. A file it runs silently: 2 + 2 is computed and forgotten, and only what’s passed to print reaches the screen.

    How it works: the interpreter reads and executes

    python is an interpreter program: it reads Python code and executes it right away; there’s no separate build into an .exe. The modes differ in where it takes the code from and what it prints on its own.

    Interactive mode A .py file
    Start python python frame.py
    Where the code comes from line by line from the keyboard the whole file, top to bottom
    What it prints on its own the value of every expression nothing — only what’s in print
    The code after exit gone stays in the file
    What for check a piece, calculate, look inside a module anything you run more than once

    The interactive mode is called a REPL: read a line, eval it, print the result, loop again. A line ending in a colon opens a block: the prompt changes to ..., and an empty line closes the block. Exit with exit(), or Ctrl+Z and Enter on Windows, Ctrl+D on Linux and macOS; since Python 3.13 plain exit without parentheses is enough.

    Python executes a file line by line, top to bottom. # and everything after it to the end of the line is a comment, which Python skips. print prints everything passed to it, separated by commas, with a space between the parts: "Full HD:", the number and "MB" are three parts of one output line. Text in quotes is a string; without quotes Python reads it as code; how values differ is covered in the Variables and types chapter.

    python looks for the file name in the terminal’s current folder — the one in the PS D:\scripts> prompt. From another folder, use the full path: python D:\scripts\frame.py.

    A module by name: python -m

    A module is a file of Python code; the standard library is the modules that come with Python. -m runs a module by name without knowing where its file lives:

    python -m calendar 2026 10
        October 2026
    Mo Tu We Th Fr Sa Su
              1  2  3  4
     5  6  7  8  9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31

    The same goes for python -m http.server from Files to your phone, python -m pip and python -m venv — packages come in the “Modules and packages” chapter. Python looks for a module in the current folder first, then in the standard library. So your own calendar.py next to you overrides the real module: python -m calendar will run yours. Don’t name your files after standard library modules.

    There’s also a fourth way in — code right in the argument, no file and no >>>: python -c "print(7 * 24 * 60 * 60)". That’s how other scripts and the command line call Python when one line is all that’s needed.

    Take it further

    Seconds in a week and in a year — a two-line file

    Each line is a print with a label and a product: days × hours × minutes × seconds.

    # seconds.py
    print("Week:", 7 * 24 * 60 * 60, "s")
    print("Year:", 365 * 24 * 60 * 60, "s")
    # Week: 604800 s
    # Year: 31536000 s
    Run frame.py from your home folder without changing to the script’s folder

    Instead of a file name, python accepts the full path to it.

    cd ~
    python D:\scripts\frame.py
    A calendar for all of 2027 in one command

    The same calendar module, just without the month.

    python -m calendar 2027

    If it doesn’t work

    SyntaxError: invalid syntax in response to python frame.py

    Cause
    The command was typed into interactive Python, after >>>: it expects Python code, not terminal commands.
    Fix
    Exit with exit() and run it in the terminal, where the prompt is PS D:\scripts> or $.

    can't open file '…\frame.py': [Errno 2] No such file or directory

    Cause
    The terminal is open in another folder. Or the file was saved as frame.py.txt: Notepad adds .txt, and Explorer hides extensions by default.
    Fix
    cd into the script’s folder; dir (ls on Linux and macOS) shows the real name. In Notepad, choose “Save as type: All files”.

    SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

    Cause
    An example from Python 2: there you wrote print "…"; in Python 3 print is a function and needs parentheses.
    Fix
    Add the parentheses: print("…"). print without them is a sign the material predates 2020, when Python 2 support ended.

    You type python, and the Microsoft Store opens

    Cause
    Python isn’t installed, and Windows substitutes a stub from the store.
    Fix
    Install the Python install manager per the first step of Files to your phone and open a new terminal window.

    Also in this chapter