C12CSCS
C12CSCS
Lists are one of the built-in data types in Python used to store multiple values in a single variable. List items are
ordered, mutable, and allow duplicate values.
13. index(item): Returns the index of the first occurrence of the specified item.
Example: mango_index = fruits.index(“mango”) → mango_index is 1
14. count(item): Returns the number of times the specified item appears in the list.
Example: count_apple = fruits.count(“apple”) → count_apple is 1
15. sort(): Sorts the items in ascending order (modifies the original list).
Example: fruits.sort() → fruits becomes sorted alphabetically
16. reverse(): Reverses the order of the items in the list (modifies the original list).
Example: fruits.reverse() → fruits becomes reversed
18. join(separator): Joins elements of a list into a single string (using a string method).
Example: joined_fruits = “, “.join(fruits) → “apple, mango, cherry”
24. Finding the Length: Get the number of items in the list.
Example: length = len(fruits) → length is 4
Tuple
10. Finding the Length: Get the number of items using len()
Example: length = len(my_tuple) → length is 3
15. Tuples are Immutable: You cannot modify, add, or remove elements once created
Example: my_tuple[1] = 10 # This will raise a TypeError
25. Creating Tuples with zip(): Combine lists into a tuple of pairs
Example: paired = tuple(zip([1, 2], [‘a’, ‘b’])) → paired is ((1, ‘a’), (2, ‘b’))
Sets:
12. Finding the Length: Get the number of items using len()
Example: length = len(my_set) → length is the count of elements
14. Intersection: Find common elements using intersection() or the & operator
Example 1: common_set = my_set.intersection({4, 5, 10}) → common_set is {4, 5}
Example 2: common_set = my_set & {4, 5, 10} → same result
15. Difference: Find elements in one set but not in another using difference() or the — operator
Example 1: diff_set = my_set.difference({5, 10}) → diff_set is {1, 4}
Example 2: diff_set = my_set — {5, 10} → same result
16. Symmetric Difference: Find elements in either set but not both using symmetric_difference() or the ^ operator
Example 1: sym_diff_set = my_set.symmetric_difference({1, 10}) → sym_diff_set is {4, 5, 10}
Example 2: sym_diff_set = my_set ^ {1, 10} → same result
17. Set Intersection Update: Modify a set to keep only elements found in another set using intersection_update()
Example: my_set.intersection_update({1, 2, 3}) → my_set becomes {1}
18. Set Difference Update: Modify a set to remove elements found in another set using difference_update()
Example: my_set.difference_update({1}) → my_set becomes set()
19. Set Symmetric Difference: Update Modify a set to keep only elements found in either set but not both using
symmetric_difference_update()
Example: my_set.symmetric_difference_update({2, 3}) → my_set becomes {2, 3}
22. Disjoint Sets: Check if two sets have no elements in common using isdisjoint()
Example: are_disjoint = my_set.isdisjoint({7, 8}) → are_disjoint is True
Dictionary:
Dictionary is one of the built-in datatype that allows us to store data in key-value pair which is unordered and mutable.
1. Creating a Dictionary: Create a dictionary using curly braces
Example: my_dict = {“key1”: “value1”, “key2”: “value2”}
4. Adding or Updating Values: Add a new key-value pair or update an existing key
Example: my_dict[“key3”] = “value3” → my_dict becomes {“key1”: “value1”, “key2”: “value2”, “key3”: “value3”}
9. Getting the Length: Get the number of key-value pairs using len()
Example: length = len(my_dict) → length is the count of pairs
17. Default Values: Use get() to return a value with a default if the key is not found
Example: value = my_dict.get(“key5”, “default”) → value is “default”
18. Popitem: Remove and return the last inserted key-value pair using popitem()
Example: last_item = my_dict.popitem() → my_dict is modified
19. Dictionary from Keys: Create a dictionary with keys from an iterable and a default value
Example: new_dict = dict.fromkeys([“a”, “b”, “c”], 0) → new_dict is {“a”: 0, “b”: 0, “c”: 0}
24. Finding Key Index: Use next() with an iterator to find the index of a key
Example: index = next((i for i, k in enumerate(my_dict) if k == “key1”), None)
25. Converting to List: Convert keys, values, or items to a list
Example: keys_list = list(my_dict.keys()) → keys_list contains the keys of my_dict
Summary
Lists are great for storing ordered collections and can be easily changed.
Tuples are perfect for when you need fixed data that shouldn’t change.
Sets are used for unique items and are great for doing math operations with groups.
Dictionaries help you store information in key-value pairs, making it easy to find what you need.