Non-Programmer's Tutorial for Python 3/Intro to Imported Libraries and other Functions
Intro to Imported Libraries and other 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.
- 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:
- 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.modf (Outside the realm of this tutorial. Will not be explained.)
- math.trunc (Outside the realm of this tutorial. Will not be explained.)
- 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.
>>> 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.
>>> 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)
>>> 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
xwith the sign of
yin 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
Trueif the value is neither an infinity nor a NaN. Returns
Falseotherwise. ** True