Introduction to Python

Python is a versatile programming language widely used in web development, scientific computing, machine learning, data science, automation, APIs, and more. It's often chosen for its quick iterability, ease of use for both reading and writing, and convenience as a glue language.

Understanding Versions

Python's release cycle is divided into major, minor, and micro (patch) versions.
Major versions are planned very far in advance for significant, incompatible changes.
Minor versions incorporate new features and occur anually.
Micro versions address bugs and release approximately every 2 months.

Alpha, Beta, Release Candidate (RC), and Final (stable) are sequential stages within the development cycle that indicate how close a version is to being considered ready for production environments. These stages and versioning mechanisms are used in many forms of software.

Installation

Windows is the only major operating system that requires manually downloading and installing Python from https://www.python.org/downloads, where it's recommended to install the latest stable version unless there is a specific requirement for an older build. Other platforms come preinstalled with a version that's recent enough for most use cases, but not necessarily the very latest.

Python has a vast standard library, but an even larger collection of external libraries at https://pypi.org. Relying on the standard library where feasible can reduce the chances of encountering dependency conflicts because third party packages are not guaranteed to be compatible with the latest Python release, which can sometimes create a dependency hell where a project's libraries require differing versions of one another and/or Python, forming a combination that cannot function together.

Usage

After Python has been installed, the simplest way to interact with it is by launching the REPL (also called an interactive interpreter), which is an environment that allows for entering code one line at a time, and then immediately seeing the results of that code. On Windows systems, search for Python and launch the shortcut. For any Unix variants like macOS, Linux, or the BSDs, launch a command-line interface (CLI) and run python. Some systems may require specifying the major version and would be launched as python3 for example.

Alternatively, code can be saved to a file, and then that file can be executed by the Python interpreter. The code itself can be written with any software capable of modifying text files, but an IDE is recommended for frequent programming.

To execute files with Python, the interpreter must be explicitly called either from a terminal or through the file itself. Naming a file with the .py file extension is enough clarification for Windows systems, but Unix environments use shebangs (hashbangs) such as #!/usr/bin/env python in the first line of the file to call the correct interpreter.

To manually execute a file with Python on any operating system regardless of the file extension or presence of a shebang:

  1. Open a terminal (search for cmd or powershell on Windows)
  2. Navigate to the directory containing the file; pretending it's in the Documents folder, enter
    cd %USERPROFILE%\Documents on Windows or cd ~/Documents on Unix systems
  3. Run python file.py

Examples

The syntax of Python is designed to be very intuitive by resembling shorthand English and mathematical notation for the most part, and uses a library.submodule.function() structure for much of the foundational logic. The following examples can be entered line by line in the REPL or written to a file then executed all at once.


The import statement brings a library, or set of predefined logic, into the current environment.

import os

The help() function opens an interactive documentation menu (version 3.13+ supports entering help). To directly open the documentation page for a specific module, place the item within the parentheses.
Entering q, quit, or exit will close the help() function.

help(os)

Strings are a data type containing characters and must be enclosed in a set of either single or double quotes.

'This is a string', "This is also a valid string"

Variables are used for storing and retrieving data in a way that avoids reiterating information each time it's needed.

variable = 'This text is being stored for later'

The print() function outputs information directly to the user through a terminal.

print('This information is supposed to be read by the user.')

Comments have no functional impact on a program, but are used for easier understanding and organisation of code segments.

# This should describe the code it's attached to

Statements ending in : require the next line and other nested logic to be indented, which must use consistent whitespaces (typically 4 spaces or a single tab) throughout the environment, otherwise it will result in a syntax error.


# This is a conditional statement
if 'String' == "String":
	result = True
	print('This antecedent is an equivalent comparison.')
	print('All consequents require the same indentation.')
else:
	print("These strings aren't the same.")
					

Use exit() or quit() to stop executing a program at a specific point or to close the REPL (with version 3.13+, parentheses are not required in the REPL).

Here is a combined demonstration of the previous examples:


#!/usr/bin/env python3

import sys

# Prints Python metadata
var = 'Python version installed: '
print(var + sys.version)

# Conditionally informs user of release type
if sys.version_info[3] == 'final':
	print('This is a stable build of Python.')
else:
	print('This Python installation is not recommended for production.')
	exit(1) # Exits program with error status code
					

Code Etiquette

Writing code is a problem solving process, however the legibility of your code should not be one of those problems. Aside from the objective syntax requirements of various programming languages, there are also subjective attributes such as using spaces or tabs for indentation, delimiter padding, line length, naming conventions, and more. Because there is an availability of choice, there will inevitably be variation, which can make readability challenging depending on the decisions made.

Style guides act as a standard for subjective syntax to provide widespread consistency for the purpose of improving readability, which in turn helps with collaboration. The official style guide for Python is outlined in PEP 8, but other style guides may exist on a per-organisation or per-project level that set additional stipulations.

Regardless of the guidelines provided, the most crucial proclivities are using consistent syntax throughout a project and commenting your code. Both will significantly ease the process of deciphering logic that's unfamiliar to someone else, or even your future self.

Learning

The widespread popularity of Python makes it very easy to find educational resources. The official documentation at https://docs.python.org is an excellent reference, and real-world demonstrations can be found in open source projects throughout code hosting platforms like GitHub, GitLab, Codeberg, and others. Even video platforms have both static content and live streams of Python-related media.

This website will be updated with various articles covering applied examples and in-depth overviews of specific subjects. Some of my other open source projects linked below also contain Python code that is freely available for usage and reference.