Python Programming Language Cheat Sheet: 2022 Guide

python-programming-language-cheat-sheet:-2022-guide

Python Programming Language Cheat Sheet: 2022 Guide

With over 10.1 million developers using Python, the popularity of the Python programming language can’t be denied. Since the first release in 1990, Python has gained public support in academia and business, being used extensively in artificial intelligence and machine learning, serving as the underpinning of OpenStack, as well as powering the cloud file storage service Dropbox.

This extensibility makes Python an excellent programming language for junior developers to get started with, but also one that remains applicable at scale, as Python is used extensively for real-world applications. This cheat sheet explores what Python is used for and how it compares to other programming languages, and provides resources for learning the language. This article is also available as a download: Python programming language: A cheat sheet (free PDF).

SEE: Hiring kit: Python developer (TechRepublic Premium)

What is the Python programming language, and who created it? 

Python is an interpreted programming language (also called a scripting language), created in 1990 by Dutch programmer Guido van Rossum, following his experience working on the education-focused ABC language at CWI. Python differs from other programming languages, as it prioritizes code readability and use of whitespace over compact, tiny source files.

Python is dynamically typed and garbage-collected (through reference counting and cycle detection), supports object-oriented and structured programming fully, and largely supports functional and aspect-oriented programming, making it particularly versatile and applicable for a wide variety of use cases.

SEE: Python is eating the world: How one developer’s side project became the hottest programming language on the planet (TechRepublic cover story PDF)

The standard library is commonly considered one of the greatest strengths of Python; this feature enables programmers to quickly develop projects without needing to rely heavily on third-party packages for the basic plumbing of a given application. To complement the standard library, the Python Package Index (PyPI) catalogues over 300,000 packages that provide various functions.

What is Python used for?  

Python’s design as a language makes it a good choice for projects with multiple authors, as the inherent readability of the language aids in the ability to pick up code and clearly understand how it operates. Python is a powerful programming language, enabling even junior developers to accomplish quite a lot—as is the case for practically everything in computer science, there is an xkcd for that.  

Python is currently one of the most popular programming languages for people to learn, widely desired for its machine learning (ML) and data science attributes. The language’s scikit-learn ML library saw an 11% increase in use in 2020, while the PyTorch ML framework used for deep learning jumped in use by 159%. Python is also Microsoft’s most popular extension for Visual Studio Code, with support on Azure and an easy install option from the Windows Store.

Python is used extensively in artificial intelligence; Google’s TensorFlow framework includes Python modules, as does Keras and Scikit-learn. Similarly, Facebook’s PyTorch is increasingly popular, with support on both AWS and Azure. For data scientists wanting to prepare data for machine learning, the Anaconda project is a distribution of Python and R that’s optimized for scientific computing, with a focus on numerical methods and statistical analysis. IBM’s Qiskit and D-Wave’s Ocean learning platforms also use Python for programming quantum computers. Other science-focused options include the popular libraries NumPy, SciPy and Matplotlib.

Outside of scientific computing, Python remains popular for web development frameworks including Django, CherryPy, Pyramid, Flash, web2py, and webapp2. Graphics editing programs also use inline Python scripting, including the 3D animation software Autodesk 3ds Max, Maya, and MotionBuilder, as well as Cinema 4D, Lightwave, Houdini, and modo, the Nuke compositor, and the open-source Blender toolset. It’s also used in familiar 2D graphics software, like PaintShop Pro, as well as the open-source software GIMP, Inkscape and Scribus.  

Additionally, LibreOffice uses Python for inline scripting, much in the same way Visual Basic is used to extend features of Microsoft Office.  

SEE: All of TechRepublic’s cheat sheets and smart person’s guides

Why use Python rather than other languages?

In 1999, software developer Tim Peters, a major contributor to Python and creator of the original CPython implementation, wrote the “Zen of Python,” an explanation of Python’s design philosophy, and the philosophy that programmers should incorporate into their programming approach. The document was later incorporated into official Python documentation.

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren’t special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one—and preferably only one—obvious way to do it.

Although that way may not be obvious at first unless you’re Dutch.

Now is better than never.

Although never is often better than right now.

If the implementation is hard to explain, it’s a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea—let’s do more of those!

How does Python compare to other programming languages?

Python can be used for practically any use case that any other high-level language (C/C++, JavaScript, PHP, etc.) can be used for. Its flexibility makes it a popular tool for prototyping, especially when used with a UI toolkit. Implementations for modern microcontrollers exist, with a focus on 32-bit and higher instruction sets. 

While the CPython reference implementation is broadly useful for most use cases, other Python interpreters do exist to address specific needs and deployment scenarios. MicroPython is a microcontroller-focused implementation supporting Arm architectures, in addition to Arduino, ESP8266, ESP32, and RISC-V (32- and 64-bit) architectures, with a Raspberry Pi implementation for its Pico family of microcontroller boards and RP2040 chips. CircuitPython is an education-focused fork of MicroPython.

PyPy is the most popular general-purpose alternative implementation of Python. It differs from CPython in that PyPy is a (faster) just-in-time compiler, while CPython is an interpreter.

Pyston is another alternative implementation of Python. Pyston 2.2, an open-source implementation of Python 3.8.8, promises to be 30% faster than the original implementation. The Pyston fork of CPython 3.8.8 is available on GitHub.

While Python normally runs using an interpreter, there are tools that allow you to compile to other languages and to machine code. These include a JavaScript transpiler, C and C++ compilers. Usefully these approaches allow you to take Python code and use it as libraries and modules for use with code written in other languages.

Other language-target implementations also exist, including CLPython for Common Lisp, IronPython for .NET/Mono, and Jython for Java. Likewise, the Nuitka project is a source-to-source compiler from Python to C/C++ source code.


Image: James Sanders/TechRepublic

What version of Python should I use?

The Python 3.x series was introduced in December 2008, addressing and rectifying fundamental design flaws, as well as generally modernizing the language. Python 3 was developed with the guiding principle of “[reducing] feature duplication by removing old ways of doing things.” Due to this, Python 3 is not fully backward-compatible with Python 2, requiring developers to modernize their code to run on the new version.

Support for Python 2.7 ended on January 1, 2020. The latest releases of Python are versions 3.8.13, 3.9.13 and 3.10.5. 

Python 3.11 is currently in beta, with beta 3 released at the beginning of June 2022, and is due to be released in October 2022. Speeding up Python is a primary focus of the language’s core development team, with van Rossum hoping to double the performance of CPython in version 3.11 as part of his work on Microsoft’s Developer Division. Python 3.11 is expected to be supported until late 2027.

How do I learn programming in Python?

Learning Python doesn’t require getting a degree in computer science—there’s a wealth of resources available online to help users get started with the programming language.

Google launched a Python training course, the Google IT Automation with Python Professional Certificate, on Coursera.

SEE: Getting started with Python: A list of free resources (free PDF) (TechRepublic)

TechRepublic Academy, a joint venture between TechRepublic, ZDNet, and StackCommerce, also offers a wide variety of in-depth Python training courses.

If you’re already familiar with programming, chances are your IDE of choice either natively supports Python or support can be added using a plugin. For new programmers, using a free IDE that supports Python is a quick way to get started—these include Atom, PyCharm, Geany, Sublime Text, and Visual Studio Code. Perhaps the most popular beginners Python programming tool is the cross-platform Thonny which also supports dialects like MicroPython.

Many offer REPL (Read Evaluate Print Loop) tooling to help you try out new Python commands in your editor or browser. This approach also makes it easy to test new code as you write it, without leaving your chosen development environment. Other tools, like Jupyter Notebooks, embed a Python interpreter in a shareable document. This allows you to share code with colleagues or provide a ready-to-experiment interactive environment to experiment with machine learning or numerical analysis.

With over 10.1 million developers using Python, the popularity of the Python programming language can’t be denied. Since the first release in 1990, Python has gained public support in academia and business, being used extensively in artificial intelligence and machine learning, serving as the underpinning of OpenStack, as well as powering the cloud file storage…