Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views5 pages

Abdulrhman Aljifri, Lab5 With Extra

1. The document discusses computer programming skills related to lists, tuples, dictionaries in Python. It provides examples of accessing elements, using indices, built-in methods, and common operations on these data structures. 2. Negative indices can be used to access elements from the end of a list. Attempting to use a float as a list index results in an error. 3. Key errors occur when accessing non-existent keys in a dictionary, while new keys can be added along with their values.

Uploaded by

special games
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)
13 views5 pages

Abdulrhman Aljifri, Lab5 With Extra

1. The document discusses computer programming skills related to lists, tuples, dictionaries in Python. It provides examples of accessing elements, using indices, built-in methods, and common operations on these data structures. 2. Negative indices can be used to access elements from the end of a list. Attempting to use a float as a list index results in an error. 3. Key errors occur when accessing non-existent keys in a dictionary, while new keys can be added along with their values.

Uploaded by

special games
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
You are on page 1/ 5

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]
[20, 1, -34]
c. lst[-5:-3]
[40, -8]
d. lst[4:]
[-8, 60, 1, 3]
e. lst[:]
[20, 1, -34, 40, -8, 60, 1, 3]
f. lst[5:1:-2]
[60, 40]
g. lst[1:5:-2]
[]
h. lst.append(12)
[20, 1, -34, 40, -8, 60, 1, 3, 12]
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
student grades: 90, 100, 60, 70, 40, 50 and 60
G = [90, 100, 60, 70, 40, 50 , 60]
2. Use a python method to sort the elements of G.
Print the elements of G.
G.sort()
print(G)--- will print [40, 50, 60, 60, 70, 90, 100]
3. Create a new variable F and use a python
method to assign the first index of the grade 60 to
the variable F. Print the value of F.
F = G.index(60)
print(F)---will print 2
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