PROG301.ipynb - Colab
PROG301.ipynb - Colab
ipynb - Colab
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
fifth_element = my_list[4]
print(fifth_element)
50
2. Check for membership: How can you check if the number 25 is present in my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]?
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
is_present = 25 in my_list
print(is_present)
False
3. Get the length of a list: How can you find the number of elements in my_list = [15, 25, 35, 45, 55, 65, 75, 85, 95, 105]?
my_list = [15, 25, 35, 45, 55, 65, 75, 85, 95, 105]
length = len(my_list)
print(length)
10
4. Get the length of a list: How can you find the number of elements in my_list = [15, 25, 35, 45, 55, 65, 75, 85, 95, 105]?
my_list = [15, 25, 35, 45, 55, 65, 75, 85, 95, 105]
length = len(my_list)
print("The number of elements in the list is:", length)
5. Add an item to the list: How would you append 110 to my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]?
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
my_list.append(110)
print(my_list)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
6. Remove an item from the list: How can you remove 50 from my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]?
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
my_list.remove(50)
print(my_list)
https://colab.research.google.com/drive/1TUXZEQ-i4KOSQaSWSgV9IraTVtpFJgrw#scrollTo=PquWvgq_MFXJ&printMode=true 1/5
11/17/24, 11:40 PM PROG301.ipynb - Colab
7. Sort a list: How would you sort my_list = [50, 40, 30, 20, 10, 60, 70, 80, 90, 100] in ascending order?
my_list = [50, 40, 30, 20, 10, 60, 70, 80, 90, 100]
my_list.sort()
print(my_list)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
8. Slice a list: How would you retrieve elements from index 3 to 8 in my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]?
[4, 5, 6, 7, 8]
9. Calculate sum and average of list elements: How would you calculate the sum and mean of my_list = [12, 14, 16, 18, 20, 22, 24, 26, 28, 30]?
my_list = [12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
total = sum(my_list)
average = total / len(my_list)
print("Sum:", total)
print("Average:", average)
Sum: 210
Average: 21.0
10. List comprehension for transformations: How can you create a new list containing cubes of all numbers in my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9,
10]?
11. Flatten nested lists: How would you convert nested_list = [[1, 2], [3, 4]] into [1, 2, 3, 4]?
[1, 2, 3, 4]
10
2. Check for membership: How would you check if the number 15 exists in my_tuple = (5, 10, 15, 20)?
https://colab.research.google.com/drive/1TUXZEQ-i4KOSQaSWSgV9IraTVtpFJgrw#scrollTo=PquWvgq_MFXJ&printMode=true 2/5
11/17/24, 11:40 PM PROG301.ipynb - Colab
my_tuple = (5, 10, 15, 20)
exists = 15 in my_tuple
print(exists)
True
3. Concatenate/join tuples: How can you combine (1, 2) and (3, 4) into one tuple?
tuple1 = (1, 2)
tuple2 = (3, 4)
combined_tuple = tuple1 + tuple2
print(combined_tuple)
(1, 2, 3, 4)
4. Get the length of a tuple: How can you find the number of elements in my_tuple = (100, 200, 300)?
5. Convert tuple to list and back: How would you modify (1, 2, 3) by first converting it to a list, adding 4, and converting it back to a tuple?
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
my_list.append(4)
modified_tuple = tuple(my_list)
print(modified_tuple)
(1, 2, 3, 4)
6. Tuple unpacking: How can you assign elements of (1, 2, 3) to variables a, b, and c?
my_tuple = (1, 2, 3)
a, b, c = my_tuple
print(a, b, c)
1 2 3
7. Use tuple as a dictionary key: How can you use (1, 2) as a key in a dictionary?
my_dict = {}
key = (1, 2)
my_dict[key] = "Value associated with (1, 2)"
print(my_dict)
True
2. Concatenate/join sets: How would you combine set1 = {1, 2, 3, 4, 5} and set2 = {6, 7, 8, 9, 10}?
https://colab.research.google.com/drive/1TUXZEQ-i4KOSQaSWSgV9IraTVtpFJgrw#scrollTo=PquWvgq_MFXJ&printMode=true 3/5
11/17/24, 11:40 PM PROG301.ipynb - Colab
set1 = {1, 2, 3, 4, 5}
set2 = {6, 7, 8, 9, 10}
combined_set = set1.union(set2)
print(combined_set)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
3. Get the length of a set: How can you find the number of elements in my_set = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}?
my_set = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
print(len(my_set))
10
4. Add an item to a set: How can you add 25 to my_set = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}?
my_set = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
my_set.add(25)
print(my_set)
{100, 70, 40, 10, 80, 50, 20, 25, 90, 60, 30}
5. Remove an item from a set: How would you remove 30 from my_set = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}?
my_set = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
my_set.remove(30)
print(my_set)
6. Union, intersection, and difference of sets: How can you find the union, intersection, and difference of A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7,
8}?
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
union_set = A | B
intersection_set = A & B
difference_set = A - B
print("Union:", union_set)
print("Intersection:", intersection_set)
print("Difference:", difference_set)
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection: {4, 5}
Difference: {1, 2, 3}
my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5, "key6": 6, "key7": 7, "key8": 8, "key9": 9, "key10": 10}
value = my_dict["key5"]
print(value)
2. Check for membership: How would you check if "key12" exists in my_dict = {"key1": 10, "key2": 20, "key3": 30, "key4": 40, "key5": 50, "key6":
60, "key7": 70, "key8": 80, "key9": 90, "key10": 100}?
https://colab.research.google.com/drive/1TUXZEQ-i4KOSQaSWSgV9IraTVtpFJgrw#scrollTo=PquWvgq_MFXJ&printMode=true 4/5
11/17/24, 11:40 PM PROG301.ipynb - Colab
my_dict = {"key1": 10, "key2": 20, "key3": 30, "key4": 40, "key5": 50, "key6": 60, "key7": 70, "key8": 80, "key9": 90, "key10": 100}
if "key12" in my_dict:
print("key12 exists")
else:
print("key12 does not exist")
3. Add an item to a dictionary: How can you add {"key11": 110} to my_dict = {"key1": 10, "key2": 20, "key3": 30, "key4": 40, "key5": 50, "key6": 60,
"key7": 70, "key8": 80, "key9": 90, "key10": 100}?
my_dict = {"key1": 10, "key2": 20, "key3": 30, "key4": 40, "key5": 50, "key6": 60, "key7": 70, "key8": 80, "key9": 90, "key10": 100}
my_dict["key11"] = 110
print(my_dict)
{'key1': 10, 'key2': 20, 'key3': 30, 'key4': 40, 'key5': 50, 'key6': 60, 'key7': 70, 'key8': 80, 'key9': 90, 'key10': 100, 'key11': 110}
4. Update a value in a dictionary: How can you change the value of "key5" from 50 to 500 in my_dict = {"key1": 10, "key2": 20, "key3": 30,
"key4": 40, "key5": 50, "key6": 60, "key7": 70, "key8": 80, "key9": 90, "key10": 100}?
my_dict = {"key1": 10, "key2": 20, "key3": 30, "key4": 40, "key5": 50, "key6": 60, "key7": 70, "key8": 80, "key9": 90, "key10": 100}
my_dict["key5"] = 500
print(my_dict)
{'key1': 10, 'key2': 20, 'key3': 30, 'key4': 40, 'key5': 500, 'key6': 60, 'key7': 70, 'key8': 80, 'key9': 90, 'key10': 100}
5. Iterate through dictionary items: How can you iterate over keys and values in my_dict = {"key1": "A", "key2": "B", "key3": "C", "key4": "D",
"key5": "E", "key6": "F", "key7": "G", "key8": "H", "key9": "I", "key10": "J"}?
my_dict = {
"key1": "A", "key2": "B", "key3": "C", "key4": "D", "key5": "E",
"key6": "F", "key7": "G", "key8": "H", "key9": "I", "key10": "J"
}
for key, value in my_dict.items():
print(f"{key}: {value}")
key1: A
key2: B
key3: C
key4: D
key5: E
key6: F
key7: G
key8: H
key9: I
key10: J
https://colab.research.google.com/drive/1TUXZEQ-i4KOSQaSWSgV9IraTVtpFJgrw#scrollTo=PquWvgq_MFXJ&printMode=true 5/5