Login Register

What Are Numbers In Python?

What Are Numbers In Python?

Numbers refer to values in Python programming that we can use for calculations, such as adding, subtracting, measuring size, price, quantity, etc.

Whenever we write a value in programs like 10, 3.16, or 8+2j, Python stores it as a number in memory.

Types of Numbers in Python

Python divides numbers into three main type, such as:

  1. Integer (int)
  2. Floating-Point (float)
  3. Complex Numbers (complex)

We can use each type for different purposes for our real-world projects. Let’s understand in depth:

Python numeric types

1. Integer (int)

An integer refers to a whole number that we have used in our program, and it can be positive or negative without a decimal point.

Integers don’t have any fixed size limit in Python programming, and they can be used for many different operations in Python programs. Integer also supports binary, octal and hexadecimal formats.

These types of integer numbers can contain:

-10, -1, 0, 5, 100, 99999
  • You can see that Python can store thousands of digits, depending on your computer’s RAM.

Examples of Integer Type:

1) Normal Integer Assignment:

# Integer assignment
age = 25
year = -2024

# Binary representation
binary_num = 0b1010 # Binary for 10
print(binary_num) # Output: 10

# Hexadecimal representation
hex_num = 0x1A # Hexadecimal for 26
print(hex_num) # Output: 26

2) Binary Representation:

binary_num = 0b1010
print(binary_num) # Output: 10
  • Here, numbers starting with 0b are written in binary form.
  • 1010 is binary, but it is equal to 10 in decimal, so Python converts it into a normal integer when printing the output.

3) Hexadecimal Representation:

hex_num = 0x1A
print(hex_num) # Output: 26
  • In this code, the numbers starting with 0x and it’s written in hexadecimal form.
  • 1A in hexadecimal equals 26 in decimal.
  • Python automatically converts and displays the decimal value.

2. Floating-Point (float)

A floating-point number or float is a special number that refer to the decimal point. It is used to represent the different kind of numbers such as weight price, temperature, or distance.

Python follows the IEEE 754 format, so we can store both very small and very large decimal values accurately. Because floats are mostly useful for scientific and mathematical calculations.

What Is IEEE 754?

IEEE 754 is an international standard that defines how computers should store and work with decimal numbers (floating-point).

This method is important, because floats have decimal values and computers work in binary (0 and 1), so it can face difficulty to exactly store them.

IEEE 754 gives a universal rule so every computer stores, calculates, and handles big and small decimal numbers in the same way.

What Is Exponential Notation?

Exponential notation is a short form of writing very big or very small decimal numbers. It uses e to means “x 10 to the power”

For example:

  • 1.5e3 means → 1.5 × 10³ = 1500
  • 2e-4 means → 2 × 10⁻⁴ = 0.0002

Python uses this format to easily handle scientific or tiny decimal numbers.

Examples of Float Numbers:

# Float assignment
price = 99.99
pi = 3.14159

# Exponential representation
large_num = 1.2e3 # 1.2 x 10^3
print(large_num)

Output:

3. Complex Numbers (complex)

A complex number is a special type of number that contains two parts:

  • A real part (normal number)
  • an imaginary part that written with the latter j

Form of complex number:

a + bj

#a = real part
#b = imaginary part
#j = imaginary unit used in mathematics and engineering

Complex numbers are especially useful in multiple fields, such as physics, electrical engineering, signal processing, and scientific simulations where calculations involve waves, currents, or advanced mathematical models.

In simple words, a complex number combines a real value and an imaginary value in one single number so Python can perform advanced mathematical operations.

let’s understand the simple examples of complex numbers:

# Complex number assignment
z = 3 + 4j

# Access real and imaginary parts
print(z.real) # Output: 3.0
print(z.imag) # Output: 4.0

# Complex arithmetic
z1 = 1 + 2j
z2 = 2 + 3j
result = z1 + z2
print(result) # Output: (3+5j)

Number Operations in Python

Python provides a variety of operators for performing calculations on numbers:

Arithmetic Operators

OperatorDescriptionExampleResult
+Addition10 + 515
Subtraction10 – 55
*Multiplication10 * 550
/Division10 / 25.0
//Floor Division10 // 33
%Modulus (remainder)10 % 31
**Exponentiation2 ** 38

Type Conversion in Numbers

Python allows you to convert between number types using built-in functions:

  1. int(): Converts a float or string to an integer.
  2. float(): Converts an integer or string to a float.
  3. complex(): Converts a number or string to a complex number.

Examples of Type Conversion:

# Float to int
num = 5.67
int_num = int(num)
print(int_num) # Output: 5

# Int to float
age = 30
float_age = float(age)
print(float_age) # Output: 30.0

# String to complex
complex_num = complex("3+4j")
print(complex_num) # Output: (3+4j)

Checking the Type of a Number

To check the type of a number, Python provides the type() function.

Example:

x = 10
print(type(x)) # Output: <class 'int'>

y = 3.14
print(type(y)) # Output: <class 'float'>

z = 1 + 2j
print(type(z)) # Output: <class 'complex'>

Advantages of Python Numbers

  1. Dynamically Typed: No need to declare the type explicitly.
  2. Memory Efficient: Python manages memory allocation efficiently for large numbers.
  3. Rich Library Support: Libraries like NumPy and SciPy extend Python’s number capabilities.

Learn Advanced Topics About Python