You are on page 1of 12

Lab Assignment 02 (Python) Due: 09.02.

2024

Exercises:

1. Write a Python program to multiply all the items in a list.


My work:
Python Code:

Description:
This program defines a function multiply_list() that takes a list of numbers as an argument. It initializes a variable
total of 1. Then, it iterates through each item in the list using a for loop and multiplies the current total by the
current item. Finally, it returns the total product.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex-1-Assignment-2
Output:

2. Write a Python program to get the biggest and smallest number from a list. (For this one you can make a
function and return a tuple with the max and min number from the list)
My work:
Python Code:

Description:
This program defines a function find_max_min() that takes a list of numbers as an argument. It uses the built-in
Python functions max() and min() to find the maximum and minimum numbers in the list. The function returns
these two numbers as a tuple.In the main part of the program, we create a list called numbers and pass it to the
find_max_min() function. The function returns a tuple with the max and min number from the list. We then
unpack this tuple into two separate variables, max_num, and min_num, and print the values.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex-2-Assignment-2#main.py
Output:

3. Write a Python program to remove duplicates from a list.

My work:
Python Code:
Description:
This program defines a function remove_duplicates_ordered() that takes a list as an argument. It initializes an
empty list called output_list. It then iterates through each item in the input list using a for loop. If the current item
is not already in output_list, it appends the current item to output_list. Finally, it returns output_list.
In the main part of the program, we create a list called numbers and pass it to the remove_duplicates_ordered()
function. The result is then printed out.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex3-Assignment-2#main.py
Output:

4. Write a Python program to find the list of words that are longer than n from a given list of words.

My work:
Python Code:

Description:
This program defines a function find_long_words() that takes a list of words and an integer n as arguments. It
uses list comprehension to create a new list containing only the words longer than n characters.In the main part
of the program, we create a list called words and pass it to the find_long_words() function along with the
integer 4. The function returns a list of words that are longer than 4 characters. We then print the result.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex-4-Assignment-2
Output:

5. Write a Python program which gets as an input a list with email address
and returns a list with people’s usernames (Ex: nakomegi@gmail.com will get
only the part before @, so only “nakomegi”. Be careful there won’t be only one email.)
a. Imagine all emails in the list are in the domain of @gmail.comb.
b.Imagine if emails have different domains
(in which case can we use slicing, if possible, you have to use it?)

My work:
Python Code:

Description:
This program defines a function extract_usernames() that takes a list of email addresses as an argument. It uses
a list comprehension to create a new list that contains the usernames extracted from each email address.
The list comprehension iterates through each email address in the input list and extracts the username using
string slicing. Specifically, it uses the index() method to find the position of the @ symbol in the email address
and then uses slicing to extract the substring before the @ symbol.
In the main part of the program, we create a list called email_list and pass it to the extract_usernames()
function. The function returns a list of usernames. We then print the result.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex-5-Assignment-2#main.py
Output:

6. Write a Python Program that has three lists: Weather=[‘Sunny’,’Windy’,’Snowy’,’Rainy’,’Warm’]


Place=[‘Epoka’,’Tirana’,’Korca’,’Qyteza’] Feeling = [‘Bored’,’Happy’,’Sad’,’Excited’]
Define a function that will choose a random element from and for each list. After the function has selected
one word from each list, it will make a sentence with those. (Ex: Sunny, Korca, Excited are the selected
words, the sentence might have the following structure : It is Sunny in Korca and I am Excited)

My work:
Python Code:

Visit the link to view the work:


https://replit.com/@RigiSpahi/Ex-6-Assignment
Output:
Description:
This program defines a function generate_sentence() that takes three lists as arguments: weather_list, place_list,
and feeling_list. The function uses the random.choice() function to select a random element from each list. It then
constructs a sentence using the selected elements and returns the sentence as a string.
In the main part of the program, we define three lists Weather, Place, and Feeling, and pass them to the
generate_sentence() function. The function returns a sentence that incorporates a random element from each list.
We then print the result.
7. Write a Python program to unpack a tuple into several variables.
My work:
Python Code:

Description: This code is creating a tuple called 'tup' with the values (1, 2, 3). Then it's unpacking the values
of the tuple into three separate variables, var1, var2, and var3. Finally, it prints the values of var1, var2, and
var3.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex-7-Assignment-2

Output:

8. Write a Python program to convert a tuple to a string.


My work:
Python Code:
Description: This code defines a convert_tuple_to_string function that takes a tuple as an argument and
returns a string that is the concatenation of the elements in the tuple. The function uses the join() method of
a String object to join the elements in the tuple. The join() method takes a sequence (such as a list or a
tuple) as an argument and returns a string that is the concatenation of the strings in the sequence.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex-8-Assignment-2
Output:

9. Write a Python program to check whether an element exists within a tuple.


My work:
Python Code:

Description: This Python program checks whether an element exists within a tuple. It takes an element as
input from the user and checks if the element is present in the tuple. If the element is found in the tuple, it prints
"Element exists in the tuple". Otherwise, it prints "Element does not exist in the tuple".

Visit the link to view the work:


https://replit.com/@RigiSpahi/Ex9-Assignment
Output (Example with number 10.- 10 does not exist in the tuple) :
10. Write a Python program to find repeated items in a tuple.
My work:
Python Code:

Description: This code takes a tuple A and counts the frequency of occurrence of each element in the tuple. It
uses a dictionary called repeated_word to store the count of each element. Then, it iterates over the dictionary and
prints out the elements that occur more than once.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex10-Assignment-2
Output:

11. Write a python program that takes the list you created during the
lesson that had 54 tuples representing a deck of playing cards, shuffles it
and then divides the shuffled tuples into 4 different lists. The cards can be
divided as such: the first element of the list will go to the first list, the
second will go to the second one and so on. For this exercise you have to
use the shuffle method in lists. To use this method you have to import the
random library. Here is an example of the usage:
import random
mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist)
print(mylist)
Output:
cherry apple banana
*Note: Since the shuffle method is a random method the probability of 2 exact same
shuffles for 54 elements is very slim so please don’t copy the exercise from each other.
My work:
Python Code:

Description: This code creates a deck of towns and villages of Albania and Italy, shuffles the deck, and then
deals the shuffled deck into four hands.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex11-Assignment-2#main.py
Output:
12. Convert two lists into a dictionary (declare two lists and create a dictionary)
My work:
Python Code:

Description: The code creates a dictionary word_dict_with_explanation by zipping the albanian_words list
with the word_explanations list. Then it prints the resulting dictionary.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex12-Assignment-2
Output:

13. Print the value of key ‘history’ from the below:


sampleDict = {
"class": {
"student": {
"name": "Mike",
"marks": {
"physics": 70,
"history": 80
}}}}

My work:
Python Code:
Description: This will return the value associated with the key 'history' in the 'marks' dictionary, which
is 80. We can then print this value using the print() function.
Visit the link to view the work:
https://replit.com/@RigiSpahi/Ex13-Assignment-2
Output:

14. BONUS: Create a dictionary which will contain english and albanian words. Then make a function
which randomly will choose 4 words from english words and pass them to another function which
will translate those 4 words in albanian through the dictionary
My work:
Python Code:
Description: The code creates a dictionary of words and their translations from English to Albanian.
Then it defines a function 'translate_word' to look up and return the translation of a given word.
It also contains a while loop that prompts the user to enter a word from the list of code to translate and then
prints the translation if it is found in the dictionary. The loop continues until the user enters 'exit'.

Visit the link to view the work:


https://replit.com/@RigiSpahi/Ex14-Assignment-2
Output:

WORKED: ERIKA SPAHI

You might also like