- Definition: Lists are ordered, mutable collections of items.
- Usage:
nums = [1, 2, 3]
- Creates a list with elements 1, 2, and 3.
- Lists can hold items of different data types, though typically they hold similar types for consistency.
- Definition: Lists in Python are dynamic, meaning they can grow and shrink in size.
- Usage:
nums.append(4) # Adds element 4 to the end of the list nums.pop() # Removes and returns the last element (4 in this case)
append
is used to add an item to the end of the list.pop
is used to remove the last item from the list.
- Definition: Accessing a subset of the list using indices.
- Usage:
nums[1:3]
(returns elements at index 1 and 2)nums = [1, 2, 3, 4] sliced = nums[1:3] # Result: [2, 3]
- Slicing allows you to create a new list from an existing one, starting at the first index and up to but not including the second index.
- Definition: A concise way to create lists.
- Usage:
[i for i in range(5)]
(creates[0, 1, 2, 3, 4]
)squares = [i*i for i in range(5)] # Result: [0, 1, 4, 9, 16]
- List comprehensions provide a concise way to create lists from sequences.
- Definition: Assigning elements of a list to multiple variables.
- Usage:
a, b, c = [1, 2, 3]
nums = [1, 2, 3] a, b, c = nums # a=1, b=2, c=3
- Useful for quickly assigning values without needing to access indices individually.
- Definition: Strings are immutable sequences of characters.
- Usage:
s = "hello"
- Creates a string with the text "hello".
- Definition: Accessing a subset of the string.
- Usage:
s[1:3]
(returns characters at index 1 and 2)s = "hello" sliced = s[1:3] # Result: "el"
- Definition: Strings cannot be changed after they are created.
- Usage:
s = "hello" s = s + " world" # Creates a new string "hello world"
- To change a string, you must create a new one.
- Definition: Changing data types to/from strings.
- Usage:
num = int("123") # Converts string "123" to integer 123 s = str(123) # Converts integer 123 to string "123"
- Definition: Tuples are immutable, ordered collections of items.
- Usage:
t = (1, 2, 3)
- Creates a tuple with elements 1, 2, and 3.
- Definition: Tuples cannot be changed after creation.
- Usage:
t = (1, 2, 3) # t[0] = 0 # This will raise an error because tuples are immutable
- Definition: Tuples can be used as keys in dictionaries.
- Usage:
d = {(1, 2): "a pair"} # Tuples as keys
- Definition: Dictionaries are mutable collections of key-value pairs.
- Usage:
d = {"a": 1, "b": 2}
- Creates a dictionary with keys "a" and "b" mapped to values 1 and 2, respectively.
- Definition: Retrieve a value by its key.
- Usage:
d["a"]
(returns1
)value = d["a"] # Result: 1
- Definition: Change the value associated with a key.
- Usage:
d["a"] = 10
d["a"] = 10 # Changes the value of key "a" to 10
- Definition: Delete a key-value pair.
- Usage:
del d["a"]
del d["a"] # Removes the key "a" and its value from the dictionary
- Definition: A concise way to create dictionaries.
- Usage:
{i: i*2 for i in range(3)}
(creates{0: 0, 1: 2, 2: 4}
)doubled = {i: i*2 for i in range(3)} # Result: {0: 0, 1: 2, 2: 4}
- Definition: Sets are unordered collections of unique items.
- Usage:
s = {1, 2, 3}
- Creates a set with elements 1, 2, and 3.
- Definition: Add or remove items from a set.
- Usage:
s.add(4) # Adds 4 to the set s.remove(3) # Removes 3 from the set
- Definition: A concise way to create sets.
- Usage:
{i for i in range(5)}
squares = {i*i for i in range(5)} # Result: {0, 1, 4, 9, 16}