0% found this document useful (0 votes)
43 views4 pages

Python Day (4) Lab - Part

The document outlines several Python programming tasks, including creating a program for adding and removing items from a list, functions for processing distinct numbers and finding the longest strings in a list, and a program to track unique words input by the user. It also mentions a reading task related to Python programming topics and the tracking of progress through a Google sheet. Each task includes sample outputs to illustrate expected results.

Uploaded by

modyesam25
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)
43 views4 pages

Python Day (4) Lab - Part

The document outlines several Python programming tasks, including creating a program for adding and removing items from a list, functions for processing distinct numbers and finding the longest strings in a list, and a program to track unique words input by the user. It also mentions a reading task related to Python programming topics and the tracking of progress through a Google sheet. Each task includes sample outputs to illustrate expected results.

Uploaded by

modyesam25
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

Python Lab (4) – Part (1)

Addition and Removal


Please write a program which asks the user to choose between addition and removal.
Depending on the choice, the program adds an item to or removes an item from the end of
a list. The item that is added must always be one greater than the last item in the list. The
first item to be added must be 1.
The list is printed out in the beginning and after each operation. Have a look at the example
execution below:
The list is now []

a(d)d, (r)emove or e(x)it: d


The list is now [1]

a(d)d, (r)emove or e(x)it: d

The list is now [1, 2]

a(d)d, (r)emove or e(x)it: d

The list is now [1, 2, 3]

a(d)d, (r)emove or e(x)it: r

The list is now [1, 2]


a(d)d, (r)emove or e(x)it: d

The list is now [1, 2, 3]

a(d)d, (r)emove or e(x)it: x

Bye!

Note: You may assume that, if the list is empty, there will not be an attempt to remove
items.
Distinct Numbers
Please write a function named distinct_numbers, which takes a list of integers as its
argument. The function returns a new list containing the numbers from the original list in
order of magnitude, and so that each distinct number is present only once.
Sample Output:
my_list = [3, 2, 2, 1, 3, 3, 1]
print(distinct_numbers(my_list)) # [1, 2, 3]

The length of the longest in the list


Please write a function named length_of_longest, which takes a list of strings as its
argument. The function returns the length of the longest string.
my_list = ["first", "second", "fourth", "eleventh"]
result = length_of_longest(my_list)
print(result) # 8

my_list = ["adele", "mark", "dorothy", "tim", "hedy", "richard"]


result = length_of_longest(my_list)
print(result) # 7
All the longest in the list
Please write a function named all_the_longest, which takes a list of strings as its argument.
The function should return a new list containing the longest string in the original list. If
more than one are equally long, the function should return all of the longest strings.
The order of the strings in the returned list should be the same as in the original.
my_list = ["first", "second", "fourth", "eleventh"]
result = all_the_longest(my_list)
print(result) # ['eleventh']

my_list = ["adele", "mark", "dorothy", "tim", "hedy", "richard"]


result = all_the_longest(my_list)
print(result) # ['dorothy', 'richard']

Same Word Twice


Please write a program which asks the user for words. If the user types in a word for the
second time, the program should print out the number of different words typed in, and exit.
Sample Output:
Word: once
Word: upon
Word: a
Word: time
Word: upon
You typed in 4 different words
Reading Task
- Defining functions - Python Programming MOOC 2024
- More functions - Python Programming MOOC 2024
- Lists - Python Programming MOOC 2024
- Definite iteration - Python Programming MOOC 2024
I will send you a google sheet and I will track your progress through it:
• Check the box if you finished reading each of the previous topics.
• Write the new idea/information you learned from reading these topics.

You might also like