Python Numbers

Python supports integers, floating-point  and complex numbers. They are defined as int , float , and complex in Python. Integers and floating points are separated by the presence or absence of a decimal point.

int (plain integers): This one is pretty standard — plain integers are just positive or negative whole numbers.

a = 20
print(type(a)) 
<class 'int'>

More Examples:

a = 2
b = 3

# Addition
c = a + b
print(c)

m = 2
n = 3

# Subtraction
d = m - n
print(d)


x = 2
y = 3

# Multiplication
z = x * y
print(z)
5
-1
6

float (floating point real values): Floats represent real numbers, but are written with decimal points (or scientific notation) to divide the whole number into fractional parts.

a = 10.5  
print(type(a) 
<class 'float'>

More Examples:

a = 2.5
b = 3.5

# Addition
c = a + b
print(c)

m = 2.5
n = 3.5

# Subtraction
d = m - n
print(d)


x = 2.5
y = 3.5

# Multiplication
z = x * y
print(z)
6.0
-1.0
8.75

complex (complex numbers): Represented by the formula a + bJ, where a and b are floats, and J is the square root of -1 (the result of which is an imaginary number). Complex numbers are used sparingly in Python.

a = 2+5j  
print(type(a)  
<class 'complex'>

More Examples:

a = 2 + 1j
b = 3 + 2j

# Addition
c = a + b
print(c)

m = 2 + 1j
n = 3 - 2j

# Subtraction
d = m - n
print(d)


x = 2 + 1j
y = 3 + 2j

# Multiplication
z = x * y
print(z)
(5+3j)
(-1+3j)
(4+7j)

Type Conversion ?

Type conversion means convert one data type to another data type.

Example:

x = 10    # int
y = 5.5  # float
z = 3j   # complex

#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))
10.0
5
(10+0j)
<class 'float'>
<class 'int'>
<class 'complex'>

Python Numbers
  1. Python supports integers, floating-point  and complex numbers.
  2. Integer represent the plain integers are just positive or negative whole numbers.
  3. Floats represent real numbers, but are written with decimal points.
  4. Represented by the formula a + bJ, where a and b are floats, and J is the square root of -1 (the result of which is an imaginary number).