Python Numbers

Types of Numbers in Python

Python categorizes numbers into three types:

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

Let’s explore each type in detail.

1. Integer (int)

An integer is a whole number, either positive or negative, without a decimal point. In Python, integers can be as large as your system’s memory allows, making them suitable for a wide range of applications.

Key Features of Integers:

  • No decimal or fractional part.
  • Can include negative values.
  • Supports binary, octal and hexadecimal formats.

Examples of Integer Usage:

# 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. Floating-Point (float)

A floating-point number (or float) represents real numbers with a decimal point. Python floats are based on the IEEE 754 standard, allowing them to handle scientific calculations effectively.

Key Features of Floats:

  • Includes a decimal point.
  • Can use exponential notation.
  • Useful for precise calculations like measuring distances or temperatures.

Examples of Float Usage:

# Float assignment
price = 99.99
pi = 3.14159

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

3. Complex Numbers (complex)

A complex number consists of a real and an imaginary part, represented as a + bj. Python uses the letter j to denote the imaginary unit.

Key Features of Complex Numbers:

  • The real part is a float.
  • The imaginary part is a float multiplied by j.
  • Commonly used in scientific computations.

Examples of Complex Number Usage:

# 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

Example of Arithmetic Operations:

x = 10
y = 3

print(x + y) # Output: 13
print(x / y) # Output: 3.3333333333333335
print(x ** y) # Output: 1000

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.

Leave a Comment