python

Page content
  • Guido van Rossum is the creator of the Python programming language.
  • Every number, string, data structure, function, class, module, and so on exist in the Python interpreter in its own “box,” which is referred to as a Python object.
  • Python is a high-level programming language that’s implemented in C.

Pythonic

  • Generic operations on sequences
  • built-in tuple and mapping types
  • structure by indentation
  • strong typing without variable declarations

Variables

  • A python variable name is a symbolic name that is a reference or pointer to an object.
  • Avoid the global variables.
if not hasattr(<func>, <var>):
 <func>.<var> = ...

string

Splitting

<string_var>.splitlines()
<string_var>.split('<character>', 1) # 1 means only split once so it will return a two-item list.

Complacent Comma Placement

  • Always end your lines with a comma.
names = [
     'Alice',
     'Bob',
]

String literal concatenation

my_str = ( 'This is a super long string constant '
      'spread out across multiple lines. '
      'And look, no backslash characters needed!')

Using the python interpreter interactively

  • The most straightforward way to start talking to Python is in an interactive Read-Eval-Print Loop (REPL) environment. That simply means starting up the interpreter and typing commands to it directly. The interpreter:

Reads the command you enter Evaluates and executes the command Prints the output (if any) to the console Loops back and repeats the process


Code visualization

Motivation

  • Don’t Repeat Yourself! Do One Thing!

Dictionary

  • currying: deriving new functions from existing ones by partial argument application. (named after Haskell Curry.)