Sunday, August 9, 2020

Using int, float and calculating them by casting the type and Operator Precedence in python3

In this post we are going to see, how to use the datatype int and float to do calculations. First we can see how to initialize values to a int or float variable

a = 50
b = 40.9

now we assigned two values to two variables
where for "a", int value is assigned, so the datatype for "a" will be int and for "b" float value is assigned, so the variable "b" will be float variable

you can check the type of the variable using 'type' keyword

>>> a = 50
>>> b = 40.9
>>> type(a)
<class 'int'>
>>> type(b)
<class 'float'>


Next we can see about casting, 

Code:
"""
Numeric Types: int, float castings

"""
import random

d = random.randrange(1,10)

print("type(d):",type(d))
print("d:", d)  
d = float(d)
print("type(d):",type(d))
print("d:", d)  
d = d +8.9
print("type(d):",type(d))
print("d:", d)
d = int(d)
print("type(d):",type(d))
print("d:", d)


the output
type(d): <class 'int'>
d: 4
type(d): <class 'float'>
d: 4.0
type(d): <class 'float'>
d: 12.9
type(d): <class 'int'>
d: 12


using random to get a number in the range of 1 to 10 to get assigned to the variable d, so it is a int variable and the value will be in integer

type(d): <class 'int'>
d: 4

Then we used float(d) get assigned to d, so here the d variable will be changed to float datatype and you can see the value is changed to float like 4.0

type(d): <class 'float'>
d: 4.0

and to change back to int we can use the int(value) method, to cast back to int.

Next we can see about a simple multiplication,

Code:
"""
Numeric Types: int, float
multiplying a int a float will result float
"""
a = 50
b = 40.9
res = a * b
print("type(a):",type(a))
print("type(b):",type(b))
print("type(res):",type(res))
print("a * b:", res)



here we are multiplying a int and a float value and assigned to a variable, and result will be in float and the variable's type will be float.

the output
type(a): <class 'int'>
type(b): <class 'float'>
type(res): <class 'float'>
a * b: 2045.0

Now we can see another program for division

Code:
"""
Numeric Types: int, float
Divide a int with float will result float
If result of the divide value is float means the result will be in float even that the variables are int
"""
a = 50
b = 40.9
c = 7
res = a / b
resInt = a/c
print("type(a):",type(a))
print("type(b):",type(b))
print("type(res):",type(res))
print("type(resInt):",type(res))
print("a / b:", res, "rounded to 4 decimal:", round(res,4))
print("a / c:", resInt)



the output
type(a): <class 'int'>
type(b): <class 'float'>
type(res): <class 'float'>
type(resInt): <class 'float'>
a / b: 1.2224938875305624 rounded to 4 decimal: 1.2225
a / c: 7.142857142857143


here you can see the output of dividing a / b has more values after the decimal, so if we need only 5 values after the decimal point means we can use round method as shown in the above code, while using this if the sixth value is more than 4 means the 5th place value will be increased by 1.

We can see another example for calculation

Code:
"""
Numeric Types: int, float
Calculations
"""
a = 80
b = 4
c = 9
addition = a + b
subtraction = addition - c
divide = a / b
multiply = b * c
calculation = a + b / 2 - (a / b)
print ("addition:",addition)
print ("subtraction:",subtraction)
print ("divide:",divide)
print ("multiply:",multiply)
print ("calculation:",calculation)


the output
addition: 84
subtraction: 75
divide: 20.0
multiply: 36
calculation: 62.0


here addition, subtraction, division and multiply is normal things, but we are having a code

calculation = a + b / 2 - (a / b)

here we can see how the calculation done here step by step

calculation = a + b / 2 - (a / b)
we can write the equalent value of the variable
calculation = 80 + 4 / 2 - (80 / 4)
the calcuation will be done from left to right
and the order is as followed 

Operator Precedence
1 P Parentheses
2 E Exponent
3 M Multiplication
4 D Division
5 A Addition
6 S Subtraction


so we can see the steps to get the result

calculation = 80 + 4 / 2 - (80 / 4)
calculation = 80 + 4 / 2 - 20
calculation = 80 + 2 - 20
calculation = 82 - 20
calculation = 62

You can see the below video for better understanding of this post




In next post we can see about using strings

No comments:

Post a Comment

Conditional Statement if, if and else, if and elif with else using Arithmetic Operators and Logical Operators in python3

 If you are new to coding means conditional statement will be unfamiliar to you. For that we can take a real-time example for understanding ...