Repository for Python Initiate Matters
Python is a popular interpreted language that has found application in many areas of computing. It is a relatively simple language, and is often a first programming language for many. We like to assist in this learning process.
Introduction
Interactive Python
Simple Types
Expressions
Control Structures
Functions
Sequences
Simple Scripts
Virtual Environments
Your PATH
PATH environment variable is often the cause of problems for those not overly familiar with the command-line. Learning how to manage the PATH environment variable, is a valuable skill. First, we shall look at environment variables in general, before we tackle PATH. Unless you prefer the TL;DR.Requirements
Although not much is really required (no entrance exam or competency test exists), you will simply have a better time, the more familiar you are with the following topics:
Terminal Emulator — All Unix-like operating systems will have terminal emulators, even if the emulation is in the system console (like Linux). Graphical installations will have a default terminal emulator, but many third party emulators are available. Even Windows users have options, including Windows Terminal.
Command-Line Shell — Unix-like command-line shells share common characteristics, which is not exactly replicated in the legacy Command Prompt or the portable PowerShell 7 (also known as PowerShell Core, and not to be confused with legacy ‘Windows PowerShell’). Nevertheless, all support commands that can be performed without a mouse, including managing files and directories, editing and running Python programs (scripts).
Code Editor — Although technically any text editor will do, an editor that has Python support would be better. If you have no particular preference, we suggest Visual Studio Code (code), which we shall call VSCode from now on. It has nothing in common with the Visual Studio IDE, apart from the name and the vendor.
Presumably you also realise that access to a Python interpreter would be somewhat useful. You do not necessarily have to install Python to learn Python — several online options allow for Python code input, and showing results of computations; requiring only a browser.
Little Background
Python is a high-level, interpreted programming language that was created by Guido van Rossum and released in 1991. The design philosophy of Python emphasizes code readability and simplicity, making it a popular choice for beginners and experts alike.
Python is currently managed by the Python Software Foundation, as open source software, under a permissive license, compatible with the GPL (GNU General Public License).
Ongoing Python development are driven by PEPs (Python Enhancement Proposals). Many of Python's current features and syntax (but not all) are documented in PEPs. Some PEPs are whimsical (though some might object to the adjective).
One PEP even suggests a coding style, which is taken very seriously by some. The latter is the cause for the term pythonista as somebody who writes not only pythonic (idiomatic) code, but adheres to PEP-8 and advocates it. Tools exist to check that your code adhere to PEP-8, like flake8, pylint and the must newer ‘written in Rust’ ruff, which incidentally, can also format your code, and has a Ruff VSCode Extension.
Python Versions
Python 3.0, released in 2008, represented a major change from Python 2.x, which introduced several incompatibilities. Python 2 is no longer supported, but can still be found ‘in the wild’. A consequence of this, is that on some Linux distributions, you may get Python 2, if you run python; for Python 3, you are supposed to run python3.
This repository focuses on Python 3.14, but do mention Python earlier versions down to 3.10. All scripts require at least version 3.10; those that don't will mention it. But all will run on 3.14.
Python Features
Here are a few topics and features that put Python in context and distinguish it from other programming languages.
INTERPRETER — Python is an interpreted language, that supports multi-paradigm program development. This means if you like OOP (object-oriented programming), Python will do OOP. Or, if you prefer a more imperative approach, Python will oblige.
CLASSES — All types (type in syntax), are class types. Values are thus often called objects, and have attributes, methods, and in some cases, properties. All objects are stored ‘somewhere in memory’ — we really don not care where, as Python manages memory for us.
OBJECTS — Objects are managed only by a reference. We can never move an object's memory around, only a reference to the object (much like a pointer in C/C++). This is an invariant (a fact that never changes and has no exceptions).
DUCK TYPING — Python employs duck typing, which is a form of dynamic (runtime) typing. In practice, this means that you do not have to specify types for variables or parameters. For example, you can pass any type of argument to a function, as long as the argument's type exhibits the expected behaviour.
KEYWORD ARGUMENTS — Python's term for named parameters is keyword arguments. When passing arguments to a function, the caller has the option to include the parameter name. An argument is an expression that initialises a function parameter.
MEMORY — Python occasionally and automatically, ‘collects garbage’ memory that is not in use any more. To do this, it performs reference counting, which incurs overhead. This behaviour is similar to other memory-managed languages like Java and C#, in contrast to C and C++ which does not automatically manage dynamic memory.
NOT COMPILED — Python scripts are tokenized into a more compact form, which is a high-level type of compilation. This happens before the code is interpreted (executed). Under certain circumstance, Python may store this ‘compiled’ code in a __pycache__ subdirectory, when interpreting scripts.
The ‘compiled’ script name will be name.pyc inside the cache directory. These files can be safely deleted. Alternatively, you can run them like any Python script or even distribute them to clients instead of the name.py files. It will save some space, and slightly faster loading/execution. However, this is not a security measure.
STATEMENTS — Statements are terminated by a newline, also called a linefeed, (character NL/LF). There are situations where a statement may be continued onto a physical new line (statements spanning lines).
Python is one of a few languages that allow ‘useless expressions’ as legal statements. This is so that Python REPLs can automatically print the results of such expressions. In a script, such an expression statement will have no effect (i.e., ‘useless’).
INDENTATION — Leading whitespace is significant in Python. Incorrect indentation can result in syntax errors. You should pay attention when this comes up. The main reason for this syntactic choice is the Python code appears ‘cleaner’ than languages that rely on delimiters (free-format languages).
COMPREHENSIONS — Python supports various comprehensions, which provide a concise way to create lists, tuples, or dictionaries, using a single line of code. They help make your code more readable and often require fewer lines than traditional loops.
GENERATORS — Generators are a special type of iterator that allows you to create an iterable object using the yield statement. They provide an efficient way to work with large datasets or infinite sequences by only generating the next item in the sequence when it's needed.
CONTEXT MANAGERS — Python has built-in support for creating and using context managers using the with statement. Context managers simplify the process of acquiring and releasing resources, such as file handles or network connections, by automatically closing them for you.
DECORATORS — Decorator is feature in Python that allow you to modify or extend the behaviour of functions or classes. They are essentially functions that take another function or class as input and return a new function or class with the modified behaviour.
FIRST-CLASS FUNCTIONS — Python supports first-class functions, meaning that functions can be assigned to variables, passed as arguments to other functions, and returned as values from other functions. This enables powerful functional programming techniques, such as higher-order functions and closures.
MULTIPLE INHERITANCE — Python supports multiple inheritance, allowing a class to inherit from multiple parent/base/super classes. This feature enables more complex and flexible class hierarchies, but it can also lead to potential issues such as the diamond problem if not used carefully.
EXCEPTION HANDLING — Python provides a comprehensive exception handling system, allowing you to catch, handle, and raise exceptions. This system enables you to handle errors gracefully and maintain the stability of your program.
MODULES AND PACKAGES — Python has a robust system for organizing code into modules and packages. This feature makes it easy to reuse code, manage dependencies, and distribute libraries.
BATTERIES INCLUDED — Python has a rich standard library, often referred to as ‘batteries included’. The standard library provides a wide range of functionality, such as file I/O, regular expressions, and web services, allowing you to perform many common tasks without needing to install additional packages.
STYLE GUIDE — Python has a widely accepted style guide called PEP 8 that provides recommendations for writing readable and consistent code. Following PEP 8 guidelines helps maintain a uniform coding style throughout the Python community, making it easier to read and understand other people's code — pythonic code written by pythonistas.