Code-Memo

Lists

A tuple is an ordered, mutable collection of items.

my_list = [1, 2, 3, 4, 5]

print(my_list[0])  # 1
print(my_list[-1])  # 5

Slicing

print(my_list[1:3])  # [2, 3]
print(my_list[:2])  # [1, 2]
print(my_list[2:])  # [3, 4, 5]

Adding Elements

Removing Elements

List Comprehensions

squares = [x**2 for x in range(10)]

List Techniques

Tuples

A tuple is an ordered, immutable collection of items.

my_tuple = (1, 2, 3)

print(my_tuple[0])  # 1
print(my_tuple[-1])  # 3

Tuple Unpacking

Extracting values from a tuple.

a, b, c = my_tuple

Advanced Tuple Techniques

Sets

A set is an unordered, mutable collection of unique elements.

my_set = {1, 2, 3} # my_set = set({1, 2, 3})

Removing Elements

Set Operations

Advanced Set Techniques

Dictionaries

A dictionary is an unordered, mutable collection of key-value pairs.

my_dict = {'name': 'Alice', 'age': 25}

Accessing Elements

Use keys to access values.

print(my_dict['name'])  # Alice

Adding/Updating Elements

Removing Elements

Dictionary Comprehensions

A brief way to create dictionaries.

squares = {x: x**2 for x in range(10)}

Advanced Dictionary Techniques