kota's memex

Variables and functions are usually named with snake_case while classes are typically CapitalCamelCase.

multiple assignment

You can assign values to multiple variables on one line:

x, y, z = 0, 0, 0

globals

Anything defined in the top level scope is a global variable. Accessing them from inside your functions requires the global keyword prefix.

None

None is like nil in other languages. It's returned when a function wants to indicate that no value was produced.

Don't use the equality "==" symbol to compare objects to None Use "is" instead. This checks for equality of object identity.

None is None   # => True

constants

Python doesn't have constants. Per convention variables named with ALL_CAPS are not intended to change:

MAX_CONNECTIONS = 5000

strings

Strings are immutable sequences of Unicode code points. Single, Double, or '''Triple''' quoted strings may span multiple lines - all associated whitespace will be included in the string literal.

message = "hello world"
print(message.title()) # Hello World

String literals that are part of a single expression and have only whitespace between them will be implicitly converted to a single string literal. That is, ("spam " "eggs") == "spam eggs".

There is also no mutable string type, but str.join() or io.StringIO can be used to efficiently construct strings from multiple fragments.

formatting

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)

There's also some escape codes:

\n newline
\t tab

methods

https://docs.python.org/3/library/stdtypes.html#string-methods

str.rstrip() - Strip whitespace.
str.endswith(suffix[, start[, end]]) - Returns true if the string ends with a suffix.
str.isdecimal() - Return True if all characters in the string are decimal characters and there is at least one character.
str.removeprefix(prefix) - Remove a prefix from a string.

numbers

There are three distinct numeric types: integers, floating-point numbers, and complex numbers. In addition, Booleans are a subtype of integers. Integers have unlimited precision. Floating-point numbers are usually implemented using double in C.

Integers and Floats

When you divide any two numbers, even if they are integers that result in a whole number, you'll always get a float:

print(4/2) # 2.0

Underscores

When writing long numbers you can group digits with underscores: 14_000_000_000.

type conversions

Converting from string to int: int()