Skip to content

Python

  • Definition: High-level, interpreted, general-purpose programming language.
  • Design Philosophy: Prioritizes code readability using significant indentation (whitespace).
  • Typing & Memory: Dynamically-typed and garbage-collected (automatic memory management).
  • Paradigms Supported: Procedural (structured), Object-Oriented (OOP), and Functional programming.
  • Ecosystem: Known as a “batteries included” language due to its massive, comprehensive standard library. Constantly ranks among the most popular languages.

Pros:

  • Easy to learn & highly readable.
  • High-level language (abstracts away memory management).
  • Boosts developer productivity.
  • Cross-platform and portable.
  • “Batteries included” (vast libraries).
  • Widely supported community and highly enjoyable to use.

Cons:

  • Speed Limitations: Slower execution compared to compiled languages (like C++).
  • Memory Consumption: Dynamic typing and rich objects use more memory.
  • Requires More Testing: Dynamic typing means type errors are caught at runtime, not compile time.

Python is dynamic and interpreted. It runs in two modes that produce identical results:

  1. Interactive Mode: Evaluates and responds to code statement-by-statement in real-time.
    • Launch: Open terminal $\rightarrow$ Type python $\rightarrow$ Press Enter. (Look for the >>> prompt).
    • Run Code: Type print("Hello, World!") and press Enter. The output appears without the >>> prompt.
    • Exit: Type exit() or press Ctrl-D.
  2. Script Mode: Executes an entire .py file containing multiple statements from top to bottom.
  • Origin: 19 guiding principles for Pythonic design written by Tim Peters in 1999.
  • The 20th Rule: Left blank intentionally for Python’s creator, Guido van Rossum, to fill (he never did).
  • Easter Egg: Can be viewed in the interactive interpreter by typing import this.

The 19 Principles & What They Refer To:

  1. Beautiful is better than ugly.Feature: Clean, readable syntax without clutter (e.g., no semicolons).
  2. Explicit is better than implicit.Feature: Importing specific modules (import math) rather than wildcard imports (from math import *).
  3. Simple is better than complex.Feature: Using built-in functions like sum() instead of writing manual loops.
  4. Complex is better than complicated.Feature: For difficult problems, use structured classes rather than chaotic “spaghetti” logic.
  5. Flat is better than nested.Feature: Avoiding deeply nested if statements by using early return statements.
  6. Sparse is better than dense.Feature: Writing one logical statement per line rather than cramming code together.
  7. Readability counts. → Feature: Python’s use of mandatory indentation instead of curly braces {} for code blocks.
  8. Special cases aren’t special enough to break the rules.Feature: Maintaining consistent coding standards (PEP 8) across all modules.
  9. Although practicality beats purity.Feature: Allowing technical workarounds if they drastically improve execution speed or usability.
  10. Errors should never pass silently.Feature: Python aggressively raises Exceptions (e.g., TypeError, ValueError) when things go wrong.
  11. Unless explicitly silenced.Feature: Using try...except blocks with a pass statement when you expect an error and actively choose to ignore it.
  12. In the face of ambiguity, refuse the temptation to guess.Feature: Python will not implicitly guess types (e.g., "1" + 1 raises an error instead of guessing if you want "11" or 2).
  13. There should be one— and preferably only one —obvious way to do it.Feature: “Pythonic” idioms, like using a list comprehension to filter data.
  14. Although that way may not be obvious at first unless you’re Dutch.Reference: A joke referencing Python’s creator, Guido van Rossum, who is Dutch.
  15. Now is better than never.Feature: Favoring rapid, iterative prototyping over infinite planning.
  16. Although never is often better than right now.Feature: Taking the time to structure code properly rather than rushing a flawed implementation.
  17. If the implementation is hard to explain, it’s a bad idea.Feature: A warning against overly clever, unreadable, or “hacky” logic.
  18. If the implementation is easy to explain, it may be a good idea.Feature: Good system design is naturally intuitive to other developers.
  19. Namespaces are one honking great idea — let’s do more of those!Feature: Modules and class structures that prevent variable name collisions (e.g., math.pi vs numpy.pi).
  • Timeline: Founded in the 1980s.
  • Lineage: Descendant of the ABC programming language.
  • Creator: Guido Van Rossum, known formally in the community as the “Benevolent Dictator for Life” (BDFL).
  • Python 3: Released December 2008 to fix fundamental design flaws in Python 2. It is strictly backward incompatible.
  • Python 2: Officially deprecated and no longer supported by the Python Software Foundation.
  • Verification: Run python --version in your terminal to check the active version.
Major VersionRelease DateMajor VersionRelease Date
1.0January 19943.0December 3, 2008
1.5December 31, 19973.1June 27, 2009
1.6September 5, 20003.2February 20, 2011
2.0October 16, 20003.3September 29, 2012
2.1April 17, 20013.4March 16, 2014
2.2December 21, 20013.5September 13, 2015
2.3July 29, 20033.6December 23, 2016
2.4November 30, 20043.7June 27, 2018
2.5September 19, 20063.8October 14, 2019
2.6October 1, 20083.9October 2020
2.7July 3, 20103.10October 2021
  • Official Resources: Rely strictly on official documentation (www.python.org/doc/) to avoid incorrect third-party information. Use the help() command inside the Python interpreter for built-in assistance.
  • Unix/Linux Installation: Download source, run ./configure, make, and make install.
  • Windows Installation: Download and execute the .msi installer from https://www.python.org/download/.

4. Virtual Environments & Pyenv Operations

Section titled “4. Virtual Environments & Pyenv Operations”

Virtual environments solve the problem of conflicting dependencies by isolating Python interpreters, scripts, and libraries from the host OS and from each other. Standard tools (pip, setuptools) automatically target the active environment. Common tools include virtualenv, pyenv, pipenv, and python -m venv.

Pyenv Management Toolkit:

  • Install pyenv: $ curl -L https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
  • List installable versions: $ pyenv install -l
  • Install a specific version: $ pyenv install <version>
  • List installed versions: $ pyenv versions
  • Set global default version: $ pyenv global <version>
  • Create a virtual environment: $ pyenv virtualenv <version> <venv_name>
  • Activate environment: $ pyenv activate <venv_name>
  • Deactivate environment: $ pyenv deactivate
  • Delete environment: $ pyenv uninstall <venv_name>
  • Auto-select locally: $ pyenv local <version> (Forces a specific version/environment whenever you enter that directory or its subdirectories).

5. Environment Isolation Architecture (Crucial)

Section titled “5. Environment Isolation Architecture (Crucial)”

The core logic behind virtual environments is absolute directory separation.

  • Isolated Binaries: Each installed Python version and virtual environment maintains its own dedicated python and pip executables.
  • Isolated Libraries: Each environment possesses an exclusive site-packages directory. When pip install is executed, packages are routed strictly to this isolated folder, ensuring zero cross-contamination between projects.

Would you like me to process the next block of Python text, or would you prefer to break down how to automate the pyenv setup in a bash script?

Virtual Environments

More Basics

Scopes

OOPs

Files

Modules & Packages

Project Architecture & Packages

Exceptions Handling

Logging & Debugging

Jinja2 Basics

Web Apps Arch. + Flask + HTTP

Unit Testing!

Dockerizing Python Apps

OS Modules for Files and Dir

Flask