Numbers in Python

In Python there are two kind of numbers. One is integer and the other is float. We have in built methods in Python that can change an integer to float and change float to integer.

I hope you will understand the code below. The output is self explanatory. Read the comment also.


#!/usr/bin/python3
def main():
    x = 3
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
    x = 3 /2
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
    x = round(42 / 9)
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
    # we want to round it up
    x = 42 // 9
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
    # how many digits we want to round to
    x = round(42 / 9, 3)
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
    x = 43 % 7
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
    x = int(34.78)
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
    x = float(23)
    print(x)
    print(id(x))
    print(type(x))
    print("*********")
if __name__ == "__main__":
    main()

3
10455104
class ‘int’
*********
1.5
140223146811728
class ‘float’
*********
4
10455136
class ‘int’
*********
5
140223146823568
class ‘int’
*********
4.667
140223146811968
class ‘float’
*********
1
10455040
class ‘int’
*********
34
10456096
class ‘int’
*********
23.0
140223146811968
class ‘float’
*********

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s