Professional Documents
Culture Documents
c. lst[-5:-3] print(lst[0:3] )
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)