Professional Documents
Culture Documents
March 11 (Python Revision Tour - Part 2)
March 11 (Python Revision Tour - Part 2)
http://www.youtube.com/swatichawlaofficial
For Video Explanation of this topic, please click on the following link:
https://youtube.com/live/rqudymdfba8
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]
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)
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()
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]
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
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)
<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
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
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]
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
{}
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)