Understanding Python Numbers
Python has three main numeric types: integers, floating-point numbers, and complex numbers. These types allow for a wide range of mathematical operations, from simple arithmetic to complex manipulations. Python's dynamic typing makes working with numbers easy, as the type is inferred based on the assigned value.
Types of Numbers in Python
Integers
Integers are whole numbers, both positive and negative, without a decimal point. Python can handle very large integers with ease due to its support for arbitrary-precision integers.
Basic Example of Integers
# Basic integer example
x = 10
negative_x = -20
big_num = 12345678901234567890
print(x)
print(negative_x)
print(big_num)
Output
10
-20
12345678901234567890
Explanation: Python handles integers, both positive and negative, and can work with very large numbers without losing precision. There's no need to worry about integer overflow in Python as it supports arbitrary-precision integers.
Floating-Point Numbers
Floating-point numbers, or floats, represent numbers with decimals. Python uses double precision (64-bit) for floats, meaning you can work with large and very precise floating-point numbers.
Basic Example of Floats
# Basic float example
pi = 3.14159
negative_float = -2.718
print(pi)
print(negative_float)
Output
3.14159
-2.718
Explanation: Floats are used for numbers that contain decimal points. Python automatically converts integers to floats when necessary and can handle both positive and negative floats.
Rounding Floats
# Rounding floats
rounded_pi = round(pi, 2)
print(rounded_pi)
Output
3.14
Explanation: The round()
function can be used to round floats to a specified number of decimal places. In this example, pi
is rounded to two decimal places, resulting in 3.14
.
Float Arithmetic
# Float arithmetic
x = 5.0
y = 2.0
addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
Output
Addition: 7.0
Subtraction: 3.0
Multiplication: 10.0
Division: 2.5
Explanation: Floats can be manipulated using arithmetic operations like addition, subtraction, multiplication, and division. Python automatically performs float division when dividing two float values.
Complex Numbers
Complex numbers in Python are written as a + bj
, where a
is the real part and b
is the imaginary part. Complex numbers are used in advanced mathematical calculations, such as in electrical engineering and physics.
Basic Example of Complex Numbers
# Complex number example
z = 2 + 3j
print("Real part:", z.real)
print("Imaginary part:", z.imag)
Output
Real part: 2.0
Imaginary part: 3.0
Explanation: Complex numbers have both a real and imaginary part. You can access these parts using the real
and imag
attributes.
Manipulating Complex Numbers
# Manipulating complex numbers
z1 = 1 + 2j
z2 = 3 + 4j
addition = z1 + z2
multiplication = z1 * z2
print("Addition:", addition)
print("Multiplication:", multiplication)
Output
Addition: (4+6j)
Multiplication: (-5+10j)
Explanation: Complex numbers can be added, subtracted, multiplied, and divided just like real numbers. In this example, we add and multiply two complex numbers, and the result is another complex number.
Python Number Methods
Python provides several built-in methods for working with numbers. These methods allow you to perform mathematical operations, type conversions, and more.
Absolute Value
# Absolute value of a number
x = -10
print(abs(x))
Output
10
Explanation: The abs()
function returns the absolute value of a number. In this case, the negative integer -10
is converted to its absolute value, 10
.
Power Function
# Power of a number
base = 2
exponent = 3
result = pow(base, exponent)
print(result)
Output
8
Explanation: The pow()
function raises the base
to the power of the exponent
. In this example, 2
raised to the power of 3
results in 8
.
Type Conversion
# Convert float to integer
x = 3.9
int_x = int(x)
print(int_x)
# Convert integer to float
y = 5
float_y = float(y)
print(float_y)
Output
3
5.0
Explanation: The int()
function converts a float to an integer, and the float()
function converts an integer to a float. Note that converting a float to an integer discards the decimal part.