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

0% found this document useful (0 votes)
7 views4 pages

Lab 1 Outputs

The document contains various Python code snippets demonstrating the use of lists, strings, and dictionaries. It includes examples of indexing, slicing, modifying, and combining lists, as well as string manipulation methods and dictionary operations. Additionally, it showcases error handling for invalid operations on these data structures.

Uploaded by

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

Lab 1 Outputs

The document contains various Python code snippets demonstrating the use of lists, strings, and dictionaries. It includes examples of indexing, slicing, modifying, and combining lists, as well as string manipulation methods and dictionary operations. Additionally, it showcases error handling for invalid operations on these data structures.

Uploaded by

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

1.

List
>>> 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]
>>> list1[2][1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object is not subscriptable
>>> list2[3][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
>>> list1[4][2][1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> len(list2)
8
>>> list1[12]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> list2[-4:-1]
['MAC', [2, 4, 6], 3.33]
>>> list1
IndexError: string index out of range
>>> len(list2)
8
>>> list1[12]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> list2[-4:-1]
['MAC', [2, 4, 6], 3.33]
>>> list1
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> list2[-4:-1]
['MAC', [2, 4, 6], 3.33]
>>> list1
['MAC', [2, 4, 6], 3.33]
>>> list1
['apple', 10, 3.14, [1, 2, 3], 'class', 20, [4.5, 6.7], 5.5]
>>> 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]
>>> 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]
>>> list2[5][1] = 0
>>> del list1[-3]
>>> list2[5]
[2, 0, 6]
>>> 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]
>>> list2[5][1] = 0
>>> del list1[-3]
>>> list2[5]
[2, 0, 6]
['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]
>>> list2[5][1] = 0
>>> del list1[-3]
>>> list2[5]
[2, 0, 6]
], 'class', 20, [4.5, 6.7], 5.5]
>>> list2[5][1] = 0
>>> del list1[-3]
>>> list2[5]
[2, 0, 6]
>>> list2[5][1] = 0
>>> del list1[-3]
>>> list2[5]
[2, 0, 6]
>>> del list1[-3]
>>> list2[5]
[2, 0, 6]
>>> list2[5]
[2, 0, 6]
>>> list1
['apple', 10, 3.14, [1, 2, 3], 'class', [4.5, 6.7], 5.5]
>>> list1
['apple', 10, 3.14, [1, 2, 3], 'class', [4.5, 6.7], 5.5]
>>> list1.append('university')
>>> list1
['apple', 10, 3.14, [1, 2, 3], 'class', [4.5, 6.7], 5.5, 'university']
>>> list2
[8, 'list in python', [9.1, 7.2], 15, 'MAC', [2, 0, 6], 3.33, 12.5]
>>> list2.pop()
12.5
>>> list2
[8, 'list in python', [9.1, 7.2], 15, 'MAC', [2, 0, 6], 3.33]
>>> list1
['apple', 10, 3.14, [1, 2, 3], 'class', [4.5, 6.7], 5.5, 'university']
>>> list1.insert(5,100)
>>> list1
['apple', 10, 3.14, [1, 2, 3], 'class', 100, [4.5, 6.7], 5.5, 'university']
>>> list1[5]
100
>>> list2
[8, 'list in python', [9.1, 7.2], 15, 'MAC', [2, 0, 6], 3.33]
>>> list2.append([44, 50])
>>> list2
[8, 'list in python', [9.1, 7.2], 15, 'MAC', [2, 0, 6], 3.33, [44, 50]]
>>> list2[
... len(list2) - 1]
[44, 50]

2. String
>>> str1 = "Django allows a rapid web development and creates scalable systems"
>>> str2 = "There are two areas in cloud computing: performance and security"
>>> str2[-1:-6:-1]
'ytiru'
>>> str1[9]
'l'
>>> str2[-2:]
'ty'
>>> str2[0:20:3]
'Tra ors'
>>> str1+" "+str2
'Django allows a rapid web development and creates scalable systems There are two
areas in cloud computing: performance and security'
>>> str1.endswith("systems")
True
>>> str2.split()
['There', 'are', 'two', 'areas', 'in', 'cloud', 'computing:', 'performance', 'and',
'security']
>>> str1.upper()
'DJANGO ALLOWS A RAPID WEB DEVELOPMENT AND CREATES SCALABLE SYSTEMS'
>>> str2.upper()
'THERE ARE TWO AREAS IN CLOUD COMPUTING: PERFORMANCE AND SECURITY'
>>> str1.replace("web", "")
'Django allows a rapid development and creates scalable systems'
>>> str1
'Django allows a rapid web development and creates scalable systems'
>>> str1.replace("web", "")
'Django allows a rapid development and creates scalable systems'
>>> str2.count("e")
7

3. Dict

>>> d1={"name": "Bob", "age": 35, (4, 10):['x', 'y', 'z'], '+1' : "Canada", 44:
99, 19:555}
>>> d2 = dict([("name","Livy"), ('age', 44), ((1, 3, 5), ['a', 'b', 'c']), (0,
'black'), (33, 67)])
>>> d3 = dict(id=2277, name='Michael', siblings=['Janet', 'Martin', 'Richard'])
>>> d3
{'id': 2277, 'name': 'Michael', 'siblings': ['Janet', 'Martin', 'Richard']}
>>> d1.keys()
dict_keys(['name', 'age', (4, 10), '+1', 44, 19])
>>> d2.values()
dict_values(['Livy', 44, ['a', 'b', 'c'], 'black', 67])
>>> d3.get("id")
2277
>>> d2.get("age")
44
>>> d3.get("age")
>>> d3.get("name", "Tim")
'Michael'
>>> d2.items()
dict_items([('name', 'Livy'), ('age', 44), ((1, 3, 5), ['a', 'b', 'c']), (0,
'black'), (33, 67)])
>>> d3['siblings']
['Janet', 'Martin', 'Richard']
>>> d2['siblings']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'siblings'
>>> d2.update(d3)
>>> d2[0]
'black'
>>> d1.get((1,2))
>>> d2['siblings']*
File "<stdin>", line 1
d2['siblings']*
^
SyntaxError: invalid syntax
>>> d2['siblings']
['Janet', 'Martin', 'Richard']
>>> d2['name']
'Michael'
>>> d1 == d2
False
>>> len(d2)
7
>>> for key in d1.keys():
... print(key)
...
name
age
(4, 10)
+1
44
19
>>> for key in d2.keys():
... print(d2[key])
...
Michael
44
['a', 'b', 'c']
black
67
2277
['Janet', 'Martin', 'Richard']

You might also like