Libraries and functions
Imported Libraries and Functions#
In this chapter, we will cover some functions from various imported libraries that are commonly asked about, or used in Python. This chapter is not required to fully understand basics of Python. This chapter is meant to show further capability of Python, which can be utilized with what you already know about the language.
math#
The math library has many functions that are useful for programs that need to perform mathematical operations, that cannot be accomplished using the built in operators. This section assumes you have math training up to and including Trigonometry.
The following list shows all the functions in the math library:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | math.ceil math.copysign math.fabs math.factorial math.floor math.fmod (Not the most ideal for its purpose. Will not be explained.) math.frexp (Outside the scope of this tutorial. Will not be explained.) math.fsum math.isfinite math.isinf math.isnan math.ldexp math.modf (Outside the realm of this tutorial. Will not be explained.) math.trunc (Outside the realm of this tutorial. Will not be explained.) math.exp math.expm1 math.log math.log1p math.log10 math.pow math.sqrt math.acos math.asin math.atan math.atan2 math.cos math.hypot math.sin math.tan math.degrees math.radians math.acosh math.asinh math.atanh math.cosh math.sinh math.tanh math.erf math.erfc math.gamma math.lgamma math.pi math.e |
Of course, we won't cover every one of these functions. But we will cover a good chunk of them.
Let's start off by covering the two constants in the math
library. math.pi
is the mathematical constant "π"
, to available precision on your computer. math.e
is the mathematical constant "e"
, to available precision on your computer. Here is an example of both constants when entered in interactive mode in the Python shell.
1 2 3 4 5 | >>> import math >>> math.e 2.718281828459045 >>> math.pi 3.141592653589793 |
These constants can be stored in a variable just like any other number. Below is an example of such, and shows simple operations on those variables.
1 2 3 4 5 6 7 | >>> conste = math.e >>> (conste + 5 / 2) * 2.21 11.532402840894488 >>> constpi = math.pi >>> (((7 /2.1) % constpi) * 2) 0.38348135948707984 >>> |
Now, let's look at the functions. Let's start at the top of the list, and work our way down. Some of the functions will be skipped. At this point in the tutorial, you should be able to look at each of these examples to follow, and easily figure out what the example does. A simple sentence or two about what the function does will be provided.
Below is an example of every math
module function, and how it is used. (Excluding functions noted above as not to be explained)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | >>> import math >>> math.ceil(4.5) ** Rounds the number up to the nearest non decimal number ** 5 >>> math.ceil(4.1) 5 >>> math.copysign(4, -.4) ** Returns the number x with the sign of y in the context of (x,y) -4.0 >>> math.copysign(-4, 4) 4.0 >>> math.fabs(-44) ** Return the absolute value of the number ** 44.0 >>> math.factorial(4) ** Returns the factorial of a number ** 24 >>> math.floor(4.3) ** Rounds the number down to the nearest non decimal number. ** 4 >>> math.floor(4.99999) 4 >>> math.fsum([.1,.2,5,45.2,-.054,.4]) ** Returns the sum of all the numbers in the brackets. Not always precise ** 50.846000000000004 >>> math.isfinite(3) ** Returns True if the value is neither an infinity nor a NaN. Returns False otherwise. ** True |