Working with Numbers in Python

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.

Basic Arithmetic Operations

Addition

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

Subtraction is the process of finding the difference between two numbers.

				
					# Example of subtraction
result = 10 - 3
print(result)  # Output: 7

				
			

Multiplication

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

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

Floor division returns the quotient of the division, discarding any remainder.

				
					# Example of floor division
result = 10 // 3
print(result)  # Output: 3

				
			

Modulo

Modulo returns the remainder of the division operation.

				
					# Example of modulo
result = 10 % 3
print(result)  # Output: 1
				
			

Advanced Mathematical Functions

Exponentiation

Exponentiation raises a number to the power of another number.

				
					# Example of exponentiation
result = 2 ** 3
print(result)  # Output: 8
				
			

Square Root

Square root returns the square root of a number.

				
					# Example of square root
import math

result = math.sqrt(16)
print(result)  # Output: 4.0
				
			

Trigonometric Functions

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!❤️

Table of Contents