Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

≀Paulo Portela
≀Paulo Portela

Posted on • Edited on

Introduction to the Python Math Library

Introduction

Python has a built-inmath library that provides a wide range of mathematical functions and constants. In this chapter, we'll explore some of the most commonly used functions and constants in the math library, including pi, square root, cosine, sine, absolute value, least common multiple, and factorial.

Pi

The constantpi represents the ratio of a circle's circumference to its diameter and can be accessed usingmath.pi. Here's an example of usingpi to calculate the area of a circle with a given radius:

importmathradius=5area=math.pi*radius**2print(f"The area of the circle is{area}")
Enter fullscreen modeExit fullscreen mode
Output:The area of the circle is 78.53981633974483
Enter fullscreen modeExit fullscreen mode

Square Root

Thesqrt function calculates the square root of a given number. Here's an example of using thesqrt function to calculate the length of the hypotenuse of a right triangle with sides of length 3 and 4:

importmatha=3b=4c=math.sqrt(a**2+b**2)print(f"The length of the hypotenuse is{c}")
Enter fullscreen modeExit fullscreen mode
Output:The length of the hypotenuse is 5.0
Enter fullscreen modeExit fullscreen mode

Cosine and Sine

Thecos andsin functions calculate the cosine and sine of a given angle in radians, respectively. Here's an example of using thecos andsin functions to calculate thex andy coordinates of a point on the unit circle at a given angle:

importmathangle=math.pi/4# Angle in radiansx=math.cos(angle)y=math.sin(angle)print(f"The coordinates of the point are ({x},{y})")
Enter fullscreen modeExit fullscreen mode
Output:The coordinates of the point are (0.7071067811865476, 0.7071067811865475)
Enter fullscreen modeExit fullscreen mode

Absolute Value

Thefabs function calculates the absolute value of a given floating-point number. Here's an example of using thefabs function to calculate the distance between two points on a number line:

importmathx1=-3x2=4distance=math.fabs(x1-x2)print(f"The distance between the two points is{distance}")
Enter fullscreen modeExit fullscreen mode
Output:The distance between the two points is 7.0
Enter fullscreen modeExit fullscreen mode

Least Common Multiple

Thelcm function calculates the least common multiple of the specified integer arguments. Here's an example of using thelcm function to calculate the smallest number that is divisible by both 4 and 6:

importmathx=4y=6result=math.lcm(x,y)print(f"The least common multiple of{x} and{y} is{result}")
Enter fullscreen modeExit fullscreen mode
Output:The least common multiple of 4 and 6 is 12
Enter fullscreen modeExit fullscreen mode

Factorial

Thefactorial function calculates the factorial of a given non-negative integer. Here's an example of using thefactorial function to calculate the number of ways to arrange 5 distinct objects in a row:

importmathn=5result=math.factorial(n)print(f"There are{result} ways to arrange{n} distinct objects in a row")
Enter fullscreen modeExit fullscreen mode
Output:There are 120 ways to arrange 5 distinct objects in a row
Enter fullscreen modeExit fullscreen mode

Conclusion

Themath library in Python provides a wide range of mathematical functions and constants that make it easy to perform complex calculations. Whether you're working on a math problem or building a scientific model, themath library has the tools you need to get the job done.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Portugal
  • Education
    ISEP
  • Work
    Senior Software Developer @ adidas
  • Joined

More from≀Paulo Portela

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp