0% found this document useful (0 votes)
26 views

Chapter 6 Built-In Functions

The document discusses Python's built-in math functions and math module for performing mathematical tasks on numbers, including functions like min(), max(), abs(), pow(), and math module methods like sqrt(), trunc(), ceil(), floor(), as well as the math.pi constant. Examples are provided to demonstrate how to use these functions and methods by importing the math module and calling the functions/methods, passing numbers as arguments and printing the results. Readers are prompted to write out example outputs and try using the functions and methods themselves.

Uploaded by

Killua 0707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Chapter 6 Built-In Functions

The document discusses Python's built-in math functions and math module for performing mathematical tasks on numbers, including functions like min(), max(), abs(), pow(), and math module methods like sqrt(), trunc(), ceil(), floor(), as well as the math.pi constant. Examples are provided to demonstrate how to use these functions and methods by importing the math module and calling the functions/methods, passing numbers as arguments and printing the results. Readers are prompted to write out example outputs and try using the functions and methods themselves.

Uploaded by

Killua 0707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 6 Python Math Functions

❮ PreviousNext ❯
Python has a set of built-in math functions, including an extensive math
module, that allows you to perform mathematical tasks on numbers.

6.1 Built-in Math Functions


The min() and max() functions can be used to find the lowest or highest value in an
iterable:

Example
x = min(5, 10, 25)
y = max(5, 10, 25)

print(x)
print(y)
Write down the output:

Try it Yourself »

The abs() function returns the absolute (positive) value of the specified number:

Example
x = abs(-7.25)

print(x)
Write down the output:

Try it Yourself »

The pow(x, y) function returns the value of x to the power of y (xy).

Example
Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
x = pow(4, 3)

print(x)

Write down the output:

Try it Yourself »

6.2 The Math Module


Python has also a built-in module called math, which extends the list of
mathematical functions.

To use it, you must import the math module:


import math

When you have imported the math module, you can start using methods and
constants of the module.

The math.sqrt() method for example, returns the square root of a number:

Example
import math

x = math.sqrt(64)

print(x)
Write down the output:

Try it Yourself »

The math.trunc() method for example, returns the truncated integer parts of a
number:

Example
import math

x = math.trunc(12.6)

print(x)
Write down the output:
The math.ceil() method rounds a number upwards to its nearest integer, and the
math.floor() method rounds a number downwards to its nearest integer, and
returns the result:

Example
import math

x = math.ceil(1.4)
y = math.floor(1.4)

print(x) # returns 2
print(y) # returns 1

Try it Yourself »

The math.pi constant, returns the value of PI (3.14...):

Example
import math

x = math.pi

print(x)
Write down the output:

Practice:
Convert each of the following expressions into a Python statement:

Mathematical expression Python statement


a = math.sqrt(x + 1)
Exercise:

1. Write a program that calculates the total surface area of a circular cone.

The formula of the total surface area A is , where


r is the base radius, and h is the height.
The output of the program should match the sample below:
enter the base radius: 1.2

enter the height: 3.4

the total surface area is 18.116501500136074 square units.

2. Write a function that calculate the area of a triangle by the length of its
three sides, namely a, b and c. The formula for the area A is

, where s = (a+b+c)/2. (This is known as


Heron’s formula.)

Then write a program to use the function to calculate the area of a triangle, as

shown in the sample below.


enter the length of side 1: 13

enter the length of side 2: 14

enter the length of side 3: 15

the area of the triangle is 84.0 square units.

You might also like