Python Selfmade Notes
Python Selfmade Notes
Day-1
Guido Van Rossum is the one who created Python in the year 1991.
Features of Python:
1) It is easy to learn and analyze.
1) It is interpreted language.
8) It is open source.
---> We can easily download python software without paying single rupee.
We can also contribute code for python.
Day-3
Library Function:
---> It is a function which is predefined by the developer to perform some particular task.
Note:
-> We can access the features of Library Function but we cannot modify it.
1) Keyword:
---> They are universally standard words, which is predefined by the developer to perform some particular
task.
import keyword
keyword.kwlist
len(keyword.kwlist)
35
Practical proof
a=10
b=True
b
New Section 1 Page 4
b
True
b=False
b
False
c=None
c
d=and
SyntaxError: invalid syntax
d=as
SyntaxError: invalid syntax
d=assert
SyntaxError: invalid syntax
Variable:
It is a container which is used to store the address of the value stored in the memory.
Or
Syntax: var_name=value
a=21
id():It is an inbuilt function which is used to get the actual address of the value stored inside the memory.
Syntax: id(var_name/value)
a=21
id(a)
140709722092968
id(21)
140709722092968
Syntax:
var1,var2,…..,varn=val1,val2,…..,valn
a,b,c,d=10,20,30,40
Memory allocation:
a,b,c,d=10,20,30,40
a
10
b
20
c
30
d
40
id(a)
140709722092616
id(b)
140709722092936
id(c)
140709722093256
id(d)
140709722093576
Memory allocation:
a,a,a=10,20,30
Memory allocation:
a,b,c=10,20,10
a
10
b
20
c
10
id(a)
140709722092616
id(b)
140709722092936
id(c)
140709722092616
a,a,a=10,20,30
a
30
Reference Count: It will count the number of variables sharing the same value.
a,b,c=10,20,10
New Section 1 Page 7
a,b,c=10,20,10
Memory allocation:
Conclusion: As soon the RC becomes zero(0) , it will be deleted from the memory and it will be collected by
garbage collector.
Day-4
Identifiers: It is a name given to a variable uniquely which is used to identify the value.
Note:
All the variables are identifiers, but all the identifiers are not variables.
Rules of Identifiers:
a=10
if=10
SyntaxError: invalid syntax
and=10
SyntaxError: invalid syntax
True=10
SyntaxError: cannot assign to True
a=True
a
True
Reason: Keywords are already predefined with some task, and special keywords can be used as value but not
as variable.
2a=10
a+b=10
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
a-b=30
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
a_b=40
a_b
40
b_=10
b_
10
_a=10
_a
10
_=20
_
20
Reason: All the special characters are predefined with some task, except underscore.
a b=10
SyntaxError: invalid syntax
a=10
SyntaxError: unexpected indent
ab =10
ab
10
Datatypes:
--- It is going to specify the size and type of the value stored in the variable.
Types:
1) Integer:
• It is a real number without decimal point.
• Default value : It is an initial value from where my datatype values starts. These are internally equal to
False.
• Non-default value : All the values except Default value are considered as non-default value. These are
internally equal to True.
Syntax: type(var/val)
a=10
type(a)
<class 'int'>
type(10)
<class 'int'>
• bool() : Whenever we want to check the value we stored is default value or not.
b=0
bool(b)
False
c=12
bool(c)
True
2) Float:
• It is a real number with decimal point.
• type():
a=1.2
type(a)
<class 'float'>
type(1.2)
<class 'float'>
• bool():
b=3.2
bool(b)
True
c=0.0
bool(c)
False
a=1.454359347593402956396656
a
1.454359347593403
a=22.32432857429573496447656756
a
22.324328574295734
b=2324.3534654765876789798708970
b
2324.3534654765876
3) Complex:
• It is a combination of real part and imaginary part.
a=2+3j
a
(2+3j)
b=2.3-6.8j
b
(2.3-6.8j)
c=2+3.5j
New Section 1 Page 11
c=2+3.5j
c
(2+3.5j)
d=7.1-2j
d
(7.1-2j)
• type():
a=2+3j
type(a)
<class 'complex'>
type(2+3j)
<class 'complex'>
• bool():
b=7+3.2j
bool(b)
True
c=0.0+0.0j
bool(c)
False
c
0j
a=2+3i
SyntaxError: invalid decimal literal
a=6+7b
SyntaxError: invalid decimal literal
b=4+7J
b
(4+7j)
a=2x+4j
SyntaxError: invalid decimal literal
b=4j+2
b
(2+4j)
c=3+j8
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
c=3+j8
NameError: name 'j8' is not defined
Day-6
4) Boolean:
It consists of 2 values,
• True
New Section 1 Page 12
• True
• False
a=True
type(a)
<class 'bool'>
bool(a)
True
b=False
type(b)
<class 'bool'>
bool(b)
False
a=True
100>20
True
10<5
False
Multivalued Datatype
5) String:
--- It is a collection of characters enclosed with ' '," ",''' '''
(A-Z,a-z,0-9, special characters).
Syntax: var='val1val2val3……valn'
a='python'
a
'python'
a="python"
a
'python'
a='''python'''
a
'python'
a='sakshi'
type(a)
<class 'str'>
bool(a)
True
b=""
bool(b)
False
Memory Allocation:
s='python is easy'
Indexing : The phenomenon of passing subaddress to the memory block is called as Indexing.
Types:
• +ve indexing --- starts from left to right (starts from 0, ends with len(collection)-1)
• -ve indexing --- starts from right to left (starts from -1, ends with -len(collection))
s='python is easy'
Note:
• To get/access the value from the collection, we use the syntax
var[index]
s='python is easy'
s[4]
'o'
s[-10]
s='python is easy'
s[4]
'o'
s[4]='m'
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
s[4]='m'
TypeError: 'str' object does not support item assignment
6) List:
--- It is a collection of homogeneous and heterogeneous values enclosed by [].
Syntax: var=[val1,val2,val3,…….valn]
Example: l=[10,20,30,40]
Memory allocation:
a=[10,2.0,3+0j,True,'40',[10,20]]
var[index]
a=[10,2.0,3+0j,True,'40',[10,20]]
a[4]
'40'
a[4][1]
'0'
var[index]=new_value
a=[10,2.0,3+0j,True,'4',[10,20]]
a[5]
[10, 20]
a[5][0]
10
a[5][0]='10'
a
[10, 2.0, (3+0j), True, '4', ['10', 20]]
a[5][1]='20'
a
[10, 2.0, (3+0j), True, '4', ['10', '20']]
a=[10,2.0,3+0j,True,'4',[10,20]]
a[4]='sam'
a
[10, 2.0, (3+0j), True, 'sam', [10, 20]]
a[4]
'sam'
a[4][1]
'a'
a[4][1]='m'
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
a[4][1]='m'
TypeError: 'str' object does not support item assignment
Day-7
a=[10,2.0,3+0j,True,'4',[10,20]]
a.append(7)
a
[10, 2.0, (3+0j), True, '4', [10, 20], 7]
a.append(8)
a
[10, 2.0, (3+0j), True, '4', [10, 20], 7, 8]
a.append('python')
a
[10, 2.0, (3+0j), True, '4', [10, 20], 7, 8, 'python']
a=[10,2.0,3+0j,True,'4',[10,20]]
a.insert(4,'Java')
a
[10, 2.0, (3+0j), True, 'Java', '4', [10, 20]]
a.insert(0,2+0j)
a
[(2+0j), 10, 2.0, (3+0j), True, 'Java', '4', [10, 20]]
a.insert(10,'cute')
a
[(2+0j), 10, 2.0, (3+0j), True, 'Java', '4', [10, 20], 'cute']
a.insert(100,'promise day')
a
[(2+0j), 10, 2.0, (3+0j), True, 'Java', '4', [10, 20], 'cute', 'promise day']
a.insert(4,False)
a
[(2+0j), 10, 2.0, (3+0j), False, True, 'Java', '4', [10, 20], 'cute', 'promise day']
a.insert(98,'idiot')
a
[(2+0j), 10, 2.0, (3+0j), False, True, 'Java', '4', [10, 20], 'cute', 'promise day', 'idiot']
• remove(): Used to remove the value when we don’t know the index position of the value.
syntax: var.remove(value)
a=[10,2.3,10,2.3,True,True]
a.remove(10)
a
[2.3, 10, 2.3, True, True]
a.remove(2.3)
Syntax:
var=(val1,val2,……..,valn) --- when we have multiple values
var=(val1,) --- when we have only one value.
a=10,20,30,40
a
(10, 20, 30, 40)
type(a)
<class 'tuple'>
b=(10)
type(b)
<class 'int'>
c=(10,)
type(c)
<class 'tuple'>
d=2.3,
type(d)
<class 'tuple'>
Memory Allocation:
t=(79,([10,20,30],3.9,'python'),34,58)
t=(79,([10,20,30],3.9,'python'),34,58)
t[1]
([10, 20, 30], 3.9, 'python')
t[0]
79
t[1][0]
[10, 20, 30]
t[2]
34
t[1][2]
t=(79,([10,20,30],3.9,'python'),34,58)
t[1]
([10, 20, 30], 3.9, 'python')
t[1][2]
'python'
t[1][2][2]
't'
t[1][2][2]='@'
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
t[1][2][2]='@'
TypeError: 'str' object does not support item assignment
t[3]
58
t[3]=65
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
t[3]=65
TypeError: 'tuple' object does not support item assignment
t[1][1]
3.9
t[1][0]
[10, 20, 30]
t[1][0][1]
20
t[1][0][1]=50
t
(79, ([10, 50, 30], 3.9, 'python'), 34, 58)
t[1][0]
[10, 50, 30]
t[1][0]=[1,2]
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
t[1][0]=[1,2]
TypeError: 'tuple' object does not support item assignment
t[1][1]
3.9
t[1][1]=8.9
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
t[1][1]=8.9
TypeError: 'tuple' object does not support item assignment
Day-8
Set:
--- It is a collection of homogeneous and heterogeneous values enclosed by {}.
syntax:
var={val1,val2,……,valn}
a={10,2.3,2+3j,True,'hi',(10,20)}
a={10,2.3,2+3j,True,'hi',(10,20),[30,40]}
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
a={10,2.3,2+3j,True,'hi',(10,20),[30,40]}
TypeError: unhashable type: 'list'
a={10,2.3,2+3j,True,'hi',(10,20)}
A
{True, 2.3, 'hi', 10, (2+3j), (10, 20)}
Unordered --- order of input does not match the order of output
• Set will eliminate repeated/duplicate values.
a={10,2.3,2+3j,10,True,2.3,'hi',(10,20),1}
a
{True, 2.3, 10, (2+3j), 'hi', (10, 20)}
Memory allocation:
a={10,2.3,2+3j,True,'hi',(10,20)}
hash(10)
10
hash(2.3)
691752902764107778
hash(2+3j)
3000011
hash(True)
1
hash('hello')
-6453495032443784337
hash((10,20))
-4873088377451060145
hash([10,20])
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
hash([10,20])
a={10,2.3,2+3j,True,'hi',(10,20)}
a.add('python')
a
{True, 2.3, 'hi', 'python', 10, (2+3j), (10, 20)}
a.add('hi')
a
{True, 2.3, 'hi', 'python', 10, (2+3j), (10, 20)}
a={10,2.3,2+3j,True,'hi',(10,20)}
a.remove(2+3j)
a
{True, 2.3, 'hi', 10, (10, 20)}
a.remove(10)
a
{True, 2.3, 'hi', (10, 20)}
a.remove(2.3)
a
{True, 'hi', (10, 20)}
9) Dictionary:
--- It is a collection of key-value pairs enclosed by {} and the key-value pair is seperated by colon(:).
Syntax:
var={k1:v1,k2:v2,……..,kn:vn}
a={'a':10,[10]:20,{10,20}:30}
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
a={'a':10,[10]:20,{10,20}:30}
TypeError: unhashable type: 'list'
a={'a':10,10:20,{10,20}:30}
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
a={'a':10,10:20,{10,20}:30}
TypeError: unhashable type: 'set'
• Keys should be unique. If we try to enter the same key then old value of that key will be replaced by new
value.
a={'a':10,'b':20,'a':30}
a
{'a': 30, 'b': 20}
a={'a':10,'b':10}
a
{'a': 10, 'b': 10}
a={'a':10,'b':20,'c':30}
a[1]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
a[1]
KeyError: 1
Note:
--- In dictionary, only the keys are the visible layer.
s={'a':10,'b':20,'c':30}
s['b']
20
s['a']
10
s['c']
30
s={'a':10,'b':20,'c':30}
s['c']=40
s
{'a': 10, 'b': 20, 'c': 40}
s['b']=100
s
{'a': 10, 'b': 100, 'c': 40}
Memory allocation:
d={'a':10,'b':'python','c':2+3j,(1,2):[10,20,30]}
d={'a':10,'b':'python','c':2+3j,(1,2):[10,20,30]}
d['b']
'python'
d['b'][3]
'h'
d[(1,2)]
[10, 20, 30]
d[(1,2)][2]
30
Day-9
Slicing:
--- It is used to extract the group of values from the collection.
Syntax:
var[SI:EI+1:updation]
var[SI:EI-1:updation]
Note:
• When we extract values from left to right
EI+1
• When we extract values from right to left
EI-1
Memory allocation:
s='India is best'
New Section 1 Page 23
s='India is best'
s='India is best'
s[0:5:1]
'India'
s[0:4+1:+1]
'India'
s[9:13:1]
'best'
s[0:7:2]
'Idai'
s[-9:-14:-1]
'aidnI'
s[-1:-5:-1]
'tseb'
s[-7:-14:-2]
'iadI'
Slicing shortcuts:
• If SI is 0 or -1
[:EI+1:updation]
[:EI-1:updation]
[SI::updation]
[SI::updation]
• If Updation is +1
[SI:EI:]
s='India is best'
s[::]
'India is best'
s[::-1]
'tseb si aidnI'
Syntax: dest_type(source_var)
Note:
• Converting from SVDT to MVDT only string is supported
• Converting from MVDT to SVDT only boolean is supported
For SVDT:
a=10
type(a)
<class 'int'>
int(a)
10
float(a)
10.0
complex(a)
(10+0j)
complex(a,a)
(10+10j)
bool(a)
True
For MVDT:
str(a)
'10'
list(a)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
list(a)
TypeError: 'int' object is not iterable
tuple(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
tuple(a)
TypeError: 'int' object is not iterable
set(a)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
set(a)
TypeError: 'int' object is not iterable
dict(a)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
dict(a)
TypeError: 'int' object is not iterable
For SVDT:
For SVDT:
a=7.4
type(a)
<class 'float'>
int(a)
7
complex(a)
(7.4+0j)
complex(a,a)
(7.4+7.4j)
bool(a)
True
For MVDT:
str(a)
'7.4'
list(a)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
list(a)
TypeError: 'float' object is not iterable
tuple(a)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
tuple(a)
TypeError: 'float' object is not iterable
set(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
set(a)
TypeError: 'float' object is not iterable
dict(a)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
dict(a)
TypeError: 'float' object is not iterable
For SVDT:
a=7+3j
type(a)
<class 'complex'>
int(a)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
int(a)
TypeError: int() argument must be a string, a bytes-like
object or a real number, not 'complex'
float(a)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
float(a)
TypeError: float() argument must be a string or a real number, not 'complex'
bool(a)
True
For MVDT:
New Section 1 Page 26
For MVDT:
str(a)
'(7+3j)'
list(a)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
list(a)
TypeError: 'complex' object is not iterable
tuple(a)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
tuple(a)
TypeError: 'complex' object is not iterable
set(a)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
set(a)
TypeError: 'complex' object is not iterable
dict(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
dict(a)
TypeError: 'complex' object is not iterable
For SVDT:
a=True
int(a)
1
float(a)
1.0
complex(a)
(1+0j)
bool(a)
True
complex(a,a)
(1+1j)
For MVDT:
str(a)
'True'
list(a)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
list(a)
TypeError: 'bool' object is not iterable
tuple(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
tuple(a)
TypeError: 'bool' object is not iterable
set(a)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
set(a)
TypeError: 'bool' object is not iterable
dict(a)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
For SVDT:
a='python'
int(a)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
int(a)
ValueError: invalid literal for int() with base 10: 'python'
b='12'
int(b)
12
float(b)
12.0
c='abc123'
int(c)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
int(c)
ValueError: invalid literal for int() with base 10: 'abc123'
complex(b)
(12+0j)
complex(b,b)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
complex(b,b)
TypeError: complex() can't take second arg if first is a string
complex(12,b)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
complex(12,b)
TypeError: complex() second arg can't be a string
complex(b,10)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
complex(b,10)
TypeError: complex() can't take second arg if first is a string
bool(b)
True
For MVDT:
a='python'
list(a)
['p', 'y', 't', 'h', 'o', 'n']
tuple(a)
('p', 'y', 't', 'h', 'o', 'n')
set(a)
{'o', 'n', 'p', 't', 'y', 'h'}
dict(a)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
dict(a)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
For SVDT:
a=[10,20,30,40]
type(a)
<class 'list'>
For MVDT:
str(a)
'[10, 20, 30, 40]'
tuple(a)
(10, 20, 30, 40)
set(a)
{40, 10, 20, 30}
dict(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
dict(a)
TypeError: cannot convert dictionary update sequence element #0 to a sequence
b=['hi',[10,20],(30,40),{50,60}]
dict(b)
{'h': 'i', 10: 20, 30: 40, 50: 60}
Day-11
For SVDT:
a=(10,20,30,40)
type(a)
<class 'tuple'>
int(a)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
int(a)
TypeError: int() argument must be a string, a bytes-like
object or a real number, not 'tuple'
float(a)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
float(a)
TypeError: float() argument must be a string or a real number, not 'tuple'
complex(a)
For MVDT:
a=(10,20,30,40)
type(a)
<class 'tuple'>
int(a)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
int(a)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple'
float(a)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
float(a)
TypeError: float() argument must be a string or a real number, not 'tuple'
complex(a)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
complex(a)
TypeError: complex() first argument must be a string or a number, not 'tuple'
bool(a)
True
str(a)
'(10, 20, 30, 40)'
list(a)
[10, 20, 30, 40]
set(a)
{40, 10, 20, 30}
dict(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
dict(a)
TypeError: cannot convert dictionary update sequence element #0 to a sequence
b=('sa',[10,20])
dict(b)
{'s': 'a', 10: 20}
b=('sa',[10,20,30])
dict(b)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
dict(b)
ValueError: dictionary update sequence element #1 has length 3; 2 is required
For SVDT:
a={10,20,30,40}
type(a)
<class 'set'>
int(a)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
int(a)
TypeError: int() argument must be a string, a bytes-like object or a
For MVDT:
str(a)
'{40, 10, 20, 30}'
list(a)
[40, 10, 20, 30]
tuple(a)
(40, 10, 20, 30)
dict(a)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
dict(a)
TypeError: cannot convert dictionary update sequence element #0 to a sequence
b={'cd',(10,20)}
dict(b)
{'c': 'd', 10: 20}
For SVDT:
a={'a':10,'b':20,'c':30}
type(a)
<class 'dict'>
int(a)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
int(a)
TypeError: int() argument must be a string, a bytes-like
object or a real number, not 'dict'
float(a)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
float(a)
TypeError: float() argument must be a string or a real number, not 'dict'
complex(a)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
complex(a)
TypeError: complex() first argument must be a string or a number, not 'dict'
bool(a)
True
For MVDT:
str(a)
"{'a': 10, 'b': 20, 'c': 30}"
Copy operation:
--- It is a phenomenon of copying the content from one variable to another variable.
Types:
• General copy/ Normal copy
• Shallow copy
• Deep copy
1) General copy:
--- It will just copy the variable space of one variable to another variable.
Syntax: dest_var=source_var
Memory allocation:
Example 1:
a=[10,2.5,2+3j,True] ---- linear collection
b=a
Example 2:
a=[10,2.5,[6.3,9j]]
b=a
a
[10, 2.5, [6.3, 9j]]
b
[10, 2.5, [6.3, 9j]]
a[1]=[6.3,9j]
a
[10, [6.3, 9j], [6.3, 9j]]
b
[10, [6.3, 9j], [6.3, 9j]]
id(a)
2602509778368
id(b)
2602509778368
Conclusion: Modification done in linear and nested collection of source variable is affecting the
destination variable
2) Shallow copy:
--- It is a phenomenon of copying the content of main memory layer of value space from one variable to
Syntax: dest_var=source_var.copy()
Memory allocation:
Example 1:
a=[10,2.5,2+3j,True] ---- linear collection
b=a.copy()
a=[10,2.5,2+3j,True]
b=a.copy()
a[2]=4
a
[10, 2.5, 4, True]
b
[10, 2.5, (2+3j), True]
id(a)
1867641833088
id(b)
1867641964160
Example 2:
Conclusion: Modification done w.r.t linear collection of source variable will not affect the destination
variable, but Modification done w.r.t nested collection of source variable will affect the destination
variable.
3) Deep copy:
--- It is a phenomenon of copying the entire content of value space from one variable to another.
Syntax:
import copy
dest_var=copy.deepcopy(source_var)
Memory allocation:
Example 1:
a=[10,2.5,2+3j,True] ---- linear collection
import copy
b=copy.deepcopy(a)
Example 2:
a=[10,2.5,[6.3,9j]]
import copy
b=copy.deepcopy(a)
a
[10, 2.5, [6.3, 9j]]
b
[10, 2.5, [6.3, 9j]]
a[2][1]=(2+3j)
a
[10, 2.5, [6.3, (2+3j)]]
b
[10, 2.5, [6.3, 9j]]
id(a)
2516083974016
id(b)
2516094139840
id(a[2])
2516094404224
id(b[2])
2516094059072
Conclusion: Modification done in linear and nested collection of source variable will not affect the
destination variable.
Note: Deep copy is the only type which satisfies the objective of Copy operation.
Day-12
Operators: They are the special symbols, which is used to perform specific task with the help of operands to
give the result.
Types:
• Arithmetic
• Logical
• Relational/ Comparision
• Bitwise
• Assignment
• Membership
• Identity
1) Arithmetic Operator:
• Addition(+):
For SVDT:
Note: It will consider the value only
10+20
30
2.4+6.8
9.2
(2+3j)+(4+7j)
(6+10j)
True+False
1
1+2.3+(5+3j)+True
(9.3+3j)
For MVDT:
Note: It will consider both the value and datatype.
'hi'+10
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
'hi'+10
• Subtraction(-):
For SVDT:
10-7
3
7.8-5.7
2.0999999999999996
(7+8j)-(2+3j)
(5+5j)
True-True
0
26-6.8-(6+2j)-True
(12.2-2j)
For MVDT:
'sam'-'hello'
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
'sam'-'hello'
TypeError: unsupported operand type(s) for -: 'str' and 'str'
[10,20,30]-[20,30]
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
[10,20,30]-[20,30]
TypeError: unsupported operand type(s) for -: 'list' and 'list'
(10,20,30)-(20,30)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
(10,20,30)-(20,30)
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
{10,20,30}-{20,30}
{10}
{'a':10}-{'b':20}
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
{'a':10}-{'b':20}
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
• Multiplication(*):
New Section 1 Page 38
• Multiplication(*):
For SVDT:
10*5
50
2.3*7.9
18.169999999999998
(2+3j)*(7+9j)
(-13+39j)
True*False
0
10*2.3*(4+7j)*True
(92+161j)
For MVDT:
'python'*'python'
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
'python'*'python'
TypeError: can't multiply sequence by non-int of type 'str'
'python'*4
'pythonpythonpythonpython'
[10,20,30]*2
[10, 20, 30, 10, 20, 30]
(10,20,30)*3
(10, 20, 30, 10, 20, 30, 10, 20, 30)
{10,20,30}*3
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
{10,20,30}*3
TypeError: unsupported operand type(s) for *: 'set' and 'int'
{'a':10}*2
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
{'a':10}*2
TypeError: unsupported operand type(s) for *: 'dict' and 'int'
[10,20]*[20,30]
10/2
5.0
2.3/1.8
1.2777777777777777
(2+3j)/(5+3j)
(0.5588235294117647+0.2647058823529412j)
True/False
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
True/False
ZeroDivisionError: division by zero
False/True
0.0
10//2
5
2.3//2
1.0
2//2.3
0.0
2.3//2.3
1.0
(2+3j)//(4+7j)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
(2+3j)//(4+7j)
TypeError: unsupported operand type(s) for //: 'complex' and 'complex'
True//True
1
13%3
1
4.5%2.7
1.7999999999999998
(2+3j)%(4j)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
(2+3j)%(4j)
TypeError: unsupported operand type(s) for %: 'complex' and 'complex'
True%True
0
2**3
8
4.5**2
20.25
(1+2j)**6
(117+44j)
True*7
7
True**7
1
4.3**6.2
8462.61190474804
(1+2j)**4.5
(9.963495519579647-36.03153123263964j)
16**(1/3)
2.5198420997897464
5**True
5
True**100
1
True**False
Day-13
2) Logical operator:
• Logical AND:
--- If any one operand is False, The result is False.
Note:
▪ If OP1 is False,
output = OP1
▪ If OP1 is True,
output = OP2
10 and 22
22
0.0 and 9.8
0.0
2 and 0j
0j
[9.7] and 3.4
3.4
[] and ()
[]
2 and 0.0 and 5.6
0.0
• Logical OR:
--- If any one operand is True, The result is True.
Note:
▪ If OP1 is False,
output = OP2
▪ If OP1 is True,
output = OP1
'tea' or 'coffee'
'tea'
6 or 9
6
0 or 123
123
[] or ''
''
78 or 34 or 5
78
67 and 23 or 89 and 45
23
Note:
▪ If OP is False
output = True
▪ If OP is True,
output = False
not(7)
False
not(())
True
not([''])
False
3) Relational Operator / Comparision Operator: --- Output will be in the form of boolean.
• Equal to (==): It is used to check whether both the operands are same or not.
10 == 10
True
10 == 10.0
True
True == 1
True
3.4 == 4.5
False
10 == 10
True
10 == 10.0
True
True == 1
True
3.4 == 4.5
False
'hi' == 'hi'
True
[10,20] == (10,20)
False
{1,2,3} == {3,1,2}
True
[1,2,3] == (3,1,2)
False
[1,2,3] == [3,1,2]
False
{'a':10} == {'a':20}
False
{'a':10} == {'a':10}
True
• Greater than (>): --- It will not support for complex and dictionary
--- Used to check whether the first operand is greater than the second operand or not.
For SVDT:
25 > 12
True
3.51 > 3.5
True
(2+3j) > (4+7j)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
(2+3j) > (4+7j)
TypeError: '>' not supported between instances of 'complex' and 'complex'
4j > 2j
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
4j > 2j
TypeError: '>' not supported between instances of 'complex' and 'complex'
1 > True
False
For MVDT:
▪ If val1 > v1
output = True
▪ If val1 < v1
output = False
▪ If val1 == v1
Then it will go and check the next value.
For set, it will check whether all the values of OP2 is present in OP1 or not.
If all the values of OP2 present in OP1 then len(OP1) should be greater then only it will return
True.
ord('A')
65
ord('Z')
90
ord('a')
97
ord('z')
122
'python' > 'pycharm'
True
'ABC' > 'abc'
False
[10,20,30] > [10,20,10]
True
(10,20,30) > (10,20,10)
True
{10,20,30} > {1,2,3}
False
{10,20,30,1,2,3} > {1,2,3}
True
{10,20,30} > {10,20,30}
False
• Lesser than (<): --- It will not support for complex and dictionary
--- Used to check whether the first operand is lesser than the second operand or not.
For SVDT:
20<50
True
True<1
False
3.4<3.33
False
For MVDT:
▪ If val1 < v1
output = True
▪ If val1 > v1
output = False
▪ If val1 == v1
Then it will go and check the next value.
For set, it will check whether all the values of OP1 is present in OP2 or not.
Then, If all the values of OP1 present in OP2 then len(OP2) should be greater then only it
will return True.
Day-14
• Greater than or equal to (>=): --- It will not support for complex and dictionary
--- Used to check whether the first operand is greater than or equal to the second operand or not.
For SVDT:
10>=10
True
3.4>=5.6
False
True>=False
True
For MVDT:
• Lesser than or equal to (>=): --- It will not support for complex and dictionary
--- Used to check whether the first operand is lesser than or equal to the second operand or not.
For SVDT:
10<=10.0
True
3.4<=7.2
True
True<=False
False
For MVDT:
For SVDT:
10!=10
False
2.3!=3.2
True
(2+3j)==(3j+2)
True
(3j+2)
(2+3j)
(2+3j)!=(4j+2)
True
True != False
True
For MVDT:
'happy'!='happ'
True
[10,20,30]!=[10,20,30]
False
(1,2,3)!=(True,True+True,True+True+True)
False
{1,'hello',True}!={True,True,'hello'}
False
2 2 2 2 2 2 2 2 2
• Divide by 2 method
bin(7)
'0b111'
bin(17)
'0b10001'
bin(11)
'0b1011'
• Bitwise AND(&):
Syntax: OP1 & OP2
Practical proof:
3&8
0
15 & 21
5
• Bitwise OR(|):
Syntax: OP1 | OP2
Practical proof:
6|3
7
32 | 2
34
• Bitwise NOT(~):
Syntax: ~(OP) results will be displayed like --- -(OP+1)
Practical proof:
~(3)
-4
~(-7)
6
• Bitwise XOR(^):
Syntax: OP1 ^ OP2
9 ^ 14
7
Practical proof:
9 << 2
36
9 << 1
18
9 << 5
288
18 >> 2
4
Day-15
a=a + b --- a += b
a=a - b --- a -= b
a=a*b --- a*=b
a=a/b --- a/=b
a=a//b --- a//=b
a=a%b --- a%=b
a=a**b --- a**=b
6) Membership Operator:
--- It is used to check if the value is present inside the collection or not.
• in --- Tells values present in the collection. It will return True if the values are present in the
collection else return False.
• not in --- Tells values not present in the collection. It will return True if the values are not present in
the collection else return False.
Practical proof:
'hi' in 'hi-bye'
True
'hb' in 'hi-bye'
False
1 in 123
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
1 in 123
TypeError: argument of type 'int' is not iterable
1 in '123'
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
1 in '123'
TypeError: 'in <string>' requires string as left operand, not int
'1' in '123'
True
[10] in [10,20,30]
False
[10] in [[10],20,30]
True
10 in [10,20,30]
True
10, in (10,20,30)
SyntaxError: invalid syntax
(10,) in (10,20,30)
False
(10) in (10,20,30)
True
10 not in {'a':10, 'b':20}
True
7) Identity Operator:
--- It is used to check whether both the variables are pointing to the same address or not.
• is ----- It will return True if both the variables are pointing to the same address or else return
False.
• is not ----- It will return True if both the variables are not pointing to the same address or else return
False.
Practical proof:
a=10
b=20
c=10
a is b
False
a is c
True
a is not b
True
a is not c
False
• Input Statement : We should never allow the user to modify the code, instead just allow them to access it.
Syntax:
var = input('Enter your message')
Practical proof:
• Output Statement:
--- Whenever we want to display the output we use output statement.
Syntax:
print(val1,val2,……….valn, sep=' ', end='\n')
Practical proof:
print(10,20,30,40) ------ 10 20 30 40
Programs:
# WAP to get the values from the even index in the tuple.
Day-16
Control Statement:
--- It is used to control the flow of execution.
Types:
Conditional Statement:
--- It is used to control the flow of execution based on conditions.
1) Simple if:
--- It is a keyword which is used to check the condition and it will execute the statement block if
the condition is True or else it will ignore the statement block.
Programs:
# Simple if
2) if else:
--- It is used to check the condition and it will execute the True Statement block if the condition is
True else it will execute the False Statement block.
Programs:
# if else
Note:
abs (absolute function) - It will convert the negative numbers into positive numbers. If we
already have positive number it will keep as it is.
Day-17
3) elif:
--- Whenever we want to check the multiple conditions and to execute statement blocks of
each and every condition we use elif.
Programs:
# elif
# WAP to check whether the character is uppercase or lowercase or digits or special characters
'''
ch = input('Enter the character: ')
if 'A'<=ch<='Z':
print('character is uppercase')
elif 'a'<=ch<='z':
print('character is lowercase')
elif '0'<=ch<='9':
print('character is digit')
else:
print('character is special character')'''
# WAP to check whether the number is single digit or two digit or three digit or more than 3 digit.
'''
n = abs(int(input('Enter the number: ')))
if 0<=n<=9:
print('single digit')
elif 10<=n<=99:
print('two digit')
elif 100<=n<=999:
print('three digit')
else:
print('more than three digit')'''
4) Nested if:
--- Whenever it is necessary to check a condition before checking another condition we use Nested
if.
Programs:
# Nested if
Day-18
Looping Statement:
--- It is a control statement which will control the flow of execution by repeating the same task again
and again.
Types:
• While loop
• For loop
1) While loop:
Program concepts Page 6
1) While loop:
--- It is used to execute the same set of instructions again and again until the condition become
False.
Note:
○ Initialization
○ Updation
Programs:
# while loop
'''
n = int(input('Enter the number: '))
i=2
while i <= 50:
print(i)
i = i + 2'''
Day-19
# WAP to find the factorial of a given integer / to find the prod of n natural numbers.
# Example: 5! --- 5*4*3*2*1
# Example: product upto 5 --- 1*2*3*4*5
'''
n=5
prod = 1
while n>0:
prod = prod * n
n=n-1
print(prod)'''
'''
i=1
prod = 1
while i<=5:
prod = prod * i
i=i+1
print(prod)'''
ord('A')
65
ord('a')
97
chr(65)
'A'
chr(ord('A')+32)
'a'
chr(ord('B')+32)
'b'
chr(ord('C')+32)
'c'
chr(ord('a')-32)
New Section 1 Page 1
chr(ord('a')-32)
'A'
chr(ord('b')-32)
'B'
Day-20
For loop:
--- It is self-iterative loop.
Advantage:
• It will allow us to use all the MVDT but in while loop it considers only string, list and tuple.
• No need of initialization and updation.
Range():
--- It is used to create a sequence of integers between the given value.
Syntax:
range(SV, EV+1,updation)
range(SV, EV-1,updation)
○ If updation == +1
range(SV, EV+-1)
○ If SV==0
range(EV+-1, updation)
range(1,10+1)
range(1, 11)
Programs:
# For loop
# Practice programs
'''
for i in [10,2.3,6+7j,78]:
print(i)'''
'''
for i in (12,34):
print(i)'''
'''
for i in {17,3.4,78,32}:
print(i)'''
'''
for i in {'a':10,'b':20,'c':30}:
print(i)'''
'''
for i in 'sakshi':
print(i,end=' ')'''
'''
for i in range(1,6):
print(i) '''
# WAP to check whether the string is palindrome or not without using slicing.
'''
s = input('Enter the string: ')
rev = ''
for i in s:
rev = i + rev
if rev == s:
print('palindrome')
else:
print('not palindrome')'''
Day-21
'''
t = eval(input('Enter the tuple: '))
out = {}
for i in t:
'''
l = eval(input('Enter the list: '))
out = {}
for i in l:
if type(i) == str:
out[i] = i[0]+i[-1]
print(out)'''
'''
s = input('Enter the string: ')
out = {}
for i in s:
if 'a'<=i<='z':
out[i] = chr(ord(i)-32)
elif 'A'<=i<='Z':
out[i] = chr(ord(i)+32)
print(out) '''
Note:
○ Split() --- it is used to split each word present in the string
○ Join() --- it is used to join/merge the strings present inside the collection.
'''
s = input('Enter the string: ')
out = []
a = s.split()
for i in a:
out.append(i[::-1])
print(' '.join(out)) '''
'''
s = input('Enter the string: ')
out = []
a = s.split()
for i in a:
Day - 22:
New Section 1 Page 6
Day - 22:
Syntax:
Example:
for i in range(1,5):
for j in range(1,3):
print(i,j)
Strong Number: If the number is equal to the sum of the factorial of individual digits, then we
can call that number as Strong Number.
'''
l = eval(input('Enter the list: '))
out = {}
for i in l:
if type(i) == str:
vow = ''
for j in i:
if j in 'AEIOUaeiou':
vow += j
out[i] = vow
print(out) '''
#Assignment
# Get the following output.
'''
Input : [12, 'program',4+2j, False,'holiday']
Output : {'program' : 'prgrm', 'holiday' : 'hldy'} '''
#Assignment
# Get the following output.
'''
Input : [12, 'program',4+2j, False,'holiday']
Output : {'program' : 'PROGRAM', 'holiday' : 'HOLIDAY'} '''
Patterns:
--- Using Nested for loop to print some unique structure or pattern.
Programs:
# Pattern
'''
***
for i in range(1,4):
print('*',end = ' ') '''
'''
***
for i in range(1,4):
for j in range(1,4):
print('*',end = ' ')
print() '''
'''
***
***
***
***
***
for i in range(1,6):
for j in range(1,4):
print('*',end = ' ')
print()'''
'''
*****
*****
for i in range(1,3):
for j in range(1,6):
print('*',end = ' ')
print() '''
'''
*
*
*
*
*
'''
'''
@
*@
**@
***@
****@
'''
'''
####$
###$&
##$&&
#$&&&
$&&&&
'''
'''
*****
* *
* *
* *
*****
'''
n = int(input('Enter the num: '))
for i in range(1,n+1):
for j in range(1,n+1):
if i == 1 or j == 1 or i == n or j ==n:
print('*',end = ' ')
else:
print(' ',end = ' ')
print()
'''
10000
01000
00100
00010
00001
'''
n = int(input('Enter the num: '))
for i in range(1,n+1):
for j in range(1,n+1):
if i ==j:
print('1',end = ' ')
else:
print('0',end = ' ')
print()
'''
* *
* *
*
* *
* *
'''
n = int(input('Enter the num: '))
for i in range(1,n+1):
for j in range(1,n+1):
if i == j or i+j == n+1:
print('*',end = ' ')
else:
print(' ',end = ' ')
print()
'''
*****
* *
'''
*
*
*****
*
*
'''
n = int(input('Enter the num: '))
for i in range(1,n+1):
for j in range(1,n+1):
if i == n//2+1 or j == n//2+1:
print('*',end = ' ')
else:
print(' ',end = ' ')
print()
'''
*********
** * **
* * * * *
* *** *
*********
* *** *
* * * * *
** * **
*********
'''
n = int(input('Enter the num: '))
for i in range(1,n+1):
for j in range(1,n+1):
if i==1 or j==1 or i==n or j==n or i==j or i+j==n+1 or i==n//2+1 or j==n//2+1:
print('*',end = ' ')
else:
print(' ',end = ' ')
print()
Day-23
'''
12345
12345
12345
12345
12345
New Section 1 Page 12
12345
12345
12345
12345
for j in range(1,n+1):
print(j,end = ' ')
print() '''
'''
11111
22222
33333
44444
55555
'''
1
12
123
1234
12345
'''
'''
23
23 24
23 24 25
23 24 25 26
23 24 25 26 27
'''
'''
5
54
543
5432
54321
'''
'''
5
54
543
5432
54321
'''
'''
* *
** **
* * *
* *
* *
'''
'''
* *
* *
* * *
** **
* *
'''
'''
**** * *
* * * *
* ** * * *
**** ** **
* * *
'''
for j in range(1,n+1):
if j == 1 or j == n or (i == j and i >= n//2+1 ) or (i+j==n+1 and i >= n//2+1):
print('*',end = ' ')
else:
print(' ',end = ' ')
print()
'''
****
*
***
*
****
'''
Assignment:
--- Print the first 4 letters in your name using patterns.
New Section 1 Page 15
--- Print the first 4 letters in your name using patterns.