Types of Numbers in Python
Python categorizes numbers into three types:
- Integer (int)
- Floating-Point (float)
- 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
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
– | Subtraction | 10 – 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 2 | 5.0 |
// | Floor Division | 10 // 3 | 3 |
% | Modulus (remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
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:
- int(): Converts a float or string to an integer.
- float(): Converts an integer or string to a float.
- 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
- Dynamically Typed: No need to declare the type explicitly.
- Memory Efficient: Python manages memory allocation efficiently for large numbers.
- Rich Library Support: Libraries like NumPy and SciPy extend Python’s number capabilities.