Numbers play a crucial role in programming, and Python provides robust support for working with them. In this topic, we'll explore everything you need to know about working with numbers in Python, from basic arithmetic operations to more advanced mathematical functions and concepts.
Addition is the process of combining two or more numbers to find their sum.
# Example of addition
result = 5 + 3
print(result) # Output: 8
Subtraction is the process of finding the difference between two numbers.
# Example of subtraction
result = 10 - 3
print(result) # Output: 7
Multiplication is the process of repeated addition and finding the product of two numbers.
# Example of multiplication
result = 4 * 5
print(result) # Output: 20
Division is the process of splitting a number into equal parts or finding the quotient of two numbers.
# Example of division
result = 10 / 2
print(result) # Output: 5.0
Floor division returns the quotient of the division, discarding any remainder.
# Example of floor division
result = 10 // 3
print(result) # Output: 3
Modulo returns the remainder of the division operation.
# Example of modulo
result = 10 % 3
print(result) # Output: 1
Exponentiation raises a number to the power of another number.
# Example of exponentiation
result = 2 ** 3
print(result) # Output: 8
Square root returns the square root of a number.
# Example of square root
import math
result = math.sqrt(16)
print(result) # Output: 4.0
Python’s math module provides various trigonometric functions such as sine, cosine, and tangent.
# Example of trigonometric functions
import math
angle = math.radians(90) # Convert angle to radians
sin_result = math.sin(angle)
cos_result = math.cos(angle)
tan_result = math.tan(angle)
print(sin_result, cos_result, tan_result) # Output: 1.0 6.123233995736766e-17 1.633123935319537e+16
Understanding how to work with numbers is essential for any Python programmer. In this topic, we've covered basic arithmetic operations like addition, subtraction, multiplication, and division, as well as more advanced concepts such as exponentiation, square root, and trigonometric functions. By mastering these concepts, you'll be well-equipped to handle a wide range of numerical tasks in your Python programs. Keep practicing and experimenting with different numerical operations to solidify your understanding. Happy Coding!❤️