Assignment 3 Lists: Questions
Assignment 3 Lists: Questions
Lists
Questions
1.
1.
1.
1. Define List. Explain the different ways of
creating list with an example
2. Differentiate between string and list w.r.t
mutability
3. Explain following methods w.r.t list (with
example)
append
extend
sort
pop
del
remove
4. Explain the following functions w.r.t list
len
max
min
sum
5. Justify why lists are equivalent but not
identical
6. Write a note on list aliasing
7. Distinguish between the operations that
modify the lists and operations that create a new list.
8. List and explain the pitfalls and ways to avoid
the mistakes that can happen with list
Exercise
1.
1. Write a python program to remove duplicates from List
2. Write a function called as is_anagram that take two
strings and returns true if they are anagrams, using list.
Note: Two words are said to be anagrams if you can
rearrange the letters from one to spell the other
3. Write a program that will store the schedule for a given
day for a particular TV station. The program should ask you
for the name of the station and the day of the week before
asking you for the name of each show and the start and stop
times. Once the schedule is complete it should be displayed
as a table.
4. Write a python program to find the longest word in a
given sentence
5. [“www.zframez.com”, “www.wikipedia.org”,
“www.asp.net”, “www.abcd.in”]Write a python program to
print website suffixes (com , org , net ,in) from this list
Dictionaries
Questions
1.
1. Define dictionary. Explain different ways to create
dictionary variable with an example.
2. Explain following commands w.r.t dictionary with an
example
in
len
keys
3. Differentiate two ways of dictionary as a set of counters
with an example
4. Explain working of for loop w.r.t dictionary with an
example
Exercises
1. Wikipedia has a list of the tallest mountains in the
world, with each mountain’s elevation. Pick five mountains
from this list.
Create a dictionary with the mountain names as
keys, and the elevations as values.
Print out just the mountains’ names, by looping
through the keys of your dictionary.
Print out just the mountains’ elevations, by looping
through the values of your dictionary.
Print out a series of statements telling how tall
each mountain is: “Everest is 8848 meters tall.”
Display the information in alphabetical order by
each mountain’s name.
2. Given the following dictionary:inventory = {
‘gold’ : 500,
‘pouch’ : [‘flint’, ‘twine’, ‘gemstone’],
‘backpack’ : [‘xylophone’,’dagger’, ‘bedroll’,’bread loaf’]
}
Try to do the followings:
Add a key to inventory called ‘pocket’.
Set the value of ‘pocket’ to be a list consisting of
the strings ‘seashell’, ‘strange berry’, and ‘lint’.
sort()the items in the list stored under the
‘backpack’ key.
Then .remove(‘dagger’) from the list of items
stored under the ‘backpack’ key.
Add 50 to the number stored under the ‘gold’ key.
3. Try to follow the steps:
Create three dictionaries: lloyd, alice, and tyler.
Give each dictionary the keys “name”,
“homework”, “quizzes”, and “tests”.Have the “name” key
be the name of the student (that is, lloyd’s name should be
“Lloyd”) and the other keys should be an empty list. Look
in solutions, the “solution 1”. Chechk if you have done it
rigth.
Now copy this code:
lloyd = {
“name”: “Lloyd”,
“homework”: [90.0,97.0,75.0,92.0],
“quizzes”: [88.0,40.0,94.0],
“tests”: [75.0,90.0]
}
alice = {
“name”: “Alice”,
“homework”: [100.0, 92.0, 98.0, 100.0],
“quizzes”: [82.0, 83.0, 91.0],
“tests”: [89.0, 97.0]
}
tyler = {
“name”: “Tyler”,
“homework”: [0.0, 87.0, 75.0, 22.0],
“quizzes”: [0.0, 75.0, 78.0],
“tests”: [100.0, 100.0]
}
Below your code, create a list called studentsthat
contains lloyd, alice, and `tyler.
for each student in your students list, print out that
student’s data, as follows:print the student’s name
print the student’s homework
print the student’s quizzes
print the student’s tests
Write a function average that takes a list of numbers and
returns the average.
Define a function called average that has one
argument, numbers.
Inside that function, call the built-in sum() function with
the numbers list as a parameter. Store the result in a
variable called total.
Use float() to convert total and store the result in total.
Divide total by the length of the numbers list. Use the
built-in len() function to calculate that.
Return that result.
Write a function called get_average that takes a student
dictionary (like lloyd, alice, or tyler) as input and returns
his/her weighted average.
Define a function called get_average that takes one
argument called student.
Make a variable homework that stores the
average() of student[“homework”].
Repeat step 2 for “quizzes” and “tests”.
Multiply the 3 averages by their weights and return the
sum of those three. Homework is 10%, quizzes are 30%
and tests are 60%.
Define a new function called get_letter_grade that
has one argument called score. Expect score to be a
number.
Inside your function, test score using a chain of
if: / elif: / else: statements, like so:If score is 90 or above:
return “A”
Else if score is 80 or above: return “B”
Else if score is 70 or above: return “C”
Else if score is 60 or above: return “D”
Otherwise: return “F”
Finally, test your function. Call your get_letter_grade
function with the result of get_average(lloyd). Print the
resulting letter grade.
Define a function called get_class_average that has
one argument, students. You can expect students to be a
list containing your three students.
First, make an empty list called results.
For each student item in the class list, calculate
get_average(student) and then call results.append() with
that result.
Finally, return the result of calling average() with
results.
Finally, print out the result of calling
get_class_averagewith your students list. Your students
should be [lloyd, alice, tyler].
Then, print the result of get_letter_grade for the
class’s average.
Tupples
Questions
1.
1. Explain different ways of creating tuple with an
example
2. Explain behaviour of tupple w.r.t comparision operators
with an example
3. Explain DSU pattern with an example