Python Exercise 2

Mannan Ul Haq
0

Objective:

  • Python Data Structures

Activity 1:

1. Create a list named `fruits` and initialize it with values `["apple", "banana", "cherry", "date"]`.
2. Add "elderberry" and "fig" to the list.
3. Remove "banana" from the list.
4. Convert this list into a tuple.

Solution:

# 1. Create a list named fruits and initialize it with values ["apple", "banana", "cherry", "date"].
fruits = ["apple", "banana", "cherry", "date"]

# 2. Add "elderberry" and "fig" to the list.
fruits.extend(["elderberry", "fig"])

# 3. Remove "banana" from the list.
fruits.remove("banana")

# 4. Convert this list into a tuple.
fruits_tuple = tuple(fruits)



Activity 2:

1. Given a tuple: colors = ("red", "green", "blue")
2. Convert this tuple into a list.
3. Add "yellow" and "violet" to the list.
4. Convert it back to a tuple.

Solution:

# 1. Given a tuple: colors = ("red", "green", "blue")
colors = ("red", "green", "blue")

# 2. Convert this tuple into a list.
colors_list = list(colors)

# 3. Add "yellow" and "violet" to the list.
colors_list.extend(["yellow", "violet"])

# 4. Convert it back to a tuple.
colors = tuple(colors_list)



Activity 3:

1. Create two sets: set1 = {1, 2, 3, 4} and set2 = {3, 4, 5, 6}
2. Find the union, intersection, and difference of these two sets.

Solution:

# 1. Create two sets: set1 = {1, 2, 3, 4} and set2 = {3, 4, 5, 6}
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# 2. Find the union, intersection, and difference of these two sets.
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
difference_set = set1.difference(set2)



Activity 4:

1. Given a set: numbers = {1, 3, 5, 7, 9, 11}

2. Remove the number 3 from the set.

3. Add numbers 2 and 4 to the set.


Solution:

# 1. Given a set: numbers = {1, 3, 5, 7, 9, 11}
numbers = {1, 3, 5, 7, 9, 11}

# 2. Remove the number 3 from the set.
numbers.remove(3)

# 3. Add numbers 2 and 4 to the set.
numbers.update({2, 4})


Activity 5:

1. Create a dictionary named student with keys "name", "age", and "grade" and their respective values "John", 15, and "A+".
2. Add a key "school" with the value "Greenwood High".
3. Remove the key-value pair with the key "age".

Solution:

# 1. Create a dictionary named student with keys "name", "age", and "grade" and their respective values "John", 15, and "A+".
student = {"name": "John", "age": 15, "grade": "A+"}

# 2. Add a key "school" with the value "Greenwood High".
student["school"] = "Greenwood High"

# 3. Remove the key-value pair with the key "age".
del student["age"]



Activity 6: 

1. Given a dictionary: car = {"brand": "Toyota", "model": "Corolla", "year": 2020}
2. Change the "year" to 2021.
3. Print all the keys in the dictionary.
4. Print all the values in the dictionary.

Solution:

# 1. Given a dictionary: car = {"brand": "Toyota", "model": "Corolla", "year": 2020}
car = {"brand": "Toyota", "model": "Corolla", "year": 2020}

# 2. Change the "year" to 2021.
car["year"] = 2021

# 3. Print all the keys in the dictionary.
print(car.keys())

# 4. Print all the values in the dictionary.
print(car.values())


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !