An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
5 Helpful Python Math Module Methods
Hi, my name is Aya Bouchiha, today, we are going to discuss 5 methods from the math module that you will probably use in your next project!
If you are a javascript developer, I recommend to you this article:
math.floor()
math.floor(): rounds a number down to the nearest integer.
import mathprint(math.floor(2.3)) # 2print(math.floor(1.89)) # 1print(math.floor(7.5)) # 7print(math.floor(-10.14)) # -11
math.ceil()
math.ceil() rounds a number up to the next largest integer.
import mathprint(math.ceil(2.3)) # 3print(math.ceil(1.89)) # 2print(math.ceil(7.5)) # 8print(math.ceil(-10.14)) # -10
math.trunc()
math.trunc(): returns the integer part of a number by deleting any fractional digits.
If you're confiused with math.trunc(), math.floor() and math.ceil(), you need to know that math.trunc() plays exactly the role of math.floor() when the giving number is positive, and It play the role of math.ceil() if the giving number is negative.
import mathprint(math.trunc(0.000000000000000001)) # 0print(math.trunc(2.5)) # 2print(math.trunc(8.999999999999999999)) # 9print(math.trunc(-7.8)) # -7
math.sqrt()
math.sqrt(): returns the square root of the giving positive number.
import mathprint(math.sqrt(4)) # 2.0print(math.sqrt(25)) # 5.0print(math.sqrt(100)) # 10.0print(math.sqrt(16)) # 4.0
math.degrees()
math.degrees(): this method returns the degrees of a given angle in radians.
import mathpi = math.pi print(math.degrees(1)) # 57.29577951308232print(math.degrees(pi)) # 180.0print(math.degrees(pi / 2)) # 90.0print(math.degrees(pi / 4)) # 45.0
Reference
Hope you enjoyed reading this post, Have a nice day!
- email: [email protected]
- telegram: Aya Bouchiha
Original Link: https://dev.to/ayabouchiha/5-helpful-python-math-module-methods-44gf
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To