To store numerical values in the program, we use Numbers. When you assign a value to objects, a number of objects are created. In python, numbers are also taken as objects.
There are three types of numbers in python, given below
- Int
- Float
- Complex
To create a number variable in Python:
Python Numbers can be created by directly assigning the value to a variable.
To create any number variable Syntax, is as given below,
Example:
To find the type of variable:
type( ) function is used to find the type of number in python.
Syntax is as given below,
a = 88 b = 657.09 print(type(a)) print(type(b))
Int:
Int / Integer can be described as a whole number. They can be positive or negative. They should be without decimals and can be of unlimited length.
int() function is used to get the integer representation of an object. The object must implement the __int__() method that returns an integer.
value = 777 #intnumber negative_no = -777 print(type(value)) print(value) print(negative_no)
Float:
Floating-point or Float is a number that can positive or negative and can be consist on one or more decimals.
float() function is used to get the float representation of an object. The object must implement the __float__() method that returns a floating-point number.
a1 = 78.98 a2 = -45.32 print(type(a1)) print(a2)
Complex:
Complex numbers consist of two parts one as ‘real part’ other as ‘imaginary part denoted as ‘j’.
complex() function is used to get the complex representation of an object. We can also use the complex() function to create a complex number. The first value is the real part and the other value is the complex part.
m = 8 + 6j print(m) print(type(m))
Type Conversion
Type conversion can be done on numbers to convert their type. Number’s type can be change with the help of Int() , float() and complex() functions. For better understanding consider this example given below,
m = 565 # Int n = 12.89 # float o = 5 + 5j # complex #conversion from Int to float: x = float(m) #conversion from float to Int: y = int(n) #conversion from Int to complex: z = complex(o) print(x) print(type(x)) print(y) print(type(y)) print(z) print(type(z))