Python Programming for Beginners Lesson 2 Math Module
Introduction to Python
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming. Python is often described as a “batteries included” language due to its comprehensive standard
Python was conceived in the late 1980s as a successor to the ABC language. Python 2.0, released in 2000, introduced features like list comprehensions and a garbage collection system with reference counting.
Python 3.0, released in 2008, was a major revision of the language that is not completely backward-compatible, and much Python 2 code does not run unmodified on Python 3.
The Python 2 language was officially discontinued in 2020 (first planned for 2015), and “Python 2.7.18 is the last Python 2.7 release and therefore the last Python 2 release.” No more security patches or other improvements will be released for it. With Python 2’s end-of-life, only Python 3.5.xand later are supported.
Python interpreters are available for many operating systems. A global community of programmers develops and maintains CPython, a free and open-source reference implementation. A non-profit organization, the Python Software Foundation, manages and directs resources for Python and CPython development.
Rounding Numbers in Python
In mathematics, rounding a number means taking the number, removing the digits after the decimal point and then shifting it to it’s nearest integer value. So for example, say the number that I want to round is 1.6. Now on the number line 1.6 lies between the integers 1 and 2, so if I round 1.6, the result will be 2 since 2 is the integer value that 1.6 is most closest to (As 2 – 1.6 = 0.4 < 1.6 – 1 = 0.6). Similarly, if the number was supposed to be 1.4, then it will get rounded to 1, since 1 is the integer closest to 1.4 instead of 2 (As 1.4 – 1 = 0.4 < 2 – 1.4 = 0.6). One key thing to note here is that if I round 1.5, the output will be 2. Any value which lies exactly in between two integers will always get rounded to the highest integer i.e. if I round 2.5 it will get rounded to 3, 3.5 will get rounded to 4 and so on. So in order to do this rounding in Python, we can use the built in “round” function of Python which does the exact same work as described above. If I do round(2.1), Python is
going to return the value 2. If I type round(1.5), then Python is going to return me the value 2. As can be seen the color of the word “round” turns purple which indicates that it is a built in function of Python. However, to exploit the full computational power of Python we should be using the “math” module which provides a bunch of other different mathematical operations/functions as well.
Importing Python’s Math Module
A module is basically a collection of different functions. To import Python’s math module type “import math” and press Enter.
Sometimes we might want to force the rounding of a number to the next lowest integer or to the next highest integer i.e. if I do round(2.1) then it is going to give me the value 2. But what if I instead wanted it to return the next highest integer i.e. 3? Well that can be achieved using
the “ceil” function of the “math” module. To do that all we have to do is type math.ceil(2.1) and it is going to return me the value 3. Similarly if I want to retrieve the next lowest integer, i.e. if I do round(1.5) and instead of getting 2, I want Python to return the value 1 then all I need to do is use the “floor” function of the math module i.e. type in math.floor(1.5) and it will return us the value 1.
Along with having useful functions like “ceil” and “floor” Python also provides us the values for some important mathematical numbers such as ‘pi’ and ‘e’ (euler’s number). All we have to do is type math.pi and math.e in order to get their values. This can save us a lot of time while doing numerical computations.
Standard Trigonometric Functions
Before proceeding any further we should know how the curve of sine (‘sin’ in short) and cosine (‘cos’ in short) function looks like as in the next part we shall be dealing with the sine and cosine functions.
There is another trigonometric function called ‘tangent’ (tan in short) which is nothing but (tan(x) = sin(x)/cos(x)). As can be seen the sine curve increases from 0 to 1 as the values along the x axis go from 0 to pi/2 radian, then decreases back to 0 as x values go from pi/2 to pi. It continues further down to -1 as the x axis values approach 3pi/2 and then the cycle repeats again. Similar is the story of the cosine function as well, instead it starts from 1 goes down to 0 and then -1 and again climbs back up to 1. However, do keep in mind that the ‘pi’ that we are discussing here is not the mathematical ‘pi’ value that we saw earlier. This one is the ‘pi’ value in radians. A value of ‘pi’ in radians signifies an angle of 180 degrees. So pi/2 will be 90 degrees, 3pi/2 will be 270 degrees and so on. Any time you see ‘pi’ being used in trigonometric functions like sine, cosine, tan etc. always remember that ‘pi’ here will mean 180 degrees and not 3.14 like we saw earlier.
So now if we want to get the values of the sine and cosine functions at various radian values using Python we can use the built in ‘sin’ and ‘cos’ function of Python’s math module. To do that simply type in math.sin(math.pi/2) in order to get sine function value at pi/2 and math.sin(math.pi) in order to get sine function value at pi. We can also do something like math.cos(0) to get the value of cosine function at 0 radians, which is 1. We can also combine the ‘floor’ and ‘ceil’ functions with the cos and sin functions and do something like
math.floor(math.sin(math.pi)) which will give us the value 0. The ‘acos’ and ‘asin’ functions of the math module return the inverse of sine and cosine function i.e. if I do math.asin(0) then it will return the value 0 since sine function has the value 0 at 0 radians. If I do math.acos(0) it will give me the value 1.57 which is equal to (3.14/2) since at pi/2 radians the value of cosine function is 0. Remember that when
returning the values of ‘asin’ and ‘acos’ Python will not return the values to us in degrees or radians, so it will use ‘pi’ = 3.14 in order to compute those values and return something like 1.57 for pi/2 instead of returning 90 degrees. There is another function called ‘hypot’ which returns the hypotenuse of a right angled triangle. It will be more clear from the figure below.
As can be seen, the hypotenuse of a right angled triangle (a triangle which has a 90 degree in one of its vertex, here it is at the intersection of a and b) is the side which is opposite to the vertex containing the 90 degree angle (in this case it is the intersection of the sides a and b). The hypotenuse can be measured using the formula √��
2 + �� 2 where a and b are the length of the other two sides. If I do something like math.hypot(3,4) then a will get the value 3, b will get the value 4 and a2 + b2 will be equal to 32 + 42 = 25 and the square root of 25 i.e. √25 will be equal to 5 (square root of 25 has got two answers, +5 and -5 but we are discarding -5 since length can never be negative).
Squaring Numbers in Python
Squaring, cubing or raising a number to any power is possible with the help of Python’s inbuilt math.pow function. Raising a number ‘a’ to the power of ‘b’ is represented as ab which is equivalent to saying that I want to multiply ‘a’ with itself ‘b’ times i.e. a x a x a x ….b times. Raising 2 to the power of 3 i.e. 23 = 2 x 2 x 2 = 8. Similarly, raising 9 to the power of 2 i.e. 92 = 9 x 9 = 81. If we want to achieve the same results in Python, then what we can do is use the math.pow function. The syntax is as follows: math.pow(2, 3), this will be equal to doing 23 and will return the value 8. We can also do math.pow(9, 2) and this will return the value 92 = 81. A shorthand technique to do this would be to use the double star operator ‘**’ i.e. 2 ** 3 is the same as math.pow(2, 3) and 9 ** 2 is the same as math.pow(9, 2). The math module also provides another function called the ‘exp’ or exponentiation function which is nothing but raising the value ‘e’ i.e. euler’s number to any integer. So if I do math.exp(2), Python will solve e2 = e x e behind the scenes and return me the value 7.38(approx).
Before diving into natural logs, it is important to know what log actually is. In the simplest words, log stands for logarithm. Now logba is called a logarithm of ‘a’ with respect to the base ‘b’. If logba = n, then that means that in order to get ‘a’, we have to raise the base ‘b’ to the power of n i.e. we have to go on multiplying b ‘n’ times if we want to get the value ‘a’ (b x b x b x …n times = a). Now if the base ‘b’ is equal to euler’s number ‘e’ that we saw earlier, than the logarithm is called the “Natural Log” and logee = 1 (since we only have to use ‘e’ once in order to get the value ‘e’). If we want to do this in Python then all we have to do is type in math.log(math.e), this will feed the value of e to the natural log function and return us the value 1. If we want to change the base to some other number instead of e, let’s say we want to change it to 10 and calculate the value of log101000 in Python, then what we can do is math.log10(1000) and this will return us the value 3 as 10 x 10 x 10 = 1000. If we want to change the base to 2 and calculate log28 in Python, then we can do something like math.log2(8) and this will return us the value of 3 as 2 x 2 x 2 = 8.
That brings to the end of almost all the basic mathematical operations that we can perform in Python. You can use this pdf as a reference for a quick lookup if you ever forget any of the Python syntaxes or what some of those mathematical concepts meant.
One thing that you will be able to take away from this lecture is that Python is very capable of doing mathematical processes and there are plenty of functions available for that kind of thing. If you want more in depth stuff you might want to check out the NumPy and SciPy libraries for more advanced functionality that is best suited for data analysis.