Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

In [1]:  from IPython.core.

interactiveshell import InteractiveShell


InteractiveShell.ast_node_interactivity = "all"

#this is how we display all the values in a cell otherwise output will just display result of last expression

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.

In [3]:  #some addtion.


1+1
1+3
3+-4
-9+5
10-89
100+1000000

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

In [9]:  #modulus operator gives reminder


5%2
10%3
10%2
10%4

Out[9]: 1

Out[9]: 1

Out[9]: 0

Out[9]: 2

In [10]:  #raise to power


2**100
2**2
2**5

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

In [12]:  #now python does not cross boundries across types


1+"a"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[12], line 2
1 #now python does not cross boundries across types
----> 2 1+"a"

TypeError: unsupported operand type(s) for +: 'int' and 'str'

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.

In [15]:  from decimal import *


a=0.1
b=0.2
c=Decimal(a+b)
c

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

In [18]:  ###lets try to set percision to 10.


getcontext().prec = 4
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
######################
######################
a=0.1
b=0.2
z= decimal.Decimal(a)+ decimal.Decimal(b)
z
c==z
c==d

Out[18]: Decimal('0.3')

Out[18]: Decimal('0.3000')

Out[18]: Decimal('0.3000')

Out[18]: True

Out[18]: True

In [19]:  #does not work for float


a=0.1
b=0.2
c=float(str(a))+float(str(b))
c

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)

type of variable is----> <class 'int'> and value is 2


now will convert it into float
type of a is now <class 'float'> and value is 2.0

type of variable is----> <class 'float'> and value is 5.6


now will convert it into int
type of a is now <class 'float'> and value is 5.6

type of variable is----> <class 'str'> and value is 3


now will convert it into float
type of a is now <class 'float'> and value is 3.0

type of variable is----> <class 'str'> and value is 5.6


now will convert it into float
type of a is now <class 'float'> and value is 5.6

type of variable is----> <class 'int'> and value is 5


now will convert it into complex
type of a is now <class 'complex'> and value is (5+0j)

type of variable is----> <class 'float'> and value is 6.5


now will convert it into complex
type of a is now <class 'complex'> and value is (5.6+0j)

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)

ValueError: complex() arg is a malformed string

In [26]:  # importing "math" for mathematical operations


import math

a = 3.5

# returning the ceil of 3.5
print ("The ceil of 3.5 is : ", end="")
print (math.ceil(a))

# returning the floor of 3.5
print ("The floor of 3.5 is : ", end="")
print (math.floor(a))

# find the power
print ("The value of 3.5**2 is : ",end="")
print (pow(a,2))

# returning the log2 of 16
print ("The value of log2 of 3.5 is : ", end="")
print (math.log2(a))

# print the square root of 3.5
print ("The value of sqrt of 3.5 is : ", end="")
print(math.sqrt(a))

# returning the value of sine of 3.5
print ("The value of sine of 3.5 is : ", end="")
print (math.sin(a))

The ceil of 3.5 is : 4


The floor of 3.5 is : 3
The value of 3.5**2 is : 12.25
The value of log2 of 3.5 is : 1.8073549220576042
The value of sqrt of 3.5 is : 1.8708286933869707
The value of sine of 3.5 is : -0.35078322768961984

In [ ]:  ​

In [ ]:  ​

In [ ]:  ​

In [ ]:  ​
In [ ]:  ​

You might also like