Search Here

Numbers in python

Hello, how are you guys!!! I hope you will be fine and also enjoying this tutorial. In my previous post you learned about  python mutable and immutable objects in detail. You also learned the difference between mutable and immutable objects. Today you will learn "Numbers" in python with example.


Numbers in Python:
       There are basically two different kinds of numbers in Python. Here in the below screen shot we define a variable. And we will  give it a value (num = 30). And then we will print that variable. You see we get that value.

1. #!/usr/bin/python3
2. def main():
3.    num = 30
4.    print(num)
5. if __name__ == "__main__": main()


Out Put:
30

Numbers in python 

 We can also check the type of number. In the code below in line no 4 we use "type()" function to check the type of a given number.
1. #!/usr/bin/python3
2. def main():
3.    num = 30
4.    print(type(num), num)
5. if __name__ == "__main__": main()

If we run the above code we will get both the type and the value (we see that its class is int, and its value is 30). 

Out Put:
<class 'int'> 30
 
If we declare another variable like num1 and set its value to 30.0, now its type is going to be <class 'float' > 30.0. So those are the two different kinds of numbers in Python. In the below screen shot you see the difference between two.

Numbers in python 

 If I take an integer like "div", and I say div = 30/ 9, I am no longer going to have an integer. If I save this and run it, we will see that I have class 'float' and that it is this real number with a whole lot of digits after the decimal point. If I want to get an integer division, here in the line number 5 I can use two slashes instead of one. And now what I will get, instead of 3.3333333333333335, I will get just 3.

1.#!/usr/bin/python3
2.def main():
3.    div = 30 / 9
4.    print(type(div), div)
   
5.    div1 = 30 // 9
6.    print(type(div1), div1)
       
7.if __name__ == "__main__": main() 

Out Put:
<class 'float'> 3.3333333333333335
<class 'int'> 3

Numbers in python  

Now here you will notice that it is not rounded up. So if I want it to be rounded, I can type round ( div = round(30 / 9) ) and get back the floating point division and save that and run it, now we have it rounded up. It's a 3.

1.#!/usr/bin/python3
2.def main():
3.   div = round(30 / 9)
4.    print(type(div), div)
       
5.if __name__ == "__main__": main()

Out Put:
<class 'int'> 3

I can give another argument to round it to how many digits I want it to round to. For example if want to round a number with 2 digits than the above code look (line # 3) like this div = round(30 / 9, 2)  I can save that and run it. Now the out put is <class 'float'> 3.33.

In python I might also want the remainder, which is also called modulo. We can find out modulo of a number with "%" sign. In python type casting is very easy. If I have a floating point number, say 30.9, but I want to make sure that it's actually going to be read as an integer, I can say int of, like this "int(30.9)", and save that and run it, and I get just the 30 part. Likewise, if I have an integer number and I want the floating point representation of it, I can say float like that "float(30)" and save that and run it. And I actually get a floating point representation of the integer 30. 

           Float and int are actually object constructors. So when I say float 30, what I am actually doing is I am creating an object of type float, and I am giving it an initial value of 42. That's getting passed to the constructor. So those are the basic ways that you can use and create integers and floating point numbers in Python 3. Below is the complete code of the above description.

#!/usr/bin/python3
def main():
    div = round(30 / 9, 2)
    print(type(div), div)
   
    modulo = 30 % 9
    print(type(modulo), modulo)
   
    type_cast = int(30.9)
    print(type(type_cast), type_cast)
   
    type_cast = float(30)
    print(type(type_cast), type_cast)
       
if __name__ == "__main__": main()

Out Put:
<class 'float'> 3.33
<class 'int'> 3
<class 'int'> 30
<class 'float'> 30.0
Numbers in python 

Today you learned about numbers in python. We learned that there are two types of numbers in python. First is "int" and the second one is "float". We also learned that how to convert "int" into "float" and vice verse. In my next post you will learn about "strings" in python with example.

Post a Comment

0 Comments