Numbers - 1 - Jupyter Notebook
Numbers - 1 - Jupyter Notebook
Numbers - 1 - Jupyter Notebook
In [2]: #now note we are not using print statment/ function to print results as we are in intractive mode.
#only diffrence is that in juypter notebook we can control when to run code as in defalut intractive mode it runs
# immediately after we press enter.
Out[3]: 2
Out[3]: 4
Out[3]: -1
Out[3]: -4
Out[3]: -79
Out[3]: 1000100
In [4]: #subtraction
99-1
60-+5
91-91
0-81
Out[4]: 98
Out[4]: 55
Out[4]: 0
Out[4]: -81
In [5]: #multiplication
56*9
2*2
1*0
0*99
6*3
Out[5]: 504
Out[5]: 4
Out[5]: 0
Out[5]: 0
Out[5]: 18
In [6]: #division
6/2
2/2
100/4
3/2
10/3
##############floor division
6//2
2//2
100//4
3//2
10//3
Out[6]: 3.0
Out[6]: 1.0
Out[6]: 25.0
Out[6]: 1.5
Out[6]: 3.3333333333333335
Out[6]: 3
Out[6]: 1
Out[6]: 25
Out[6]: 1
Out[6]: 3
In [7]:
10/4
10/4.0
10//4
10//4.0
Out[7]: 2.5
Out[7]: 2.5
Out[7]: 2
Out[7]: 2.0
In [8]: type(3/2) # true/ classic division returns quotient of the division,including reminder......result is float
type(6//2)# floor divisionreturns quotient rounded to nearest interger........result is int.
Out[8]: float
Out[8]: int
Out[9]: 1
Out[9]: 1
Out[9]: 0
Out[9]: 2
Out[10]: 1267650600228229401496703205376
Out[10]: 4
Out[10]: 32
In [11]: #now look at this rules of pyhton.......
#python ranks the complexity of numeric types so:integers are simpler than floating which is simpler
#than complex numbers.so when we have mixed types of numerical in python expression result is always of
#highest complexity type.
1+5+1.0
5*2.0
78+-.1
Out[11]: 7.0
Out[11]: 10.0
Out[11]: 77.9
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[12], line 2
1 #now python does not cross boundries across types
----> 2 1+"a"
In [13]: a=0.1
b=0.2
c=a+b
0.3==c#why
#lets see wha is the value of c
c
Out[13]: False
Out[13]: 0.30000000000000004
In [ ]: #the problem is not with the python but with representation of floating numbers
#binary floating representation
#finite precisions
#iEEE 754
In [ ]: #to resolve this we can use decimal module and its decimal data type.
#but it comes with trade off with of speed and memory.
Out[15]: Decimal('0.3000000000000000444089209850062616169452667236328125')
In [17]: a=0.1
b=0.2
c=Decimal(str(a))+Decimal(str(b))
c
a=0.1
b=0.2
d=Decimal(a)+Decimal(b)
d
print("c==d",c==d)
######################
######################
a=0.1
b=0.2
z= decimal.Decimal(a)+ decimal.Decimal(b)
z
c==z
Out[17]: Decimal('0.3')
Out[17]: Decimal('0.3000000000000000166533453694')
c==d False
Out[17]: Decimal('0.3000000000000000166533453694')
Out[17]: False
Out[18]: Decimal('0.3')
Out[18]: Decimal('0.3000')
Out[18]: Decimal('0.3000')
Out[18]: True
Out[18]: True
Out[19]: 0.30000000000000004
In [21]: #Q how do we know type of an object/variable
a="tabish"
b=12
c=0.12
d=Decimal('334.56')
num = 6 + 9j
#to know type of variable we will use type function
type(a)
type(b)
type(c)
type(d)
type(num)
Out[21]: str
Out[21]: int
Out[21]: float
Out[21]: decimal.Decimal
Out[21]: complex
In [22]: # We can also use built-in functions like int(), float() and complex() to convert into different types explicitly.
a = 2
print("type of variable is---->",type(a),"and value is",a)
print("now will convert it into float")
a=float(a)
print("type of a is now",type(a),"and value is",a)
b = 5.6
print("\n\ntype of variable is---->",type(b),"and value is",b)
print("now will convert it into int")
b=float(b)
print("type of a is now",type(b),"and value is",b)
c = '3'
print("\n\ntype of variable is---->",type(c),"and value is",c)
print("now will convert it into float")
c=float(c)
print("type of a is now",type(c),"and value is",c)
d = '5.6'
print("\n\ntype of variable is---->",type(d),"and value is",d)
print("now will convert it into float")
d=float(d)
print("type of a is now",type(d),"and value is",d)
e = 5
print("\n\ntype of variable is---->",type(e),"and value is",e)
print("now will convert it into complex")
e=complex(e)
print("type of a is now",type(e),"and value is",e)
f = 6.5
print("\n\ntype of variable is---->",type(f),"and value is",f)
print("now will convert it into complex")
f=complex(d)
print("type of a is now",type(f),"and value is",f)
In [23]: a=5.6+0j
a=int(a)
print(a)
#We can’t convert a complex data type number into int data type and float data type numbers.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 2
1 a=5.6+0j
----> 2 a=int(a)
3 print(a)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'
In [24]: #here a way to slove this problem
a=5.6+0j
b=int(a.real)
print(b)
c=int(a.imag)
print(c)
#we can assign either real or imaginery part to variable
5
0
In [25]: #We can’t apply complex built-in functions on strings if it not represented as number.
a="5.6+0j"
a=complex(a)
print(a)
a="four"
a=complex(a)
(5.6+0j)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[25], line 7
4 print(a)
6 a="four"
----> 7 a=complex(a)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: