Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1 Programming Quiz

A 10 of the mathematical functions available in Python and how can I use them in my program?

1. Math.pi
The number 𝜋 is a mathematical constant, approximately equal to 3.14159. After
importing the math module, you just need to write math.pi to access the 𝜋 number:

import math
print(math.pi)

2. Math.tau

The τ constant returns an almost precise value of 2�. Let's print its
value:

print(math.tau)

6.283185307179586

3. Math.e
We can access the number e (or the Euler’s number) simply by using
the math.e constant:

print(math.e)

2.718281828459045

4. math.nan

The math.nan constant stands for Not a Number, and it can initialize those


variables that aren't numbers. Technically, the data type of the math.nan constant
is float; however, it’s not considered a valid number.

print(math.nan)
print(type(math.nan))

nan
<class 'float'>

5. math.inf
The math.inf constant represents a floating-point positive infinity. It can represent
both positive infinity and negative infinity constants, as follows:

[Type text] Page 1


2 Programming Quiz

print(math.inf)
print(-math.inf)
inf
-inf

The math Module Functions
The math module provides a wide range of mathematical functions for many different
scientific and engineering applications, including the following:

 Number functions
 Power and logarithmic functions
 Trigonometric functions
 Angular conversion functions
 Hyperbolic functions
 and some special functions

However, we will only discuss the most important ones in this section. Let's explore
them.

Number Functions
math.ceil()
The math.ceil() method maps a floating-point number to the smallest succeeding
integer:

p = 10.1
print(math.ceil(p))
11
math.floor()
The math.floor() method maps a floating-point number to the greatest preceeding
integer:

q = 9.99
print(math.floor(q))
9
math.factorial()
The math.factorial(n) method returns the product of all natural numbers less
than or equal to n, if n a positive integer. However, if n = 0, it returns 1. The code
below uses the math.factorial() to calculate 5!:
n = 5
print("{}! = {}".format(n, math.factorial(n)))
5! = 120

[Type text] Page 2


3 Programming Quiz

math.gcd()
The math.gcd() method returns the greatest common denominator for two numbers;
we can use it to reduce fractions.

For example, the GCD for 25 and 120 is 5, so we can divide the numerator and
denominator by 5 to get a reduced fraction (e.g., 25120=524). Let's see how it
works:
gcd = math.gcd(25, 120)
print(gcd)
5
math.fabs()
The math.fabs() method removes the negative sign of a given number, if any, and
returns its absolute value as a float:

print(math.fabs(-25))
25.0
Power and Logarithmic Functions
math.pow()
The math.pow() method returns a floating-point value representing the value of x to the
power of y . Let's try the math.pow() method by forecasting an investment. To do that, we
need to know the initial deposit, the annual interest rate, and the number of years that you
invest your money in an investment account. Finally, by using the following formula, we can
calculate the final amount of the investment:
For example, consider the following values:

 Initial deposit: $10,000


 Annual interest rate: 4%
 Number of years: 5

The code calculates the final amount deposited in the investment account after five
years:

deposit = 10000
interest_rate = 0.04
number_years = 5
final_amount = deposit * math.pow(1 + interest_rate, number_years)
print("The final amount after five years is", final_amount)
The final amount after five years is 12166.529024000001

[Type text] Page 3


4 Programming Quiz

A 10 of the mathematical functions available in Python and how we can use them in my program?

[Type text] Page 4

You might also like