AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Library, modules and the math module

Lessons 51–52 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
52

Practical lesson: the math module

Textbook: p. 103
GoalIn the practical lesson, uses the functions of the math module (sqrt, floor, ceil, factorial, log10, pi, sin) and round() for rounding, and calculates geometric tasks.
New words
math: the module for mathematical functions and constants · mathsqrt(): the square root of a number · sqrt()floor(): rounds down; ceil(): rounds up to a whole number · floor() va ceil()round(x, n): rounds x to n digits after the point; it is a built-in function · round()
Explanation

The math module gives ready functions for mathematical calculations; we load it with import math and use it as math.sqrt(x). math.sqrt(x) is the square root, math.factorial(n) is n!, math.log10(x) is the decimal logarithm, math.pi is the number π, and math.sin and math.cos take an angle in radians, so degrees must be converted with math.radians(). math.floor(x) rounds down and math.ceil(x) rounds up: floor(−1.5) = −2, ceil(−1.5) = −1. round(x, n) and abs(x) are built-in functions and do not need math. Real numbers are stored approximately in memory, so if we calculate sin 30° we get 0.4999…; the result must be rounded with round(). Taking the root of a negative number gives a ValueError. The area of an equilateral triangle is S = a² · √3 / 4, that is, math.sqrt(3) / 4 * a ** 2; do not confuse it with √(3/4).

FunctionJob
math.sqrt(x)square root
math.factorial(n)n!
math.log10(x)decimal logarithm
math.piπ
math.radians(x)converts degrees to radians
math.floor(x)rounds down
math.ceil(x)rounds up
💡 Where will I use this?
In real life

A class decorating a round table for a party needed a ribbon to go round its edge. The pupil measured the radius, used the ready value of pi to find the length and rounded it up to a whole number, so the ribbon was never too short.

Who uses it

Engineers, architects and programmers use ready functions such as square roots and rounding in calculations.

Try it today

Estimate on paper the square root or rounded value of three numbers, then compare with a more exact calculation.

Worked examples
Python
1import math
2print(math.sqrt(144), math.floor(2.7), math.ceil(2.1), abs(-5))
3
4import math
5a = 4
6print(round(math.sqrt(3) / 4 * a ** 2, 2))
Output
12.0 2 3 5
6.93
Functions – the first program above. The result is 12.0 2 3 5. An equilateral triangle (a = 4) – the second program above. The area is about 6.93.
Python
1import math
2print(math.sqrt(3 ** 2 + 4 ** 2), math.factorial(5), math.log10(1000))
3
4import math
5print(round(math.pi * 2 ** 2, 2), round(math.sin(math.radians(30)), 2))
Output
5.0 120 3.0
12.57 0.5
Hypotenuse and others – the first program above. In a right triangle with legs 3 and 4 the hypotenuse is 5.0, 5! = 120, log10(1000) = 3.0. A circle and sine – the second program above. The area of a circle of radius 2 is 12.57, sin 30° = 0.5.
Class activity

“Calculator engineer”: the group writes 3 small calculation programs in which the legs of a triangle, the radius of a circle and an angle are typed; the results are compared by hand and with a calculator.

Practice
1
What are math.floor(-1.5) and math.ceil(-1.5) equal to?
2
Write a program that finds the hypotenuse of a right triangle with legs 6 and 8.
3
What is round(3.14159, 2) equal to?
4
Why does math.sqrt(-4) give an error?
Story of the Week · The Khwarizmi WayPassing on the craftClose the week: all of this week’s subjects in one storyRead →