0% found this document useful (0 votes)
21 views

PW2 python

Uploaded by

Aya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

PW2 python

Uploaded by

Aya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Ministry of Higher Education and Scientific Research

University of Oum El Bouaghi


Faculty of Exact Sciences and Natural and Life Sciences
Department of Mathematics and Computer Science

Advanced Python Language


1st Master - AI & Data Science

Practical Work 02

Exercise1. Lists Exercise2. Sets

1. Create a list of the following numbers: 1. Create two sets:


5, 10, 15, 20, 25, 30, 35, 40.
 set1 = {1, 2, 3, 4, 5}
 Print the list.  set2 = {4, 5, 6, 7, 8}
 Add 45 to the end of the list.
 Remove 10 from the list. 2. Perform the following operations:
 Change the second element to 50.
 Print the length of the list.  Find the union of set1 and set2.
 Find the intersection of set1 and set2.
2. Create a list of the first 10 alphabet letters:  Find the difference between set1 and set2 (i.e.,
['a', 'b', 'c', ..., 'j']. elements in set1 but not in set2).
 Add 9 to set1 and remove 2 from set1.
 Print the first three letters.
 Print the last two letters. 3. Create a set of numbers: {10, 20, 30, 40, 50}.
 Print the list in reverse order.
 Check if 20 is in the set.
3. Create a list of numbers from 1 to 10.  Check if 60 is in the set.

 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:

Exercise3. Tuples  Check if the name 'Eve' is in the


dictionary.
1. Create a tuple: (10, 20, 30, 40, 50).  Check if 'Alice' is in the dictionary.

 Print the first element of the tuple. 3. Given the dictionary:


 Print the last element of the tuple. fruits = {'apple': 3, 'banana': 5,
'orange': 2}
 Check if 30 is in the tuple.
 Try to change the second element to 25 (and
 Print each fruit and its quantity.
observe what happens).
 Increase the quantity of each fruit by 1 and
 print the updated dictionary.
2. Create a tuple: (100, 200, 300).
4. Create two dictionaries:
 Assign each value to a separate variable and
print them. dict1 = {'x': 1, 'y': 2}
dict2 = {'y': 3, 'z': 4}
3. Create a tuple: (5, 10, 15, 20, 25, 30, 35).
 Combine the two dictionaries into a single
 Print the first three elements. dictionary.
 Print the last two elements. 5. Given a list of elements: ['apple', 'banana',
 Print the tuple in reverse order. 'apple', 'orange', 'banana', 'apple']

4. Given a tuple of characters:  Use a dictionary to count how many times


('H', 'e', 'l', 'l', 'o')
each fruit appears in the list.
 Write a program that converts this tuple into a 6. Given a list of words: ["world", "sun",
string and prints it as "Hello". "wind", "python"]

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy