PW2 python
PW2 python
Practical Work 02
Iterate over the list and print each number 4. Given a list: [1, 2, 2, 3, 4, 4, 5, 6, 6, 6]
multiplied by 2.
Check if the number 5 is in the list. Convert the list to a set to remove duplicates.
Find the sum of all numbers in the list. Convert the set back to a list and print it.
4. Create a list of numbers: 5. Given two sentences: "the quick brown fox
[12, 4, 56, 17, 8, 34, 1]. jumps over the lazy dog" and "the fast
black dog runs quickly"
Sort the list in ascending order.
Sort the list in descending order. Convert both sentences into sets of words.
Find the common words (intersection) between
5. Given a list of numbers: [1, 2, 3, 4, 5] the two sentences.
Find the words that are unique to the first
Write a program that creates a new list where sentence (difference).
each element at index i is the cumulative sum Find the words that appear in either sentence but
of elements from the original list up to index i. not both (symmetric difference).
Example: [1, 3, 6, 10, 15]
6. Given a list of integers from 1 to 10, but with
some missing: [1, 2, 4, 5, 7, 8, 10]
Page 1 sur 2
Write a program that uses a set to find the 2. Use the people dictionary from the previous
missing numbers. question:
5. Given a list of tuples where each tuple contains a Write a program that creates a dictionary where
string and a number: the keys are the words and the values are the
length of each word.
data = [('apple', 3), ('banana', 1),
('cherry', 2)]
7. Given a list of names: ["Alice", "Bob",
"Charlie", "Anna", "David", "Brian"]
Sort the list of tuples by the second element (the
number). Write a program that groups the names by their
Print the sorted list. first letter and stores them in a dictionary.
Exercise4. Dictionaries Example : {'A': ['Alice', 'Anna'],……
1. Create a dictionary that stores the names and ages 8. Create a dictionary that stores the names and grades
of three people: of students in a class. The keys should be student
names Alice, Bob and Charlie, and the values
people = {'Alice': 25, 'Bob': 30, 'Charlie':
should be dictionaries containing their grades for
35}
three subjects: Math, Science, English.
grades = {
Print Alice’s age. 'Alice': {'Math': 90, ……} , ….
Add a new entry for David, age 40.
Print Bob’s Science grade.
Change Bob’s age to 31. Calculate the average grade for Charlie.
Remove Charlie from the dictionary.
Print the dictionary’s keys and values.
Page 2 sur 2