You are on page 1of 4

Computer Programming Skills (Lab 5)

1. Answer to the following questions by T/F

1 Tuples are enclosed in parentheses. T


2 The elements of dictionaries are ordered. F
3 The elements of tuples are ordered. T
4 Tuples are mutable. F
5 Lists can contain all kinds of things, even other lists. T
6 List methods do change the original list T
7 sort or reverse are two tuple methods F
8 To convert an object into a tuple, use tuple. T
9 Lists are generally faster than tuples. F
10 A dictionary is a more general version of a list. T

2. What happens if you attempt to access an element of a list using a


negative index?
Negative indices count backwards from the end of the list.

3. Given the statement lst = [10, -4, 11, 29]


a. What expression represents the first element of lst? lst[0]
b. What expression represents the last element of lst? lst[-1]
c. What is lst[3]? 29
d. What is lst[-4]? 10
e. Is the expression lst[3.0] legal or illegal? It is illegal, the following error will
appear:
“TypeError: list indices must be integers or slices, not float”
4. Given the list lst = [20, 1, -34, 40, -8, 60, 1, 3] evaluate the following
expressions:
a. Lst
[20, 1, -34, 40, -8, 60, 1, 3]
b. lst[0:3] lst = [20, 1, -34, 40, -8, 60, 1, 3]

[20, 1, -34] print(lst)

c. lst[-5:-3] print(lst[0:3] )

[40, -8] print(lst[-5:-3])


print(lst[4:])
d. lst[4:]
print(lst[:] )
[-8, 60, 1, 3]
print(lst[5:1:-2] )
e. lst[:]
print(lst[1:5:-2])
[20, 1, -34, 40, -8, 60, 1, 3]
lst.append(12)
f. lst[5:1:-2]
print(lst)
[60, 40]
#lst.append(x)
g. lst[1:5:-2] print(lst)
[] lst.insert(2, 44)
h. lst.append(12) print(lst)
[20, 1, -34, 40, -8, 60, 1, 3, 12] print(len(lst))

i. lst.append(x)
NameError: name 'x' is not defined
j. lst.insert(2, 44)
[20, 1, 44, -34, 40, -8, 60, 1, 3, 12]
k. len(lst)
10
5. What happens when an executing program attempts to retrieve a value using a
key that is not present in the dictionary?
Key error: will appear
6. What happens when an executing program attempts to associate a value with a
key that is not present in the dictionary?
It will add that key with its relevant value to the dictionary.
7. Write a Python code to add a key to a dictionary.
Sample Dictionary: {0: 10, 1: 20}
Expected Result: {0: 10, 1: 20, 2: 30}
d[2] = 30
d = {0: 10, 1: 20}
d[2] = 30
print(d)
Computer Programming Skills (Lab 5 – Extra )
1. Write a python statement to create a list called G. The list contains the
following
G = [90, 100, 60, 70, 40, 50 , 60]
student grades: 90, 100, 60, 70, 40, 50 and 60
G.sort()
G = [90, 100, 60, 70, 40, 50 , 60]
print(G)
2. Use a python method to sort the elements of G. F = G.index(60)
Print the elements of G.
print(F)
G.sort()
T = len(G)
print(G)--- will print [40, 50, 60, 60, 70, 90, 100]
print(T)
3. Create a new variable F and use a python P=T-F
method to assign the first index of the grade 60 to
the variable F. Print the value of F. print(P)
print(G[F:])
F = G.index(60)
V = sum(G)/len(G)
print(F)---will print 2
print(V)
4. Create a new variable T and use a python
function to assign the number of
items in the list G to the variable T. Print the value of T.
T = len(G)
print(T)--- will print 7
5. Print the number of passed students (students whose have grades greater or
equal than 60) in G.
P=T-F
print(P) --- will print 5
6. Print grades of passed students.
print(G[F:]) will print [60, 60, 70, 90, 100]
7. Use suitable python functions to print the average of grades of all students.
V = sum(G)/len(G)
print(V)

You might also like