PYTHON MATH FUNCTIONS
• INTRODUCTION TO MATH FUNCTIONS:
o Basic Math Functions.
o Power and Root Functions.
o Logarithmic and Exponential Functions.
o Trigonometric Functions.
o Constants.
o Factorial and Combinations.
INTRODUCTION TO EXCEPTION HANDLING.
Basic Math Functions in Python:
abs()
Meaning: Returns the absolute value of a number (distance from zero).
Syntax: abs(x)
Parameters: x - an integer or float
round()
Meaning: Rounds a number to the nearest integer or to specified decimal places.
Syntax: round(x, n)
Parameters:
x: the number to round
n: number of decimal places (optional)
Subject: Python Programming [Math Functions] Page 1 of 6
[Link]()
Meaning: Returns the smallest integer greater than or equal to x.
Syntax: [Link](x)
Parameters: x - a float or integer
[Link]()
Meaning: Returns the largest integer less than or equal to x.
Syntax: [Link](x)
Parameters: x - a float or integer
[Link]()
Meaning: Truncates the decimal part and returns the integer part.
Syntax: [Link](x)
Parameters: x - a float
Subject: Python Programming [Math Functions] Page 2 of 6
Power and Root Functions
[Link]()
Meaning: Calculates x raised to the power y (x^y).
Syntax: [Link](x, y)
Parameters:
x: base number
y: exponent
pow()
Meaning: Same as [Link], but returns integer if possible.
Syntax: pow(x, y)
Parameters: same as above
[Link]()
Meaning: Returns the square root of a number.
Syntax: [Link](x)
Parameters: x - a non-negative number
Logarithmic and Exponential Functions
Subject: Python Programming [Math Functions] Page 3 of 6
[Link]()
Meaning: Returns e^x, where e ≈ 2.718
Syntax: [Link](x)
Parameters: x - a number
[Link]()
Meaning: Natural logarithm (base e) of x.
Syntax: [Link](x)
Parameters: x - positive number
math.log10()
Meaning: Logarithm base 10 of x.
Syntax: math.log10(x)
Parameters: x - positive number
math.log2()
Meaning: Logarithm base 2 of x.
Syntax: math.log2(x)
Parameters: x - positive number
Trigonometric Functions
[Link]()
Meaning: Sine of angle x (in radians)
Syntax: [Link](x)
Parameters: x - angle in radians
Subject: Python Programming [Math Functions] Page 4 of 6
[Link]()
Meaning: Cosine of angle x (in radians)
Syntax: [Link](x)
Parameters: x - angle in radians
[Link]()
Meaning: Tangent of angle x (in radians)
Syntax: [Link](x)
Parameters: x - angle in radians
[Link]()
Meaning: Converts angle from degrees to radians
Syntax: [Link](deg)
Parameters: deg - angle in degrees
[Link]()
Meaning: Converts angle from radians to degrees
Syntax: [Link](rad)
Parameters: rad - angle in radians
Constants
[Link]
Meaning: Mathematical Constant π
Value: 3.141592653589793
Subject: Python Programming [Math Functions] Page 5 of 6
math.e
Meaning: Euler’s number (base of natural log)
Value: 2.718281828459045
Factorial and Combinatorics
[Link]()
Meaning: Returns the factorial of a number (n!)
Syntax: [Link](n)
Parameters: n – non-negative integer.
[Link]()
Meaning: Number of combinations (nCr)
Syntax: [Link](n,r)
Parameters: n,r – non-negative integers.
[Link]()
Meaning: Number of permitutions (nPr)
Syntax: [Link](n,r)
Parameters: n,r – non-negative integers.
Example Of comb(5,2):
From items [A, B, C, D, E], how many pairs can we form?
Answer: AB, AC, AD, AE, BC, BD, BE, CD, CE, DE → 10 pairs
(Note: AB is same as BA, so order doesn’t count.)
Example of perm(5,2):
From [A, B, C, D, E], how many 2-letter codes can we make?
Answer: AB, AC, AD, AE, BA, BC, BD, BE, CA, CB, CD, CE, DA, DB, DC, DE, EA, EB, EC, ED →
20 codes
(Note: AB ≠ BA, so order counts.)
Subject: Python Programming [Math Functions] Page 6 of 6