My Python Cheatsheet

# Single line comments start with a hash

"""
Python doesn't have multiline comments per se, but you can use
multiline strings for this purpose
"""

# Before talking about values, you should know that everything
# in Python is an object. That means, you can pass any value to
# `dir` to see what attributes and functions it offers
dir(0)

# You can get its type
type(0) # => int

# Then, you can get the documentation for the entire type or for
# a specific attribute or method
help(int)
help(int.real)


## 1. Builtin Types

## 1.1. Numbers

# You have integers
5

# and floats
10.0

# Integers have arbitrary size, so you can have a number
# as big as you need
2491210912100009210090

# You can define numbers using scientific notation
3.45e-4

# You have the expected arithmetic operations
1 + 1   # => 2
8 - 1   # => 7
10 * 2  # => 20
35 / 5

# Besides normal division, you also have integer division,
# which rounds down
5 // 3       # => 1
-5 // 3      # => -2
5.0 // 3.0   # => 1.0  # works on floats too
-5.0 // 3.0  # => -2.0

# Modulo
7 % 3 # => 1

# Exponentiation
2**3 # => 8

## 1.2. Booleans
True
False

# When used in specific context, values of other types are
# automatically evaluated to True or False
# None, 0 and empty collections all evaluate to False
# All other values evaluate to True
bool(0) # => False
bool("") # => False
bool([]) # => False
bool(1)  # => True

# `is` vs `==`
# The short version: use `is` only to check if a variable is None
if x is None:
  pass

# Use `==` for all other objects

## 1.3. Strings

# Strings are created with " or ', unlike other languages where
# `'` is only for chars
"This is a string."
'This is also a string. There\'s no difference.'

# You can treat strings as a list of characters, so you can use
# indexing and other list-like operations
"this"[0] # => "t"

# Strings are immutable
"this"[0] = "z" # => TypeError: 'str' object does not support
                   # item assignment

# Python offers several options for string formatting.
# The most recent way is provide by f-strings
name = "Luke"
f'She said his name is ${name}'

# For printing, you use the `print` function (it used to be a
# statement)
print("I'm Python. Nice to meet you!")

# Besides strings, Python provides several other types of
# collections

## 1.4. Lists
[]
[1, 2, 3]
# Lists are mutable

# Comprehensions
# Python has something called <collection> comprehension,
# which is special syntax to generate a collection with
# certain values.

# There are comprehensions for several collection types,
# except for strings and tuples

# a list comprehension
[x for x in range(10) if x % 2 == 0] # => [0, 2, 4, 6, 8]

## 1.5. Tuples
# When the tuple has one element, you need to use the comma
# to differentiate it from normal parentheses.
(1,)
()
(1,2,3)
# Tuples are immutable

# Slices
# Strings, Lists and Tuples support slice syntax to access
# elements from the collection
t = (1,2,3)
t[0] # => 1
t[-1] # 3
t[0:2] # (1,2)
# ... and more

## 1.6. Dictionaries
# They are key-value pairs. The keys need to be immutable
{}
{'name': "Luke"}
# Dictionaries themselves are mutable

# dict comprehension
{f"val_{x}": x for x in range(8) if x % 2 == 0}
  # => {'val_0': 0, 'val_2': 2, 'val_4': 4, 'val_6': 6}

## 1.7. Sets
# They are immutable and the values they hold are unique,
# just like in mathematics
set() # empty set
{1}
{1, 2}

# set comprehension
{x for x in range(10) if x % 2 == 0} # => {0, 2, 4, 6, 8}

# 2. Control Structures

# 3. Functions

# 3.1. Generators

# 3.2. Decorators

# 4. User-defined Types or Classes

# 5. Metaclasses