Course: CSCI 1250

Reference: docs.python.org

Key Features

  • Extensive standard library
  • Strong community for a wide range of applications
    • Web development, data science, machine learning, etc.
  • Interpreted language → no need to compile code

Type System

  • Dynamically typed → no need to declare variables types
  • PEP 484 added support for type hints, with editor LSPs (such as Pylance) being able to use the type hints:
def say_hello(name: str) -> str:
    return f"Hello {name}"

How Python Works

Interpreted Language

  • Since Python is an interpreted language, meaning the source code is not compiled into machine code.
  • When a Python program is ran, the interpreter parses the source code into bytecode. The bytecode is a representation of the source code, but it’s not machine code.
    • The bytecode is then executed by the Python virtual machine (PVM)
flowchart TB
    A([Source Code])
    B{{Interpreter}}
    C([Bytecode])
    D{{Bytecode Interpreter}}
    E([Execution])

    A --> B --> C
    C --> D --> E

Execution

  • Python’s execution is top-to-bottom → statements are executed sequentially.
  • Function declarations are processed, but not executed until a function is called.
  • Python does not support hoisting—you have to define functions before they’re called, or a NameError will be raised.
say_hello() # NameError raised here
 
def say_hello() -> None:
    print("Hello, World!")

print()

  • Print is a function that outputs text to the terminal
  • You can also pass a few keyword arguments, such as end into the function.
def print(
    *values: object,
    sep: str | None = " ",
    end: str | None = "\n",
    file: SupportsWrite[str] | None = None,
    flush: Literal[False] = False
) -> None

Downloading Python

  • Python can be downloaded through various methods. On Linux machines, you can install it through the provided package manager. (Although Python is usually pre-installed on Linux machines)
    • IE: sudo pacman -Sy python-pip for Arch.
  • If you’re using Windows, you can either install Python through the Microsoft Store, or by using the GUI on the [Python website].

Make sure to add Python to your PATH if you’re using Windows.