Python Numbers – Int, Float | Type Conversion

There are three types of numeric data in python.

  1. int
  2. float
  3. complex

For example:

x = 1
y = 2.5
z = 3j

print(type(x))
print(type(y))
print(type(z))

Output:

<class 'int'>
<class 'float'>
<class 'complex'>

Integer

Let’s start off with integer, an integer is well a whole number like 2, 3 or 4, positive or negative without decimal.

You can use integers to do mathematical operations for example 2 + 4.

x = 5
y = -2

z = x + y

print(z)

Output:

3

Float

Floating point number or float is a decimal number which is positive or negative.

x = 5.5
y = 7

z = x + y

print(z)
print(type(z))

Output:

12.5
<class 'float'>

We also an ‘e’ in float number which indicates the power of 10.

x = 12E4
y = 12.e4

print(type(x))
print(type(y))

Output:

<class 'float'>
<class 'float'>

Complex

Complex number is an imaginary number which is written with a ‘j’.

x = 4j
y = 5j

z = x + y

print(z)
print(type(z))

Output:

9j
<class 'complex'

Number Type Conversion

Convert a data type to another type by using int(), float(), complex(), and str() method to satisfy the requirements of an operator or function parameter.

x = 10    # int
y = 3.5  # float

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))

Output:

10.0
3
(10+0j)
<class 'float'>
<class 'int'>
<class 'complex'>

Some Mathematical Functions

Python has the following mathematical functions to performs calculations:

FunctionsCodeOutput
abs(x): The absolute value of x (positive value).print(“abs(-10): “, abs(-10))abs(-10): 10
ceil(x): The ceiling of x, the smallest integer not less than ximport math
print (“math.ceil(12.2): “, math.ceil(12.2))
math.ceil(12.2): 13
cmp(x, y): returns -1 if x < y, returns 0 if x == y and 1 if x > yprint(cmp(80, 100))-1
pow(x, y): The value of x**y.import math
print (math.pow(10, 2))
100
max(x, y, ….): returns maximum number from the argumentprint (max(2, 4, 6))6
min(x, y, ….): returns minimum number from the argumentprint (min(2, 4, 6))2
More Build-in function>>
Scroll to Top