List
Definition: A list is an ordered, mutable collection of items in Python. Lists are defined using
square brackets [].
Structure: my_list = [1, 2, 3, 'hello']
Methods:
- append(x) - Add item x to the end of the list.
- extend(iterable) - Add elements from an iterable.
- insert(i, x) - Insert item x at index i.
- remove(x) - Remove the first occurrence of x.
- pop([i]) - Remove and return item at index i (or last if not specified).
- clear() - Remove all items from the list.
- index(x) - Return index of first occurrence of x.
- count(x) - Count occurrences of x.
- sort() - Sort the list in ascending order.
- reverse() - Reverse the list in place.
- copy() - Return a shallow copy of the list.
Tuple
Definition: A tuple is an ordered, immutable collection of items in Python. Tuples are defined using
parentheses ().
Structure: my_tuple = (1, 2, 3, 'hello')
Methods:
- count(x) - Return the number of occurrences of x.
- index(x) - Return the index of the first occurrence of x.
String
Definition: A string is a sequence of characters in Python, enclosed within single, double, or triple
quotes.
Structure: my_string = 'Hello, World!'
Methods:
- lower(), upper(), capitalize(), title()
- strip(), lstrip(), rstrip()
- replace(old, new), find(sub), count(sub)
- split(sep), join(iterable)
- startswith(sub), endswith(sub)
- isdigit(), isalpha(), islower(), isupper()
Set
Definition: A set is an unordered, mutable collection of unique items in Python. Sets are defined
using curly braces {}.
Structure: my_set = {1, 2, 3}
Methods:
- add(x), update(iterable)
- remove(x), discard(x), pop(), clear()
- union(set2), intersection(set2), difference(set2), symmetric_difference(set2)
- issubset(set2), issuperset(set2), isdisjoint(set2)
Dictionary
Definition: A dictionary is an unordered, mutable collection of key-value pairs. Defined using curly
braces {} with keys and values.
Structure: my_dict = {'name': 'Alice', 'age': 25}
Methods:
- keys(), values(), items()
- get(key), update(other_dict)
- pop(key), popitem(), clear(), copy()
- setdefault(key, default)
File
Definition: Python provides built-in functions to read and write files using the open() function.
Structure: with open('[Link]', 'r') as f:
Methods:
- read(), readline(), readlines()
- write(text), writelines(lines)
- seek(pos), close()