By the end of this lesson, you'll be able to:
Definition: Python Math Module allows us to do advanced mathematics with Python - like having a super calculator in your code!
Think of the Math Module as a special toolbox filled with mathematical superpowers! :hammer_and_wrench:
The Python Math Module is a standard module that was built-in with Python - it's already there waiting for you to use it!
After importing the Math module, it allows us to use mathematical functions such as the square root function(√) or even get a constant number such as the Pi(π) number.
:information_source: Remember! To use the Math Module, you must first import it! In EduBlocks, look for the "import math" block at the very beginning of your program.
One of the most popular math constants is the Pi(π) number. This is a shortcut way of writing the Pi number without literally typing out the exact values as the decimal place can be infinite.
To get the Pi constant number, just type math.pi to represent the float number 3.141...
Input | Output |
---|---|
![]() |
![]() |
:bulb: Quick Tip!
Remember that Pi is approximately 3.14159... but using math.pi gives you a much more accurate value automatically!
As for the math methods, these are mathematical functions that require arguments and it will return the calculations to you.
Here are some of the most commons math methods used:
Method | Description | Example | Result | Real-World Use |
---|---|---|---|---|
math.sqrt(x) | Returns the square root of a number | math.sqrt(9) | 3 | Finding side lengths of squares |
math.pow(x, y) | Returns the value of x to the power of y | math.pow(2, 3) | 8 | Calculating areas and volumes |
math.floor(x) | Rounds a number down to the nearest integer | math.floor(9.6) | 9 | Counting whole items |
math.ceil(x) | Rounds a number up to the nearest integer | math.ceil(1.4) | 2 | Calculating how many containers you need |
math.sin(x) | Returns the sine of a number | math.sin(10) | -0.544021110889 | Animation and game physics |
math.cos(x) | Returns the cosine of a number | math.cos(10) | -0.839071529076 | Creating circular patterns |
math.tan(x) | Returns the tangent of a number | math.tan(90) | -1.99520041221 | Calculating angles and slopes |
math.radians(x) | Converts a degree value into radians | math.radians(180) | 3.14159265359 | Working with angles in Python |
math.degrees(x) | Converts an angle from radians to degrees | math.degrees(7.2) | 412.529612494 | Making angles easier to understand |
:bulb: Troubleshooting Tip!
If you get an error saying "math is not defined", make sure you have the "import math" block at the very beginning of your program!
Create a program that:
Create a game that:
When you drag the math blocks in EduBlocks, you're actually writing real Python code! For example:
math.sqrt()
in Pythonmath.pi
in PythonThis means you're already learning real programming while having fun with blocks!
Amazing job learning about the Math Module! You now have the power to:
Next up, you'll learn even more exciting Python features. Keep up the fantastic work, mathematician-programmer! :tada: