All The Python Language Features You Need - DeriveIt
All The Python Language Features You Need - DeriveIt
Cheat Sheets
Here are all the import Python language features you need to know. You can read the official Python docs too - we recommend
skimming the Built-in Functions and Built-in Types sections.
https://leetcode.com/playground/new is a good place to run code if you ever want to try anything out. Use Python3.
Built-in Types
x = "123" # string
x = 1 # integer
x = 1.0 # float
None # null
True # true
False # false
Basic Operations
x and y # and
x or y # or
1 % 2 # 1 (mod)
1 / 2 # .5 (division)
1 // 2 # 0 (division, rounding down)
2 ** 5 # 32 (power, 2^5)
a == b # compare by value
a is b # compare by memory location (exact comparison)
float('inf') # +infinity
float('-inf') # -infinity
for x in range(20, 30, 2): # 20...29 in steps of 2 (20, 22, 24, 26, 28)
# ...
List Comprehensions
Python has nice syntax for creating Arrays. Here's an example:
x = [a for a in [1,2,3,4,5,6] if a % 2 == 0]
# [2,4,6]
Here's an example that shows you what list comprehension is actually doing:
Generator Comprehensions
List comprehensions always run the entire for-loop (above). Generator comprehensions generate values lazily, and can stop early. To do
a generator comprehension, just use () instead of [] .
computations = (expensiveComputation(a) for a in range(20)) # <-- computes nothing here. The loop only run
when we read from the `computations` variable
Note that () can mean a tuple, or it can mean a generator. If you use them like this it means a generator.
https://deriveit.org/coding/cheat-sheets/118 2/4
2/12/24, 12:15 PM All the Python Language Features You Need - DeriveIt
If you're curious how generator comprehensions work internally, instead of running x.append(...) above, they just run yield ...
(it's ok if you don't know about yield).
List Features
x = [1,2,3] + [4,5,6] # [1,2,3,4,5,6]
x = [0, 7] * 10 # [0,7,0,7,0,7,0,7,0,7,0,7,0,7,0,7,0,7,0,7,0,7]
x = [0,1,2,3,4]
x[-1] # 4
x[3:] # [3,4]
x[:3] # [0,1,2] (start=0, stop=3 exclusive)
x[1:4] # [1,2,3]
x[4:0:-1] # [4,3,2,1] (start=4, stop=0 exclusive, step=-1)
See Matrix with One for more details about [x]*n syntax, and why you shouldn't use it to build a 2D array.
Set Features
DeriveIt
x = {"a", "b", "c"} Log In
y = {"c", "d", "e"}
y = x | y # merge sets, creating a new set {"a", "b", "c", "d", "e"}
String Features
x = 'abcde' # you can use '' or "" for strings (it doesn't matter which)
y = "abcde"
y[2] # gives "c"
z = x + y # concatenate strings
Functions
You declare a function using the def keyword:
def my_function():
# do things here
All variables you declare inside a function in Python are local. In most cases you need to use nonlocal if you want to set variables
outside a function, like below with x and y .
myfunction()
x # 100
y # 1
z[0] # 1
Anonymous Functions
You can also declare a function in-line, using the keyword lambda . This is just for convenience. It's useful when you want to pass a
function as a parameter and you don't care about naming it. These two statements both create the same function:
def fn(x,y):
return x + y
lambda x,y: x + y
Casting
You can convert between primitive types like this:
x = [1, 2, 3, 4]
s = set(x) # converts x to a set
t = tuple(x) # converts x to a tuple
l = list(s) # converts s to a list (order might not be the same as x because sets don't store order)
n = 7
s = str(n) # converts n to a string, '7'
z = int(s) # converts s to a number again, 7
Newlines
You can write something on multiple lines by escaping the newline like this:
x = 5 \
+ 10 \
+ 6
Mark as Completed:
Next
Concept
https://deriveit.org/coding/cheat-sheets/118 4/4