Numbers in Python: Integer, Float & Complex Data Types

Numbers in Python: Integer, Float & Complex Data Types

Numbers in Python

Numbers are essential in Python for performing calculations, and they are classified into three basic data types:
  1. Integers
  2. Floating-point numbers
  3. Complex numbers
This guide will take you through each of these data types in detail. Let’s dive in!

1. Integers

Definition: Integers are whole numbers without decimal values. They can be positive, negative, or zero. In Python, integers have unlimited size, constrained only by the system’s memory.Special Number Systems: Python supports binary (base 2), octal (base 8), and hexadecimal (base 16) representations for integers. Prefixes for these systems are:
  • Binary: 0b or 0B
  • Octal: 0o or 0O
  • Hexadecimal: 0x or 0X
Examples:
x = 1
y = 882399773218279
z = -125634
a = 0b1100

print(type(x))  # <class 'int'>
print(type(y))  # <class 'int'>
print(type(z))  # <class 'int'>
print(a)        # 12
print(type(a))  # <class 'int'>
Output:
<class 'int'>
<class 'int'>
<class 'int'>
12
<class 'int'>
Suggested Visuals:
  • A diagram showing different number systems with examples.
  • Code snippets visually formatted for clarity.

2. Floating-Point Numbers

Definition: Floating-point numbers are real numbers containing a decimal point, separating the integer and fractional parts. They are accurate up to 15 decimal places.Scientific Notation: Python supports scientific notation using E or e.
  • Example: 23e2 is equivalent to 23 × 10^2.
Examples:
x = 12.3
y = 12.9829379485794548679
z = -18.96

print(type(x))  # <class 'float'>
print(type(y))  # <class 'float'>
print(type(z))  # <class 'float'>
print(x)        # 12.3
print(y)        # 12.982937948579455
print(z)        # -18.96
Output:
<class 'float'>
<class 'float'>
<class 'float'>
12.3
12.982937948579455
-18.96
Suggested Visuals:
  • A table illustrating float values and their scientific notations.
  • Charts showing precision limits of floating-point numbers.

3. Complex Numbers

Definition: Complex numbers are written in the form a + bj, where a is the real part and b is the imaginary part. Note that Python uses j for the imaginary unit instead of i.Examples:
x = -5j
y = 2 + 4j
z = 22j

print(type(x))  # <class 'complex'>
print(type(y))  # <class 'complex'>
print(type(z))  # <class 'complex'>
Output:
<class 'complex'>
<class 'complex'>
<class 'complex'>
Suggested Visuals:
  • A diagram of the complex plane showing real and imaginary parts.
  • Examples of complex numbers in real-world applications.

Type Conversion of Numbers in Python

Python provides inbuilt functions to convert between number data types:
  1. int() – Converts any data type to an integer.
  2. float() – Converts any data type to a float.
  3. complex(real, imaginary) – Converts to a complex number.
Examples:
b = 13.16
c = 2 + 17j

a = 5

# Converting float to int
print(int(b))

# Converting int to float
print(float(a))

# Converting int to complex
print(complex(a))

# Converting float to complex
print(complex(b))

# Converting to complex with real and imaginary parts
print(complex(a, b))
Output:
13
5.0
(5+0j)
(13.16+0j)
(5+13.16j)
Suggested Visuals:
  • Flowchart of type conversion in Python.

Inbuilt Functions Related to Numbers

Python provides many inbuilt functions for mathematical calculations. Some popular functions include:
  • abs() – Returns the absolute value.
  • pow() – Computes power.
  • round() – Rounds off a number to a given precision.
  • math.sqrt() – Computes square root (requires importing math).
Usage Example:
import math

x = 25

# Absolute value
print(abs(-10))

# Power
print(pow(2, 3))

# Rounding
print(round(12.3456, 2))

# Square root
print(math.sqrt(x))
Output:
10
8
12.35
5.0
Suggested Visuals:
  • A table listing common math functions with examples.
  • Flowchart for how to use these functions.

Conclusion

Numbers in Python are versatile and offer a wide range of functionalities. From basic arithmetic to advanced type conversion and mathematical functions, Python’s number system is designed to handle diverse computational needs efficiently.Use this guide as a quick reference to master numbers in Python, and enhance your programs with precision and creativity!Click here to know more our program!
c