In Python, numbers are a type of data that represent numerical values. There are three main types of numbers in Python: integers, floating-point numbers, and complex numbers. In this blog, we'll cover each type of number and provide examples to help you understand how they work in Python.

Integers

        Integers are whole numbers, meaning they have no decimal point. In Python, integers are represented using the int data type. Here are some examples of integers in Python:
x = 42
y = -13
z = 0

        In the example above, x is an integer with the value of 42, y is an integer with the value of -13, and z is an integer with the value of 0.

        You can perform arithmetic operations on integers in Python using the standard mathematical operators, such as +, -, *, /, and % (which returns the remainder of a division operation). Here's an example:
a = 10
b = 3
c = a + b   # c is now 13
d = a - b   # d is now 7
e = a * b   # e is now 30
f = a / b   # f is now 3.33333...
g = a % b   # g is now 1


Floating-Point Numbers

        Floating-point numbers, also known as floats, are numbers that have a decimal point. In Python, floats are represented using the float data type. Here are some examples of floats in Python:
x = 3.14159
y = -2.5
z = 0.0

        In the example above, x is a float with the value of 3.14159, y is a float with the value of -2.5, and z is a float with the value of 0.0.

    You can perform arithmetic operations on floats in Python using the same operators as for integers. Here's an example:
a = 3.0
b = 1.5
c = a + b   # c is now 4.5
d = a - b   # d is now 1.5
e = a * b   # e is now 4.5
f = a / b   # f is now 2.0

Complex Numbers

    Complex numbers are numbers that have both a real and imaginary part. In Python, complex numbers are represented using the complex data type. Here are some examples of complex numbers in Python:
x = 2 + 3j
y = -4j
z = 1.5 - 2j

    In the example above, x is a complex number with a real part of 2 and an imaginary part of 3, y is a complex number with a real part of 0 and an imaginary part of -4, and z is a complex number with a real part of 1.5 and an imaginary part of -2.

    You can perform arithmetic operations on complex numbers in Python using the same operators as for integers and floats. Here's an example:
a = 2 + 3j
b = 1 + 2j
c = a + b   # c is now 3 + 5j
d = a - b   # d is now 1 + 1j
e = a * b   # e is now -4 + 7j
f = a / b

Python supports four different numerical data types:

  1. Integer (int): Integer is a whole number, positive or negative, without decimals, of unlimited length.
  2. Float: Float is a decimal number, with a floating point to represent fractional values. It can be used to represent real numbers and is written with a decimal point dividing the integer and fractional parts.
  3. Complex: Complex numbers are written with a "j" as the imaginary part.
  4. Boolean: A Boolean data type can have one of two values, either True or False.

        Here are some more examples of how to use these data types in Python:
# Integer
x = 10
y = -5
z = 0

# Float
a = 3.14
b = -7.5
c = 0.0

# Complex
d = 2 + 3j
e = -4j

# Boolean
f = True
g = False

        In Python, you can perform various arithmetic operations on numbers, including addition, subtraction, multiplication, division, and modulo. Here are some examples:
# Addition
a = 10
b = 5
c = a + b # Output: 15

# Subtraction
d = 7
e = 3
f = d - e # Output: 4

# Multiplication
g = 2
h = 6
i = g * h # Output: 12

# Division
j = 15
k = 3
l = j / k # Output: 5.0

# Modulo
m = 15
n = 4
o = m % n # Output: 3

        Python also provides some built-in functions for working with numbers, including abs() for returning the absolute value of a number, pow() for calculating the power of a number, round() for rounding a number to a specified number of decimal places, and int() and float() for converting between data types.
# abs()
p = -5
q = abs(p) # Output: 5

# pow()
r = 2
s = pow(r, 3) # Output: 8

# round()
t = 3.14159
u = round(t, 2) # Output: 3.14

# int()
v = 10.5
w = int(v) # Output: 10

# float()
x = 5
y = float(x) # Output: 5.0

These are just some of the basic operations and functions that can be performed on numbers in Python.

Read Next Chapter Click Here -> Lists