This is problematic when you are passing lists as a argument in a function
my_list = []new_list = my_list # creates a pointer to my_listmy_list.append(2)print(new_list) # [2]copied_list = my_list.copy() # creates a new list object in memorymy_list.append(3)print(my_list) # [2, 3]print(new_list) # [2, 3]print(copied_list) # [2] # sort in reverse ordermy_list.sort(reverse=True)
Set
seen = set() seen.add(1)seen.remove(1)
can compare equality of two sets with ==
Dictionary
my_dict = {}my_dict["key"] = "value"# to force a default value my_dict.get("key", "default value")#remove my_dict.pop("key")
String
my_string = " my string "# remove trailing whitespacemy_string.strip()# convert into array based on spacemy_string.split(" ")
Python List as stacks
my_stack = []my_stack.append(10) # [10]my_stack.append(20) #[10, 20]my_stack[-1] # look at the last element of the stack print(my_stack.pop()) # 20 (last element added into stack)
Double-ended queue in python
my_queue = collections.deque([])my_queue.append(10) # Appends 10 to the right of the queuemy_queue.appendleft(20) # Appends 20 to the left of the queue# [20, 10]my_queue.pop() # pops from the right of the queue# [20]my_queue.append(10)# [10, 20]my_queue.popleft() # pops from the left of the queue# [10]my_queue.append(10)# [10, 20]# Accessing the queueprint(my_queue[0]) # 10print(my_queue[-1]) # 20
List Comprehension
[expression for item in iterable if condition]numbers = range(10) # 0 ... 9even_numbers = [num for num in numbers if numbers % 2 == 0]
DefaultDicts
from collections import defaultdict # Using list as default_factory word_counts = defaultdict(list) word_counts['fruits'].append('apple') word_counts['fruits'].append('banana') word_counts['vegetables'].append('carrot') print(word_counts) # Output: defaultdict(<class 'list'>, {'fruits': ['apple', 'banana'], 'vegetables': ['carrot']})# Using int as default_factory char_counts = defaultdict(int) for char in "hello": char_counts[char] += 1print(char_counts) # Output: defaultdict(<class 'int'>, {'h': 1, 'e': 1, 'l': 2, 'o': 1})
list: New keys will have an empty listĀ []Ā as their default value.Ā This is useful for grouping items.
int: New keys will haveĀ 0Ā as their default value.Ā Useful for counting occurrences.
set: New keys will have an empty setĀ {}Ā as their default value.
str: New keys will have an empty stringĀ ""Ā as their default value.
f string
name = "Alice"age = 30message = f"Hello, {name}. You are {age} years old."print(message)# Output: Hello, Alice. You are 30 years old.result = f"The sum of 5 and 10 is {5 + 10}."print(result)# Output: The sum of 5 and 10 is 15.price = 123.45678formatted_price = f"The price is {price:.2f}." # Format to 2 decimal placesprint(formatted_price)# Output: The price is 123.46.large_number = 1234567formatted_number = f"Value: {large_number:,}" # Add comma as thousands separatorprint(formatted_number)# Output: Value: 1,234,567
Assert
# assert condition, messagedef add(a, b): return a + b assert add(1, 1) == 2, "expected 2 but got something else"
if condition evaluates to False, an AssertionError is raised
message is optional but provides additional information about the assertion if it fails
tricks
heights = [10, 10, 10, 10]for i, h in enumerate (heights): # i represents the index # h represents the height at each index
nonlocal keyword is used within nested functions to explicitly declare that a variable refers to a variable in the nearest enclosing scope that is not the global scope