0% found this document useful (0 votes)
12 views7 pages

Lab 1

The document discusses various numeric operations in Python like arithmetic, exponents, modulus, and type conversions. It explores differences between float and decimal representations, and how to resolve issues with float precision. Various data types like int, float, complex, and decimal are covered along with type checking functions.

Uploaded by

tabish.khan.da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Lab 1

The document discusses various numeric operations in Python like arithmetic, exponents, modulus, and type conversions. It explores differences between float and decimal representations, and how to resolve issues with float precision. Various data types like int, float, complex, and decimal are covered along with type checking functions.

Uploaded by

tabish.khan.da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

In [30]:  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 [31]:  #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 [32]:  #some addtion.


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

Out[32]: 2

Out[32]: 4

Out[32]: -1

Out[32]: -4

Out[32]: -79

Out[32]: 1000100

In [33]:  #subtraction
99-1
60-+5
91-91
0-81

Out[33]: 98

Out[33]: 55

Out[33]: 0

Out[33]: -81

In [34]:  #multiplication
56*9
2*2
1*0
0*99
6*3

Out[34]: 504

Out[34]: 4

Out[34]: 0

Out[34]: 0

Out[34]: 18
In [35]:  #division
6/2
2/2
100/4
3/2
10/3
##############floor division
6//2
2//2
100//4
3//2
10//3

Out[35]: 3.0

Out[35]: 1.0

Out[35]: 25.0

Out[35]: 1.5

Out[35]: 3.3333333333333335

Out[35]: 3

Out[35]: 1

Out[35]: 25

Out[35]: 1

Out[35]: 3

In [36]:  ​
10/4
10/4.0
10//4
10//4.0

Out[36]: 2.5

Out[36]: 2.5

Out[36]: 2

Out[36]: 2.0

In [37]:  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[37]: float

Out[37]: int

In [38]:  #modulus operator gives reminder


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

Out[38]: 1

Out[38]: 1

Out[38]: 0

Out[38]: 2

In [39]:  #raise to power


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

Out[39]: 1267650600228229401496703205376

Out[39]: 4

Out[39]: 32
In [40]:  #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[40]: 7.0

Out[40]: 10.0

Out[40]: 77.9

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


1+"a"

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

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

In [42]:  a=0.1
b=0.2
c=a+b
0.3==c#why
#lets see wha is the value of c
c

Out[42]: False

Out[42]: 0.30000000000000004

In [43]:  #the problem is not with the python but with representation of floating numbers
#binary floating representation
#finite precisions
#iEEE 754

In [44]:  #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 [45]:  import decimal


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

Out[45]: Decimal('0.3000000000000000444089209850062616169452667236328125')

In [46]:  ​
a=0.1
b=0.2
c=Decimal(a+b)
c
#it does not reslove the problem but we can get over it by using decimal but we make sure we pass numbers as string.

Out[46]: Decimal('0.3000000000000000444089209850062616169452667236328125')
In [47]:  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[47]: Decimal('0.3')

Out[47]: Decimal('0.3000')

c==d True

Out[47]: Decimal('0.3000')

Out[47]: True

In [56]:  ###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[56]: Decimal('0.3')

Out[56]: Decimal('0.3000')

Out[56]: Decimal('0.3000')

Out[56]: True

Out[56]: True

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


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

Out[49]: 0.30000000000000004

In [50]:  a=0.1
b=0.2
c= decimal.Decimal(a)+ decimal.Decimal(b)
c

Out[50]: Decimal('0.3000')
In [51]:  #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[51]: str

Out[51]: int

Out[51]: float

Out[51]: decimal.Decimal

Out[51]: complex
In [52]:  # 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 [53]:  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[53], 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 [54]:  #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 [57]:  #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[57], line 7
4 print(a)
6 a="four"
----> 7 a=complex(a)

ValueError: complex() arg is a malformed string

In [58]:  # 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

You might also like