Lab 1 S24
Lab 1 S24
NOTE: Use Python’s IDLE interactive tool. Write your answer beside each command in this sheet in bold.
Part 1 - Lists in Python: Given the following two lists:
list1 = ["apple", 10, 3.14, [1, 2, 3], "class", 20, [4.5, 6.7], 5.5]
list2 = [8, "list in python", [9.1, 7.2], 15, "MAC", [2, 4, 6], 3.33, 12.5]
1.1 - Work with list indexing and slicing:
Indicate the results if you type the following commands in IDLE:
a) list1[2][1]- error
b) list2[3][0] - error
c) list1[4][2][1] - error
d) len(list2) - 8
e) list1[12]- Index Error
f) list2[-4:-1]- ["MAC", [2, 4, 6], 3.33]
g) list1[2:14] – [3.14, [1, 2, 3], "class", 20, [4.5, 6.7], 5.5]
list2+list1 - = [8, "list in python", [9.1, 7.2], 15, "MAC", [2, 4, 6], 3.33, 12.5, "apple", 10, 3.14, [1, 2,
3], "class", 20, [4.5, 6.7], 5.5]
h) list1*2- ["apple", 10, 3.14, [1, 2, 3], "class", 20, [4.5, 6.7], 5.5, "apple", 10, 3.14, [1, 2, 3], "class",
20, [4.5, 6.7], 5.5]
i) list2[5][1] = 0 – False
j) del list1[-3] - [“apple”, 10, 3.14,[1,2,3],”class”,[4.5,6.7],5.5]
2.1 - Work with string indexing, slicing, assignment, and concatenation: Indicate the results if
you type the following commands in IDLE. Indicate the reason for each answer. Ex. The answer is ‘o’
because o is at index [7].
a) str2[-1:-6:-1] “ytiru”, because from the first element from the end, then sliced from
the -1 to -6 character.
b) str1[9] “l” because it the 9th character in the string.
c) str2[-2:] “ty” because from the end the second element to the end.
d) str2[0:20:3] “Tra ors” because from the end, starting at 0 to 20 index, take every third
character.
e) str1+" "+str2 “Django allows a rapid web development and creates scalable
systemsThere are two areas in cloud computing: performance and security”, because
it adds the two strings.
2.2 - Work with string methods: Use str methods to do the following and indicate the corresponding
results.
a) Check if the string str1 ends with the word 'systems' True
b) Return a list of words from str2
[‘There’,’are’,‘two’,’areas’,’in’,’cloud’,’computing’,’performance’,’and’,’security’]
c) Convert str1 and str2 to all uppercase letters
“DJANGO ALLOWS A RAPID WEB DEVELOPMENT AND CREATES SCALALABLE
SYSTEMS”
“THERE ARE TWO AREAS IN CLOUD COMPUTING PERFORMANCE AND
SECURITY”
d) Replace the string 'web' of str1 with an empty string
“Django allows a rapid development and creates scalable systems”
e) Count the number of times ‘e’ occurs in str2 -7
Part 3- Dictionary in Python: Define the following dicts:
#dictionary literals
d1={"name": "Bob", "age": 35, (4, 10):['x', 'y', 'z'], '+1' : "Canada", 44: 99, 19:555}
Work with dict methods: Type the following commands at the Python prompt in IDLE interactive
mode and indicate the result of each command:
a) d1.keys() dict_keys([‘name,’age’,(4,10),”+1’,44,19])
b) d2.values() dict_values[‘Livy’,44, ['a', 'b', 'c'], "black", 67]
c) d3.get('id') 2277
d) d2.get('age') 44
e) d3.get('age') – no result