Advanced
Mathematical Operations
Leverage Python’s built-in math and random modules to perform common mathematical operations and generate random values.
Built-in Math Functions
Python provides many useful math functions in the math
module:
pythonCopyEditimport math print(math.sqrt(16)) # Square root: 4.0 print(math.factorial(5)) # Factorial: 120 print(math.pow(2, 3)) # Power: 8.0 print(math.ceil(4.2)) # Ceiling: 5 print(math.floor(4.8)) # Floor: 4 print(math.pi) # Pi constant: 3.141592653589793 print(math.e) # Euler's number: 2.718281828459045
Random Numbers
The random
module is useful for generating random numbers:
pythonCopyEditimport random print(random.randint(1, 10)) # Random integer between 1 and 10 print(random.random()) # Random float between 0.0 and 1.0 print(random.choice(["red", "blue", "green"])) # Random choice from a list
Using decimal
for Precise Arithmetic
For high-precision decimal arithmetic:
pythonCopyEditfrom decimal import Decimal a = Decimal('0.1') b = Decimal('0.2') print(a + b) # 0.3 (precise)
Math Constants and Functions Summary
Function/ConstantDescriptionmath.sqrt(x)
Square rootmath.factorial(n)
Factorial of nmath.pow(x, y)
Power (x to the power y)math.ceil(x)
Ceiling of xmath.floor(x)
Floor of xmath.pi
Pi constantmath.e
Euler's number
Built-in Math Functions
Python provides many useful math functions in the math
module:
pythonCopyEditimport math print(math.sqrt(16)) # Square root: 4.0 print(math.factorial(5)) # Factorial: 120 print(math.pow(2, 3)) # Power: 8.0 print(math.ceil(4.2)) # Ceiling: 5 print(math.floor(4.8)) # Floor: 4 print(math.pi) # Pi constant: 3.141592653589793 print(math.e) # Euler's number: 2.718281828459045
Random Numbers
The random
module is useful for generating random numbers:
pythonCopyEditimport random print(random.randint(1, 10)) # Random integer between 1 and 10 print(random.random()) # Random float between 0.0 and 1.0 print(random.choice(["red", "blue", "green"])) # Random choice from a list
Using decimal
for Precise Arithmetic
For high-precision decimal arithmetic:
pythonCopyEditfrom decimal import Decimal a = Decimal('0.1') b = Decimal('0.2') print(a + b) # 0.3 (precise)
Math Constants and Functions Summary
Function/ConstantDescriptionmath.sqrt(x)
Square rootmath.factorial(n)
Factorial of nmath.pow(x, y)
Power (x to the power y)math.ceil(x)
Ceiling of xmath.floor(x)
Floor of xmath.pi
Pi constantmath.e
Euler's number
Built-in Math Functions
Python provides many useful math functions in the math
module:
pythonCopyEditimport math print(math.sqrt(16)) # Square root: 4.0 print(math.factorial(5)) # Factorial: 120 print(math.pow(2, 3)) # Power: 8.0 print(math.ceil(4.2)) # Ceiling: 5 print(math.floor(4.8)) # Floor: 4 print(math.pi) # Pi constant: 3.141592653589793 print(math.e) # Euler's number: 2.718281828459045
Random Numbers
The random
module is useful for generating random numbers:
pythonCopyEditimport random print(random.randint(1, 10)) # Random integer between 1 and 10 print(random.random()) # Random float between 0.0 and 1.0 print(random.choice(["red", "blue", "green"])) # Random choice from a list
Using decimal
for Precise Arithmetic
For high-precision decimal arithmetic:
pythonCopyEditfrom decimal import Decimal a = Decimal('0.1') b = Decimal('0.2') print(a + b) # 0.3 (precise)
Math Constants and Functions Summary
Function/ConstantDescriptionmath.sqrt(x)
Square rootmath.factorial(n)
Factorial of nmath.pow(x, y)
Power (x to the power y)math.ceil(x)
Ceiling of xmath.floor(x)
Floor of xmath.pi
Pi constantmath.e
Euler's number