0% found this document useful (0 votes)
30 views

March 11 (Python Revision Tour - Part 2)

The document discusses Python lists and tuples. It shows various operations like accessing, modifying, sorting, counting elements on lists. It also demonstrates creating and accessing tuples.

Uploaded by

utsav kakkad
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)
30 views

March 11 (Python Revision Tour - Part 2)

The document discusses Python lists and tuples. It shows various operations like accessing, modifying, sorting, counting elements on lists. It also demonstrates creating and accessing tuples.

Uploaded by

utsav kakkad
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/ 15

Please Like, Share and Subscribe our YouTube Channel :

http://www.youtube.com/swatichawlaofficial

For Video Explanation of this topic, please click on the following link:
https://youtube.com/live/rqudymdfba8

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64


bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
L=[11,22,33]
type(L)
<class 'list'>
id(L)
1910254946624
L[2]=44
L
[11, 22, 44]
id(L)
1910254946624
L=[11,22,33,44,55]
L[2]
33
L[20]
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
L[20]
IndexError: list index out of range
L
[11, 22, 33, 44, 55]
L1=['C++','Python','Java']
L+L1
[11, 22, 33, 44, 55, 'C++', 'Python', 'Java']
L1+L

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

['C++', 'Python', 'Java', 11, 22, 33, 44, 55]


2+L
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
2+L
TypeError: unsupported operand type(s) for +: 'int' and 'list'
2*L
[11, 22, 33, 44, 55, 11, 22, 33, 44, 55]
2*L1
['C++', 'Python', 'Java', 'C++', 'Python', 'Java']
10*L
[11, 22, 33, 44, 55, 11, 22, 33, 44, 55, 11, 22, 33, 44, 55, 11, 22, 33, 44, 55,
11, 22, 33, 44, 55, 11, 22, 33, 44, 55, 11, 22, 33, 44, 55, 11, 22, 33, 44, 55,
11, 22, 33, 44, 55, 11, 22, 33, 44, 55]
L*L1
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
L*L1
TypeError: can't multiply sequence by non-int of type 'list'
L
[11, 22, 33, 44, 55]
L[2:5]
[33, 44, 55]
L
[11, 22, 33, 44, 55]
L[::-1]
[55, 44, 33, 22, 11]
L[::-2]
[55, 33, 11]
L=[10,20,30]
id(L)
Please /swatichawlaofficial /swatichawlaofficial Prepared By :
Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

1910254956224
L1=L
L1
[10, 20, 30]
id(L1)
1910254956224
L[1]='C++'
L
[10, 'C++', 30]
L1
[10, 'C++', 30]
L=[10,20,30]
id(L)
1910255220800
L1=list(L)
L1
[10, 20, 30]
id(L1)
1910255032768
L[1]='Swati'
L1
[10, 20, 30]
L
[10, 'Swati', 30]
L=[11,22,[10,20,30],'Python']
L[0]
11
L[1]
22
L[2]

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

[10, 20, 30]


L[3]
'Python'
L[2][2]
30
L[0][1]
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
L[0][1]
TypeError: 'int' object is not subscriptable
L[3]
'Python'
L[3][2]
't'
L
[11, 22, [10, 20, 30], 'Python']
len(L)
4
list("Python")
['P', 'y', 't', 'h', 'o', 'n']
list((11,22,33))
[11, 22, 33]
L=[11,22,3,11,33,22]
L.index(22)
1
L.index(2)
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
L.index(2)
ValueError: 2 is not in list

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

L
[11, 22, 3, 11, 33, 22]
L.append(25)
L
[11, 22, 3, 11, 33, 22, 25]
L.append(25,34)
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
L.append(25,34)
TypeError: list.append() takes exactly one argument (2 given)
L.append([25,34])
L
[11, 22, 3, 11, 33, 22, 25, [25, 34]]
L.extend(['C','Python'])
L
[11, 22, 3, 11, 33, 22, 25, [25, 34], 'C', 'Python']

L
[11, 22, 3, 11, 33, 22, 25, [25, 34], 'C', 'Python']
L.index(2,3)
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
L.index(2,3)

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

ValueError: 2 is not in list


L.insert(2,3)
L
[11, 22, 3, 3, 11, 33, 22, 25, [25, 34], 'C', 'Python']
L
[11, 22, 3, 3, 11, 33, 22, 25, [25, 34], 'C', 'Python']
L.pop()
'Python'
L.pop()
'C'
L
[11, 22, 3, 3, 11, 33, 22, 25, [25, 34]]
L.pop(2)
3
L
[11, 22, 3, 11, 33, 22, 25, [25, 34]]
L.remove(33)
L
[11, 22, 3, 11, 22, 25, [25, 34]]
L.clear()
L
[]
del L
L
Traceback (most recent call last):
File "<pyshell#86>", line 1, in <module>
L
NameError: name 'L' is not defined. Did you mean: 'L1'?
L1
[10, 20, 30]

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

del L1[0:2]
L1
[30]
L1.remove()
Traceback (most recent call last):
File "<pyshell#90>", line 1, in <module>
L1.remove()
TypeError: list.remove() takes exactly one argument (0 given)
L=[11,22,33,11]
L.remove(11)
L
[22, 33, 11]
L.remove(44)
Traceback (most recent call last):
File "<pyshell#94>", line 1, in <module>
L.remove(44)
ValueError: list.remove(x): x not in list
L
[22, 33, 11]
L.count(22)
1
L.count(55)
0
L
[22, 33, 11]
L.reverse()
L
[11, 33, 22]
L=[11,55,33,77,22,98,56]
L.sort()

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

L
[11, 22, 33, 55, 56, 77, 98]
L.sort(reverse=True)
L
[98, 77, 56, 55, 33, 22, 11]
L=[11,55,33,77,22,98,56]
sorted(L)
[11, 22, 33, 55, 56, 77, 98]
L
[11, 55, 33, 77, 22, 98, 56]
L=[11,22,[33,44,55]]
max(L)
Traceback (most recent call last):
File "<pyshell#110>", line 1, in <module>
max(L)
TypeError: '>' not supported between instances of 'list' and 'int'
L
[11, 22, [33, 44, 55]]
L=[34,5,3,56,67]
L.sort()
L
[3, 5, 34, 56, 67]
L.sort(reverse=True)
L
[67, 56, 34, 5, 3]
L=[34,5,3,56,67]
sorted(L)
[3, 5, 34, 56, 67]
L
[34, 5, 3, 56, 67]

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

T=(11)
type(T)
<class 'int'>
T=(11,)
type(T)
<class 'tuple'>
T=11,22,33,44
type(T)
<class 'tuple'>
T=22,33,45,(34,45,67),54,56
type(T)
<class 'tuple'>
T
(22, 33, 45, (34, 45, 67), 54, 56)
(2)*3
6
(2,)*3
(2, 2, 2)
T=(11,22,33,44)
id(T)
1910255123840
a,b,c,d=T
a
11
b
22
c
33
d
44

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

b=20
T=(a,b,c,d)
T
(11, 20, 33, 44)
id(T)
1910248892720
T
(11, 20, 33, 44)
L=list(T)
L
[11, 20, 33, 44]
L[0]=10
L
[10, 20, 33, 44]
T=tuple(L)
T
(10, 20, 33, 44)
T
(10, 20, 33, 44)
T=(1,2,3,4,5)
T.index(3)
2
T
(1, 2, 3, 4, 5)
T[2]
3
T.index(4)
3
D={1:'1',2:'22',3:'333',4:'4444'}
type(D)

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

<class 'dict'>
D={1:'1',2:'22',3:'333',4:'4444',1:'One'}
D
{1: 'One', 2: '22', 3: '333', 4: '4444'}
D={1:'1',2:'22',3:'333',4:'4444'}
d[2]
Traceback (most recent call last):
File "<pyshell#160>", line 1, in <module>
d[2]
TypeError: 'int' object is not subscriptable
D[2]
'22'
D
{1: '1', 2: '22', 3: '333', 4: '4444'}
D[2]='Two'
D
{1: '1', 2: 'Two', 3: '333', 4: '4444'}
D[5]='Five'
D
{1: '1', 2: 'Two', 3: '333', 4: '4444', 5: 'Five'}

D[22]
Traceback (most recent call last):
File "<pyshell#171>", line 1, in <module>
D[22]
KeyError: 22

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

D['22']
Traceback (most recent call last):
File "<pyshell#172>", line 1, in <module>
D['22']
KeyError: '22'
D
{1: '1', 2: 'Two', 3: '333', 4: '4444', 5: 'Five'}
del D[2]
D
{1: '1', 3: '333', 4: '4444', 5: 'Five'}
1 in D
True
'333' in D
False
D
{1: '1', 3: '333', 4: '4444', 5: 'Five'}
D[2]
Traceback (most recent call last):
File "<pyshell#179>", line 1, in <module>
D[2]
KeyError: 2
D[3]
'333'
D.get(3)
'333'
D[2]
Traceback (most recent call last):
File "<pyshell#182>", line 1, in <module>
D[2]
KeyError: 2

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

D.get(2)
a=D.get(2)
print(a)
None
D.get(2,'Not exist')
'Not exist'
D.get(1,'Not exist')
'1'
D
{1: '1', 3: '333', 4: '4444', 5: 'Five'}
D.items()
dict_items([(1, '1'), (3, '333'), (4, '4444'), (5, 'Five')])
D
{1: '1', 3: '333', 4: '4444', 5: 'Five'}
D.keys()
dict_keys([1, 3, 4, 5])
D.values()
dict_values(['1', '333', '4444', 'Five'])
D={1:10,2:20,3:30}
D1={3:40,5:50}
D.update(D1)
D
{1: 10, 2: 20, 3: 40, 5: 50}
D1
{3: 40, 5: 50}
L=[11,22,33,44]
L.pop(2)
33
L
[11, 22, 44]

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

L.pop()
44
D
{1: 10, 2: 20, 3: 40, 5: 50}
D.pop()
Traceback (most recent call last):
File "<pyshell#203>", line 1, in <module>
D.pop()
TypeError: pop expected at least 1 argument, got 0
D.pop(2)
20
D
{1: 10, 3: 40, 5: 50}
D.pop(9)
Traceback (most recent call last):
File "<pyshell#206>", line 1, in <module>
D.pop(9)
KeyError: 9
D.pop(9,'Key not present')
'Key not present'
D.pop(5,'Key not present')
50
D
{1: 10, 3: 40}
D.popitem()
(3, 40)
D
{1: 10}
D.clear()
D

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
http://www.youtube.com/swatichawlaofficial

{}
D={1: 10, 2: 20, 3: 40, 5: 50}
D.pop(2)
20
D
{1: 10, 3: 40, 5: 50}
L=['Mani','Mini']
min(L)
'Mani'

Method 1

L=[11,22,33,44,55]
for i in L:
print(i)

Method 2 (Using range() function)


L=[11,22,33,44,55]
for i in range(len(L)):
print(L[i])

D={1: '1', 2: 'Two', 3: '333', 4: '4444', 5: 'Five'}


for i in D:
print(i,D[i])

To get access to Premium Notes, please join the


membership of the channel.
https://www.youtube.com/channel/UC2vH9rqG
h-fJELF8acGfBdw/join

Please /swatichawlaofficial /swatichawlaofficial Prepared By :


Follow
us at: t.me/swatichawla12cs /swatichawlaofficial Swati Chawla

You might also like