Python Numbers, Type Conversion, and Mathematics
Python is a versatile and powerful programming language that simplifies various tasks in computing, including mathematical operations and data type management. In this blog, we will explore Python numbers, type conversion, and mathematical functions, along with practical examples to help you understand how these concepts work.
In Python, numbers are used to represent numerical values. Python supports various types of numbers, including integers, floating-point numbers, and complex numbers.
An integer is a whole number, either positive or negative, without any decimal points.
Example:
x = 5
y = -12
A float is a number that has a decimal point or is expressed in exponential form.
Example:
x = 5.75
y = -3.14
A complex number is made up of a real and imaginary part. The real and imaginary parts are separated by a plus or minus sign, with the imaginary part followed by the letter "j".
Example:
x = 3 + 4j
y = 1 - 2j
Python provides several ways to convert data from one type to another. This process is known as type conversion or type casting.
Implicit conversion, also known as coercion, is automatically performed by Python when you mix different numeric types, and Python automatically converts one type to another to avoid errors.
Example:
x = 5 # Integer
y = 2.5 # Float
result = x + y # The integer is implicitly converted to a float
print(result) # Output: 7.5
Explicit conversion, also known as type casting, allows you to manually convert one data type to another using functions like int()
, float()
, str()
, and complex()
.
Example:
x = 5.75 # Float
y = int(x) # Explicit conversion to integer
print(y) # Output: 5
z = int("10") # Converting string to integer
print(z) # Output: 10
Python has a rich set of built-in mathematical functions and modules that allow you to perform advanced mathematical operations.
Python supports the basic arithmetic operators: addition, subtraction, multiplication, division, modulus, and exponentiation.
a = 10
b = 3
# Addition
print(a + b) # Output: 13
# Subtraction
print(a - b) # Output: 7
# Multiplication
print(a * b) # Output: 30
# Division
print(a / b) # Output: 3.3333...
# Modulus (Remainder)
print(a % b) # Output: 1
# Exponentiation
print(a ** b) # Output: 1000
math
ModulePython's math
module provides additional mathematical functions, such as square root, factorial, trigonometric functions, and constants like Pi.
import math
# Square root
print(math.sqrt(16)) # Output: 4.0
# Factorial
print(math.factorial(5)) # Output: 120
# Pi constant
print(math.pi) # Output: 3.141592653589793
# Trigonometric functions
print(math.sin(math.pi / 2)) # Output: 1.0
Python provides several methods for rounding numbers to a desired number of decimal places.
x = 5.6789
# Round to 2 decimal places
print(round(x, 2)) # Output: 5.68
# Floor and Ceil
print(math.floor(x)) # Output: 5 (Round down)
print(math.ceil(x)) # Output: 6 (Round up)
The abs()
function returns the absolute value of a number (i.e., its distance from zero).
x = -15
print(abs(x)) # Output: 15
Python provides support for complex numbers, which can be manipulated using various mathematical functions. Complex numbers are represented as a + bj
where a
is the real part and b
is the imaginary part.
You can perform basic arithmetic operations with complex numbers in Python, just like with integers or floats.
Example:
z1 = 3 + 4j
z2 = 1 - 2j
# Addition
z3 = z1 + z2
print(z3) # Output: (4+2j)
# Multiplication
z4 = z1 * z2
print(z4) # Output: (11 - 10j)
abs()
Function on Complex NumbersYou can use the abs()
function to calculate the magnitude (or absolute value) of a complex number.
Example:
z = 3 + 4j
print(abs(z)) # Output: 5.0 (Magnitude of the complex number)