Python Numbers
In Python, numbers are used to store numeric values. Python supports three different types of numbers:
- Integers (
int) - Floating-point numbers (
float) - Complex numbers (
complex)
Each of these types has its own unique properties and use cases. Let’s take a closer look at each of them.
1. Integers (int)
Integers are whole numbers, positive or negative, without decimals. Python automatically detects an integer type when you assign a whole number to a variable.
Example:
a = 10 # Positive integer
b = -5 # Negative integer
c = 0 # Zero (integer)
Python integers can be of arbitrary length, meaning you can work with very large numbers without worrying about overflow.
Example of large integers:
large_number = 123456789012345678901234567890
print(large_number)
2. Floating-Point Numbers (float)
Floating-point numbers, or simply floats, represent real numbers and are used for decimal values. They contain a decimal point or an exponential (e) to represent very large or very small numbers.
Example:
x = 3.14 # Positive float
y = -0.001 # Negative float
z = 1.0 # Float with a decimal point
Floats can also be written in scientific notation by using an e to indicate powers of 10.
Example of scientific notation:
a = 1.5e2 # 1.5 * 10^2 = 150.0
b = 2.5e-3 # 2.5 * 10^-3 = 0.0025
3. Complex Numbers (complex)
Complex numbers are used to represent numbers with a real and an imaginary part. In Python, the imaginary part is written with a j (e.g., 1 + 2j). Complex numbers consist of two parts:
- The real part is a floating-point number.
- The imaginary part is a floating-point number followed by
j.
Example:
c = 1 + 2j # Complex number with real part 1 and imaginary part 2
d = -3j # Complex number with real part 0 and imaginary part -3
You can access the real and imaginary parts of a complex number using the .real and .imag attributes.
Example:
c = 3 + 4j
print(c.real) # Output: 3.0
print(c.imag) # Output: 4.0
Arithmetic Operations on Numbers
Python supports standard arithmetic operations like addition, subtraction, multiplication, division, and more. Here’s a summary of these operations:
Basic Operations:
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division (float) | 5 / 2 = 2.5 |
// | Floor Division | 5 // 2 = 2 |
% | Modulus (remainder) | 5 % 2 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
Example:
a = 10
b = 3
print(a + b) # Addition: 13
print(a - b) # Subtraction: 7
print(a * b) # Multiplication: 30
print(a / b) # Division (float): 3.333...
print(a // b) # Floor division: 3
print(a % b) # Modulus (remainder): 1
print(a ** b) # Exponentiation: 1000
Built-in Functions for Numbers
Python provides several built-in functions for working with numbers.
abs(): Returns the absolute value of a number.print(abs(-10)) # Output: 10pow(x, y): Returnsxraised to the powery(same asx ** y).print(pow(2, 3)) # Output: 8round(x, n): Roundsxtondecimal places.print(round(3.14159, 2)) # Output: 3.14max(): Returns the largest value among the arguments.print(max(1, 5, 10, -3)) # Output: 10min(): Returns the smallest value among the arguments.print(min(1, 5, 10, -3)) # Output: -3sum(): Returns the sum of all items in an iterable.print(sum([1, 2, 3, 4])) # Output: 10
Type Conversion
You can convert between different number types using type casting functions:
int(): Converts a number to an integer.float(): Converts a number to a float.complex(): Converts numbers to complex numbers.
Example:
x = 3.14
y = int(x) # Converts float to integer: 3
z = "10"
w = float(z) # Converts string to float: 10.0
Example: Number Operations
# Integer arithmetic
a = 10
b = 3
print(a + b) # Output: 13
print(a ** b) # Output: 1000
# Floating-point arithmetic
x = 3.14
y = 1.5
print(x * y) # Output: 4.71
# Complex number operations
z = 1 + 2j
w = 2 + 1j
print(z + w) # Output: (3+3j)
Summary of Number Types:
| Type | Description | Example |
|---|---|---|
int | Integer (whole number, no decimals) | 42, -7 |
float | Floating-point number (decimal) | 3.14, -0.5 |
complex | Complex number (real + imaginary parts) | 1 + 2j |