Python Class Basics and Data Types
Python Class Basics and Data Types
In [2]: 2*2
Out[2]: 4
In [4]: this
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 1
----> 1 this
In [5]: #this
#is
#a
#pyhton class
In [6]: """this
is
a python class"""
In [7]: a=10
In [8]: type(a)
Out[8]: int
In [9]: b=10.0
type(b)
Out[9]: float
In [10]: c=2+3i
In [11]: c=2+2j
In [12]: type(c)
Out[12]: complex
In [13]: a=True
In [14]: type(a)
Out[14]: bool
In [16]: type(s)
Out[16]: str
In [17]: l=[10,20,1,"sunil",20.5,[1,2,3]]
In [18]: t=(0,20,1,"sunil",20.5,[1,2,3])
In [19]: type(l)
Out[19]: list
In [20]: type(t)
Out[20]: tuple
In [1]: s={10,12,13,1,2,3,4,1,2,2,2}
In [2]: s
In [3]: c=None
In [4]: type(c)
Out[4]: NoneType
In [5]: type(s)
Out[5]: set
In [7]: d
In [8]: type(d)
Out[8]: dict
In [10]: d
In [11]: 2+2
Out[11]: 4
In [12]: 2+2.3
Out[12]: 4.3
In [13]: 2*3
Out[13]: 6
In [14]: 2-3
Out[14]: -1
In [15]: 2/3
Out[15]: 0.6666666666666666
In [16]: 3/3
Out[16]: 1.0
In [17]: 10%3
Out[17]: 1
In [18]: 10/3
Out[18]: 3.3333333333333335
In [19]: 10//3
Out[19]: 3
In [20]: 2**3
Out[20]: 8
In [21]: 10>20
Out[21]: False
In [22]: 10<20
Out[22]: True
In [23]: 10==20
Out[23]: False
In [24]: 10>=10
Out[24]: True
In [25]: 10!=20
Out[25]: True
In [26]: a=10
In [28]: a
Out[28]: 20
Out[29]: False
Out[30]: False
In [32]: 20 and 10
Out[32]: 10
In [33]: 20 or 10
Out[33]: 20
In [34]: a= 10
In [35]: b=not(a)
In [36]: b
Out[36]: False
In [37]: l=[10,20,1,200,13,9]
In [39]: 10 in l
Out[39]: True
In [40]: 9 not in l
Out[40]: False
In [41]: 'W' in s
Out[41]: False
In [42]: a=10
In [43]: b=20
In [44]: c=10
In [45]: a is b
Out[45]: False
In [46]: d=20-10
In [47]: c is d
Out[47]: True
In [48]: id(a)
Out[48]: 140705341875608
In [49]: id(c)
Out[49]: 140705341875608
In [50]: id(b)
Out[50]: 140705341875928
In [51]: a=10-20/20+10*0
In [52]: a
Out[52]: 9.0
1020
sum=int(a)+int(b)
print(sum)
30
30
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[56], line 1
----> 1 a=int(input("Enter the 1st number"))
2 b=int(input("enter the 2nd number"))
3 sum=a+b
30.5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[58], line 1
----> 1 a=float(input("Enter the 1st number"))
2 b=float(input("enter the 2nd number"))
3 sum=a+b
30.5
fail
1st div
2nd div
3rd div
fail
fail
In [15]: print("Nielit")
print("Nielit")
print("Nielit")
print("Nielit")
print("Nielit")
Nielit
Nielit
Nielit
Nielit
Nielit
0
1
2
3
4
1
2
3
4
5
6
7
8
9
10
2
4
6
8
10
2
4
6
8
10
In [23]: l=[10,20,30,40,50,60]
for i in l:
print(i)
10
20
30
40
50
60
In [24]: l=[10,20,30,40,50,60]
for i in l:
print(l)
In [25]: a=1
while a<10:
print(a)
a=a+1
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
8
9
10
0
1
2
3
4
5
6
print('*')
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
1
1 2
1 2 3
1 2 3 4
In [1]: l=[1,2,3,4,[10,20,30,40],"Welcome",1.5,1.3,1.5]
In [2]: l[0]
Out[2]: 1
In [3]: l[5]
Out[3]: 'Welcome'
In [4]: len(l)
Out[4]: 9
In [5]: l[9]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[5], line 1
----> 1 l[9]
In [6]: l[8]
Out[6]: 1.5
In [7]: l[-1]
Out[7]: 1.5
In [8]: l
Out[8]: [1, 2, 3, 4, [10, 20, 30, 40], 'Welcome', 1.5, 1.3, 1.5]
In [9]: l[4]
In [10]: l[4][1]
Out[10]: 20
In [11]: l[4][1]=200
In [12]: l
Out[12]: [1, 2, 3, 4, [10, 200, 30, 40], 'Welcome', 1.5, 1.3, 1.5]
In [13]: for i in l:
print(i)
1
2
3
4
[10, 200, 30, 40]
Welcome
1.5
1.3
1.5
1
2
3
4
[10, 200, 30, 40]
Welcome
1.5
1.3
1.5
In [16]: l[:]
Out[16]: [1, 2, 3, 4, [10, 200, 30, 40], 'Welcome', 1.5, 1.3, 1.5]
In [17]: l[:5]
In [18]: l[2:5]
In [19]: l[::2]
In [20]: s="Welcome"
In [21]: s[::-1]
Out[21]: 'emocleW'
In [22]: l1=[1,2,3,4]
l2=[10,20,30,40]
In [23]: l1+l2
In [24]: l1*l2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[24], line 1
----> 1 l1*l2
In [25]: l
Out[25]: [1, 2, 3, 4, [10, 200, 30, 40], 'Welcome', 1.5, 1.3, 1.5]
In [26]: l1*4
Out[26]: [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
In [27]: 1 in l
Out[27]: True
In [28]: s
Out[28]: 'Welcome'
In [29]: l4=list(s)
In [30]: l4
In [31]: l
Out[31]: [1, 2, 3, 4, [10, 200, 30, 40], 'Welcome', 1.5, 1.3, 1.5]
In [32]: [Link](500)
In [33]: l
Out[33]: [1, 2, 3, 4, [10, 200, 30, 40], 'Welcome', 1.5, 1.3, 1.5, 500]
In [35]: l5=[]
for i in range(7):
num=int(input("enter the number"))
[Link](num)
print(l5)
[1, 2, 3, 4, 5, 6, 7]
In [36]: l5
Out[36]: [1, 2, 3, 4, 5, 6, 7]
In [37]: l
Out[37]: [1, 2, 3, 4, [10, 200, 30, 40], 'Welcome', 1.5, 1.3, 1.5, 500]
In [38]: [Link](l5)
In [39]: l
Out[39]: [1,
2,
3,
4,
[10, 200, 30, 40],
'Welcome',
1.5,
1.3,
1.5,
500,
1,
2,
3,
4,
5,
6,
7]
In [40]: [Link](2,2000)
In [41]: l
Out[41]: [1,
2,
2000,
3,
4,
[10, 200, 30, 40],
'Welcome',
1.5,
1.3,
1.5,
500,
1,
2,
3,
4,
5,
6,
7]
In [42]: [Link](2000)
In [43]: l
Out[43]: [1,
2,
3,
4,
[10, 200, 30, 40],
'Welcome',
1.5,
1.3,
1.5,
500,
1,
2,
3,
4,
5,
6,
7]
In [44]: [Link]()
Out[44]: 7
In [45]: [Link](7)
Out[45]: 1.3
In [46]: min(l)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[46], line 1
----> 1 min(l)
In [47]: min(l5)
Out[47]: 1
In [48]: l5
Out[48]: [1, 2, 3, 4, 5, 6, 7]
In [49]: l6=l5
In [50]: l6
Out[50]: [1, 2, 3, 4, 5, 6, 7]
In [51]: l6[5]=500
In [52]: l6
In [53]: l5
In [54]: l6=l5[:]
In [57]: l6
In [58]: l6[5]=6
In [59]: l6
Out[59]: [1, 2, 3, 4, 5, 6, 7]
In [60]: l5
In [61]: l7=list(l5)
In [62]: l7
In [63]: l7[5]=6
In [64]: l7
Out[64]: [1, 2, 3, 4, 5, 6, 7]
In [1]: t=(1,2,3,4,5,(10,20,30))
In [2]: type(t)
Out[2]: tuple
In [3]: t[4]
Out[3]: 5
In [9]: t[5][0]
Out[9]: 10
In [5]: len(t)
Out[5]: 6
In [6]: t=(1,2,3,4,5,[10,20,30])
In [8]: t[5][0]
Out[8]: 10
1
2
3
4
5
[10, 20, 30]
In [11]: t[2]=100
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[11], line 1
----> 1 t[2]=100
In [12]: t+t
In [13]: [Link](6)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[13], line 1
----> 1 [Link](6)
In [14]: t+(6,)
In [15]: t[1:4]
Out[15]: (2, 3, 4)
In [16]: t[::2]
Out[16]: (1, 3, 5)
In [17]: t
In [18]: [Link](4)
Out[18]: 1
In [19]: [Link](6)
Out[19]: 0
In [20]: [Link](4)
Out[20]: 3
In [21]: len(t)
Out[21]: 6
In [22]: a=20
b=30
a,b=b,a
print(a)
print(b)
30
20
In [24]: type(s)
Out[24]: str
In [25]: s[1]
Out[25]: 'e'
In [26]: s[1]='E'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[26], line 1
----> 1 s[1]='E'
In [27]: a=20
In [28]: a=30
In [29]: for i in s:
print(i)
W
e
l
c
o
m
e
t
o
N
i
e
l
i
t
P
a
t
n
a
W
e
l
c
o
m
e
t
o
N
i
e
l
i
t
P
a
t
n
a
In [32]: s[0:7]
Out[32]: 'Welcome'
In [33]: s[::-1]
In [34]: 'w' in s
Out[34]: False
In [36]: s is s1
Out[36]: False
In [43]: s is s2
Out[43]: False
In [44]: s2 is s
Out[44]: False
In [45]: s
In [46]: s+s2
In [47]: s*3
In [48]: s
In [51]: s5=[Link]('W','w')
In [50]: s
In [52]: s5
In [53]: _
In [2]: d['Name']
Out[2]: 'deepraj'
Out[3]: 25
In [4]: d['Nmae']='suraj'
In [5]: d
Out[5]: {'Name': 'deepraj', 'roll no': 25, 'program': 'MCA', 'Nmae': 'suraj'}
In [6]: d['Name']='Mohit'
In [7]: d
Out[7]: {'Name': 'Mohit', 'roll no': 25, 'program': 'MCA', 'Nmae': 'suraj'}
In [9]: d1
In [10]: d['same']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[10], line 1
----> 1 d['same']
KeyError: 'same'
In [11]: for i in d:
print(i)
Name
roll no
program
Nmae
('Name', 'Mohit')
('roll no', 25)
('program', 'MCA')
('Nmae', 'suraj')
Name : Mohit
roll no : 25
program : MCA
Nmae : suraj
In [15]: dict(pair1)
In [17]: functionname()
Welcome to nielit
In [18]: functionname()
Welcome to nielit
In [19]: len(d)
Out[19]: 4
In [21]: add()
30
sum=a+b
print(sum)
In [23]: add1(10,30)
40
In [24]: c=add1(10,20)
30
In [26]: print(c)
None
return sum
In [29]: print(add1(2,3))
In [30]: c=add1(2,3)
In [31]: print(c)
In [33]: add2(1,2,3)
Out[33]: 6
In [34]: add2(2,3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[34], line 1
----> 1 add2(2,3)
In [36]: add2(2,3)
Out[36]: 5
In [39]: add2(2,3,4)
Out[39]: 9
In [42]: add2(2,3)
Out[42]: 14
In [45]: add2()
Out[45]: 0
In [46]: add2(1)
Out[46]: 1
In [47]: add2(2,3)
Out[47]: 5
In [48]: add2(2,3,4)
Out[48]: 9
In [49]: add2(2,3,4,5)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[49], line 1
----> 1 add2(2,3,4,5)
In [60]: a1=10
def add():
a1=20
b1=30
print(a1+b1)
print(a1)
add()
10
50
In [2]: add1(2,3,4)
Out[2]: 9
In [4]: add2(1)
Out[4]: 1
In [5]: add2(1,2)
Out[5]: 3
In [6]: add2(2,3,4,5)
Out[6]: 14
In [8]: fun(a=10,b=20,d=30,c=40)
a = 10
b = 20
d = 30
c = 40
In [10]: d(2)
Out[10]: 4
In [11]: d(3)
Out[11]: 9
In [13]: d1(2,3)
Out[13]: 5
In [16]: l=[1,2,3,4]
square=map(lambda x:x**2,l)
square_list=list(square)
print(square_list)
[1, 4, 9, 16]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[24], line 5
3 return True
4 l=[1,2,3,4,5,6,7]
----> 5 d=filter((is_even(),l))
6 e=list(d)
7 print(e)
In [26]: [Link](4)
Out[26]: 2.0
In [27]: mul(2,3)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[27], line 1
----> 1 mul(2,3)
In [29]: mul(2,3)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[29], line 1
----> 1 mul(2,3)
In [30]: [Link](2,3)
Out[30]: 6
In [31]: [Link](3,2)
Out[31]: 5
In [33]: add(3,4)
Out[33]: 7
In [35]: arr1=[Link]([1,2,3,4,5])
In [37]: print(type(arr1))
<class '[Link]'>
In [38]: l=[10,20,30,40]
arr2=[Link](l)
In [39]: arr2
In [41]: [Link]
Out[41]: (4,)
In [42]: [Link]
Out[42]: 1
In [43]: [Link]
Out[43]: 4
In [44]: arr3=[Link]([[1,2,3],[4,5,6]])
In [45]: arr3
In [46]: [Link]
Out[46]: (2, 3)
In [47]: [Link]
Out[47]: 2
In [ ]: