Here’s a detailed explanation of Python Numbers for Python programming:
Python Numbers
What are Numbers in Python?
Numbers in Python are used to store numeric values. Python supports several types of numbers, including integers, floating-point numbers, and complex numbers. Each type serves a specific purpose depending on the kind of data being handled.
Types of Numbers in Python
- int (Integer)
- Stores whole numbers (positive, negative, or zero) without any decimal point.
- Example:
x = 10 y = -5 z = 0 print(type(x)) # Output: <class 'int'>
- float (Floating-point)
- Stores real numbers with a decimal point or in exponential (scientific) notation.
- Example:
x = 3.14 y = -0.99 z = 1.5e3 # Scientific notation (1.5 * 10^3 = 1500) print(type(x)) # Output: <class 'float'>
- complex (Complex Numbers)
- Represents numbers in the form
a + bj
, wherea
is the real part andb
is the imaginary part. - Example:
num = 3 + 4j print(num.real) # Output: 3.0 print(num.imag) # Output: 4.0 print(type(num)) # Output: <class 'complex'>
- Represents numbers in the form
Numeric Operations
Python supports a variety of arithmetic operations with numbers:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus (Remainder) | 5 % 2 | 1 |
** | Exponentiation | 5 ** 3 | 125 |
Examples:
# Basic Arithmetic
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333...
print(a // b) # Output: 3 (Floor Division)
print(a % b) # Output: 1 (Remainder)
print(a ** b) # Output: 1000 (10 raised to the power of 3)
Type Conversion
Python provides several built-in functions to convert between numeric types:
Function | Description | Example |
---|---|---|
int(x) | Converts x to an integer | int(3.5) → 3 |
float(x) | Converts x to a floating point | float(5) → 5.0 |
complex(x) | Converts x to a complex number | complex(3) → 3+0j |
Examples:
x = 5.7
y = int(x) # Converts float to int
print(y) # Output: 5
z = float(y) # Converts int back to float
print(z) # Output: 5.0
c = complex(x) # Converts to complex number
print(c) # Output: (5.7+0j)
Mathematical Functions
Python includes a standard library called math
that provides many mathematical operations:
Examples:
import math
# Square root
print(math.sqrt(16)) # Output: 4.0
# Power
print(math.pow(2, 3)) # Output: 8.0
# Rounding
print(round(3.14159, 2)) # Output: 3.14
# Absolute value
print(abs(-7)) # Output: 7
# Trigonometric functions
print(math.sin(math.pi / 2)) # Output: 1.0
Special Numeric Values
Python also supports special numeric constants like infinity and NaN (Not a Number).
Examples:
# Infinity
pos_inf = float('inf')
neg_inf = float('-inf')
print(pos_inf, neg_inf) # Output: inf -inf
# NaN (Not a Number)
nan = float('nan')
print(nan) # Output: nan
Practical Example
Here’s an example that combines different numeric types and operations:
# Calculate the area of a circle
import math
radius = 5
area = math.pi * (radius ** 2)
print(f"Area of the circle: {area}") # Output: Area of the circle: 78.53981633974483
# Perform floor division and modulus
x = 15
y = 4
quotient = x // y
remainder = x % y
print(f"Quotient: {quotient}, Remainder: {remainder}")
Important Notes
- Dynamic Typing: You can reassign a variable to a different type:
x = 10 # x is an int x = 10.5 # x is now a float x = 3 + 4j # x is now a complex number
- Immutable Numbers: Numbers in Python are immutable, meaning their value cannot change after being created. When you perform an operation, a new object is created.
- Precision of Floating-Point Numbers: Floats may not always provide exact results due to how they’re stored in memory:
print(0.1 + 0.2) # Output: 0.30000000000000004
Use thedecimal
module for higher precision if needed:from decimal import Decimal print(Decimal('0.1') + Decimal('0.2')) # Output: 0.3
This explanation provides a clear overview of Python Numbers.