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,
"""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.9print("type(d):",type(d))print("d:", d)d = int(d)print("type(d):",type(d))print("d:", d)
the outputtype(d): <class 'int'>d: 4type(d): <class 'float'>d: 4.0type(d): <class 'float'>d: 12.9type(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,
"""Numeric Types: int, floatmultiplying a int a float will result float"""a = 50b = 40.9res = a * bprint("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
"""Numeric Types: int, floatDivide a int with float will result floatIf result of the divide value is float means the result will be in float even that the variables are int"""a = 50b = 40.9c = 7res = a / bresInt = a/cprint("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
"""Numeric Types: int, floatCalculations"""a = 80b = 4c = 9addition = a + bsubtraction = addition - cdivide = a / bmultiply = b * ccalculation = a + b / 2 - (a / b)print ("addition:",addition)print ("subtraction:",subtraction)print ("divide:",divide)print ("multiply:",multiply)print ("calculation:",calculation)
the outputaddition: 84subtraction: 75divide: 20.0multiply: 36calculation: 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