Skip to content

Python

Python is an easy-to-learn high-level general-purpose programming language. It is especially suitable for beginners to programming, amateurs/hobbyists, scientists, engineers.

Pros:

  • Easy to learn
  • High-level
  • Dynamically typed
  • Simple and readable syntax
  • Vast ecosystem
  • Large community and help sources
  • Rapid prototyping
  • Excellent glue language
  • Many packages interface with C or other fast languages to achieve performant code
  • Interoperable with many other languages

Cons:

  • Very slow: About 50 times slower than C. This is not a deal-breaker though for a glue/scripting language, and for prototyping purposes.
  • Low-quality packages: There are almost no imposed requirements on packages and compability between packages: Expect compatibility issues, breaking functionality across package versions, unintuitive APIs, and lacking documentation.
  • Non-standardized tooling: despite the emphasis on the Pythonic way, there are hilariously many cases of competing tools, packages, and standards: see dependency management, repository structure, unit testing, to name a few.
  • Dynamically typed: great for rapid prototyping, not for large frameworks.
  • No built-in language support for vector and matrix operations: results in needlessly verbose code for any engineering or data science applications.
  • No fluent interface: No support for chaining function calls, which is a common use case in analysis scripts and data manipulation. This results in many data science libraries resorting to the builder pattern, which forces the use of implementing subclasses just to call another function.

Short Python cheatsheets:

  • https://www.pythoncheatsheet.org/
  • https://quickref.me/python

Run

Script

Action Code Details
Start Python script, wait for completion
os.system('python script.py')
Start Python script with arguments, wait for completion
os.system('python script.py -file "hello.csv")

Environment

Environment variables

All snippets require import os. Updates to the environment are not reflected in the os.environ mapping.

Action Code Details
Get list of all environment variables
list(os.environ)
Get all environment variables as dict
dict(os.environ)
Check if environment variable exists
'path' in os.environ
Case-insensitive
Get value of environment variable, error if not found
os.environ['path']
Try get value of environment variable, or return None
os.getenv('path')
Try get value of environment variable, or return None
os.environ.get('path')
Try get value of environment variable, or return default value v
os.environ.get('missingvar', v)
Set / update environment variable
os.environ['derp'] = 'hello'
Set environment variable only if it is not yet defined
os.environ.setdefault('derp', 'world')
Delete environment variable, error if not defined
del os.environ['derp']
Delete environment variable, error if not defined
os.environ.pop('derp')
Try delete environment variable
os.environ.pop('derp', None)