Thursday, April 16, 2015

Python Variables - Numbers

Everything in python3 is object.
Every object has its own id, type and value.

id is unique identifier of the object.
type is class of the object.
values is the contents of the object.

Ex:
#!/usr/bin/python3
x=9
print(x)
print(type(x))
print(id(x))


Output:
9
<class 'int'>
10455296


Numbers:

Numbers assigned to variable can be two types: integer and float

Integer is a whole number whereas float is a decimal number.

Integer example:

no=56
print(type(no),no) 


Output:
<class 'int'> 56


Float example:
no=56.0
print(type(no),no)



Output:
<class 'float'> 56.0

Examples:
no=45/7
print(type(no),no)


Output:
<class 'float'> 6.428571428571429

Rounding the number
no=round(45/7)
print(type(no),no)


Output:
<class 'int'> 6

Setting to two decimal points.
no=round(45/7, 2)
print(type(no),no)


Output:
<class 'float'> 6.43


No comments:

Post a Comment