0% found this document useful (0 votes)
8 views3 pages

Lab 1 S24

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Lab 1 S24

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP 8347: Internet Applications and Distributes Systems

SUMMER 2024 LAB #1

PREETHA SIVAKUMAR 110127240

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]

1.2 - Work with list methods and data types:


Type python commands to do the following:
a) append the string 'university' to list1- list1.append(“university”)
b) remove the last element of list2 – del list2[7]
c) insert the item 100 at index 5 in list1- list1.insert( 5, 100)
d) add the integers (together) in the list [44, 50] at the end of list2- list2.append([44,50])

Part 2 - Strings in Python: Given the following two strings:


str1 = "Django allows a rapid web development and creates scalable systems"
str2 = "There are two areas in cloud computing: performance and security"

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].

1 * means after update


COMP 8347: Internet Applications and Distributes Systems
SUMMER 2024 LAB #1

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}

#dictionary using sequences


d2 = dict([("name","Livy"), ('age', 44), ((1, 3, 5), ['a', 'b', 'c']), (0, 'black'), (33, 67)])
#dictionary using keywords
d3 = dict(id=2277, name='Michael', siblings=['Janet', 'Martin', 'Richard'])

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

2 * means after update


COMP 8347: Internet Applications and Distributes Systems
SUMMER 2024 LAB #1

f) d3.get('name', 'Tim') ‘Michael’


d2.items() dict_items([("name","Livy"), ('age', 44), ((1, 3, 5), ['a', 'b', 'c']), (0, 'black'), (33,
67)])
g) d3['siblings'] - =['Janet', 'Martin', 'Richard']
h) d2['siblings'] - KeyError
i) d2.update(d3) {"name","Michael"), 'age', 44, (1, 3, 5), ['a', 'b', 'c']), 0, 'black', (33,
67),id”2277,’siblings: ['Janet', 'Martin', 'Richard']}
j) d2[0] “black”
k) d1.get((1,2))- no result
l) d2['siblings']* - ['Janet', 'Martin', 'Richard']
m) d2['name']* - “Michael”
n) d1 == d2 False
o) len(d2) 7
p) for key in d1.keys():
print(key)
name
age
(4, 10)
+1
44
19
q) for key in d2.keys():
print(d2[key])
Livy
44
['a', 'b', 'c']
black
67

3 * means after update

You might also like