Comprehensive Python

Table Of Contents:

Python Data Types : Numbers

Numbers would be thought of as any numbers that we use in our everyday lives.
Numbers are used for different purposes, from simple mathematical calculations tho the advanced and complex computations.
It happen that the first data type we encounter in our data type series are Numbers.
In the below verses, we will vet through numbers in more details, and we will provide a simple shell for demystifying the power of python numbers.

Types Of Numbers

Although everything is number that concern the calculations, Numbers do come in different form, and they may be classified as:

Integers

The most common, and easiest number type is an integer. This are natural numbers that we are familiar with in calculations or in mathematical classes.
Example of integer data type would be 1, 2, 3, 4, 6, 8, 20, 100,etc In fact, all natuaral numbers without decimal pointer would be considered, and are the good examples of Integer number type.
Earliar versions of python also supported the Long integers etc.

Floats
Although the Integers are the type that gives no headache to our brains, there are still the decimal point numbers.
The decimal point numbers are all considered Floating Point numbers in programming.
An example of float python numbers would be 1.5, 3.14, 100.983647 etc If you are having a programming background from other languages, like C++, you would have noticed that there would have been the Double for double precision floating points.
something like this
double floatNumber ;
This is not the case in python, and would smatter out horrendous errors: In python counterpart, you would simply say:
floatNumber = 29.928746457
and you are good to go.

Complex
Another non-common data type that is also known in python is a Complex, and believe me, this type of numbers only very rare will you encounter, if not only here.
They are composed of two number parts, that is, the real part, and imaginary part.
Their physical appearance are moulded like this:
20+3j where 20 is a real part, and 3j is an imaginary number.
I may conclude they are used for scientific calculations, and rarely will you meet or use them during your programming.

All the number types above would be used for mathematical calculations and below is how we would use them in variables.
>>> integerNumber = 10
>>> floatNumber = 10.98
>>> complexNumber = 10+8j
>>> print(integerNumber,floatNumber,complexNumber) #   10  10.9  10+8j


Python Mathematics and Calculations

With the concept of Python numbers in mind, we're good to go with our mathematical calculations.
Let's start by running some few calculation.
In Python, we can do mathematical calculations using Arithmetic Operators, that is : "+" (plus sign) for addition,
"-" (minus Sign) for subtraction, "*" (asteric) for multiplication.
(two asterics can be used as power of a number, ie >>> 2**2 # 4 i.e 2 to the power 2),
and "/" (forward slash) for division.
"%" (Percentage sign) is also used to calculate the remainder of two integer division.
Taken we have two variables, a and be, where a = 10, and b = 20. Then you can do mathematical calculations as:
>>> a+b # 30
>>> b-a # 10
>>> a*b # 200
>>> b/a 2
In the console below, there are variables "a" and "b" declared for you already, as a = 10 and b = 20;
do mathematical calculations as using arithmetic operators as described in the above section.
You may also use print() python function to display your output,as, for instance
>>> print(b%a) # 20/10 remainder is 0.
>>>

The Math Module

The above was the basic mathematical calculations, though, we can do more mathematical complex operations,
like calculating square root of numbers, finding the square of a number, trigonometric calculations, i.e tangets, degrees,sine,cosine etc.
To do so, we will require a module called math. This module has capabilities that we will require in our day-to-day mathematical calculations. Let's get started with math module.
The math module is an external module of python. To get it into our python code, we need to import it before using it.
We import the external modules by writing one of the following:
>>>import math
>>>from math import *
>>>from math import functionname

import math will import the whole math module as a folder, and to get the internal files, that is, functions,
we use the "." sign. for example, to use the Power function of math module, pow() we write:
>>>import math
>>>math.pow(4,2) # 4 to the power 2 = 16
from math import functionname on the other part do the otherwise. Instead of importing the whole module as a folder.
it only take the function name and use it right away without using "." (scope operator).
That is, if we have a module (file) called "example.py" which contains a function called "func", then, instead of saying:
>>>example.func() to call our function in the file, we go strait and say func() and we are good to go.
Finally, we can see that using from module import function one-by-one to import our function, we will kinda get wearly and/or,
Our code will get more clustered (occupied).We can however solve this by importing all modules at once by using asteric "*".
This can be done as : from modulename import *, and everything in the modulename will be used as if we wrote it in our recent file.
More talk-talk, time to Walk-walk!, now that we know how to use modules, lets work with our math module as much as we can.
Trigonometry
Trigonometry is a subfield of maths that focus on shapes. Math has more trigonometric functions and below are the walk-through with them.
math.acos(): a math module function, which returns arc cosine of a number, measured in radians.
>>>math.acos(0.8) # 0.6435011087932843
math.acosh() : a math module function, return an inverse hyperbolic cosine.
>>>math.acosh(90) # 5.192925985263684
math.asin() : Just like math.acos but returns the arc sine of a given number.
>>>math.asin(0.8) # 0.9272952180016123
math.asinh() : Similar to math.acosh but this looks on hyperbolic sine.
>>>math.asinh(90) # 5.192987713658941
math.atan(): a math module function, which returns arc tangent of a number, measured in radians.
>>>math.atan(0.8) #0.6747409422235527
math.atan2() : a math module function, return an arc tangent of two numbers, that is, for instance 'y' and 'x'.
This functions like math.tan but this takes two numbers. however, consider the two code snippets below:
from __future__ import division # We want decimal points
z = 9/8 # 1.125
math.atan(z) # 0.844153986113171

Is just the same as below code:
>>>math.atan2(9/8) # 0.844153986113171
math.atanh() : Similar to math.acosh but this looks on inverse hyperbolic tangents.
>>>math.atanh(0.8) # 1.0986122886681098
math.ceil() : a math module function that returns the ceiling of number,
ie 'x' as a float.That is a smallest integral value of a number, greater than or equal to given number. look at this:
import math
math.ceil(1) # 1
math.ceil(1.1) # 2
math.ceil(1.9) # 2

math.copysign() : This math module function takes two numbers,
and returns the first number with a sign of a second number with it.
import math
math.copysign(4,-3) # -4 (4 is now negative)
math.copysign(-2,1) # 2 (2 is positive because it copied sign of 1)
math.copysign(2,5) # 2 (last number is positive)

math.cos() : Math module function which returns the cosine of a number.
>>>math.cos(0.8) # 0.6967067093471654
math.cosh() : Math module function which returns the hyperbolic cosine of a number.
>>>math.cos(0.8) # 1.3374349463048447
math.degrees() : Math module function which converts radians of an angle to degrees.
>>>math.degrees(1.5707963267948966) # 90.0 (right angle)
math.e : Math module object which returns the Euler Number.
>>>math.e # 2.718281828459045
math.exp() : Math module function which returns the Euler Number raised to a number passed to a function.
>>>math.exp(2) # (2.718281828459045 to the power 2: Euler**2)
math.expm1() : Math module function which returns the Euler Number raised to a number passed to a function, minus 1.
>>>math.expm1(2) # (2.718281828459045**2)-1 = 6.38905609893065
math.fabs() : Math module function (short of float absolute), which returns the absolute value of a floating point number.
>>>math.fabs(2) # 2.0
math.factorial() : Math module function which returns the factorial of a given number, ie x*(x-1)*(x-2)*...*(x-x+1).
>>>math.factorial(4) # 4*3*2*1 = 24
math.floor() : Math module function which returns the largest integral value, which is less than or equal to the given float.
>>>import math
>>>math.floor(2) # 2.0
>>>math.floor(2.1) # 2.0
>>>math.floor(2.9) # 2.9

math.fmod() : Math module function which returns the floating point number from modulus division of two numbers.
>>>math.fmod(5,3) # 5%3 converted to float = 2.0
math.frexp() : Math module function/method which returns pair (tuple) of mantissa and exponent of a given number.
>>>math.frexp(2) # (0.5,2)
math.fsum() : Math module function/method which returns an accurate, ie without truncating, sum of numbers of an iterable (lists/tuples).
>>>math.fsum([2.0,1.5,3.3]) # 2.0+1.5+3.3 = 6.8
math.hypot() : Math module function/method which returns euclidean distance, as described in the Pythagoras Theory, that is:
h =  a2 + b2.
>>>math.hypot(2,3) #  22 + 32 = 3.605551275463989
math.fisinf() : Math module function/method which returns a boolean indicating whether the given number is infinite or not.
>>>math.isinf(float('inf')) # True
math.isnan() : Math module function/method which returns a boolean indicating whether a passed float is not a number.
>>>math.isnan(float('nan')) # True
math.ldexp() : Math module function/method which takes two numbers ie x and y and returns the product(multiplication) of first number (x) and 2 to the power last number (y), that is x*(2**y).
>>>math.ldexp(2,3) # 2*(2**3) = 16
math.log() : Math module function/method which returns a logarithm of a number. It takes two numbers, x and y with the first number being the number we want to do log calculations, and the second, y, being the base.
If the base is not provided, the Natural logarithm (Euler) is used as a base. >>>math.log(2,10) # log 2 base 10 = 0.30102999566398114
math.log10() : Math module function/method which takes a numbers ie x and returns its log to base 10.
>>>math.log10(2) # log 2 base 10 = 0.30102999566398114
math.log1p() : Math module function/method which takes a number ie x and returns the natural logarithm of that number plus 1, that is math.log1p(x) is the same as math.log(x+1) .
>>>math.log1p(2) # math.log(2+1) = 1.0986122886681096
math.modf() : Math module function/method which takes a number ie x and returns the fractional part, and its integer part, both in floating point numbers.
>>>import math
>>>math.modf(2) # (0.0,2.0) 0.0 means no fractional part.
>>>math.modf(2.5) # (0.5,2.0) 0.5 is fractional part and 2.0 is integral part.
math.pi : Math module Object which returns the PI used in circle calculations. for instance, area of a circle is
area = πr2.
>>>math.pi # 3.141592653589793
math.pow() : Math module function/method which takes two numbers and returns the first number raised to the last number. That is, if numbers are 'x' and 'y', then it returns the results of xy.
>>>math.pow(2,3) # 23 = 8
math.radians() : Math module function/method which converts and angle from degrees to radians. for instance, an angle 'x', having 90 degrees, in radians is:
>>>math.radians(90) # 1.5707963267948966
math.sinh() : Math module function/method which returns the hyperbolic sine of a given number.
>>>math.sinh(5) # 74.20321057778875
math.sqrt() : Math module function/method which returns the square root of a given number.
>>>math.sqrt(9) # 3
math.tan() : Math module function/method which returns the tangent of a given number, measured in radians.
>>>math.tan(30) # -6.405331196646276
math.tanh() : Math module function/method which returns the hyperbolic tangent of a given number.
>>>math.tanh(30) # 1.0
math.trunc() : Math module function/method which returns the tangent of a given number, measured in radians.
>>>math.tan(30) # -6.405331196646276
math.trunc() : Math module function/method and the last, which truncates a given number to the nearest integral number.
It works as math.floor() but this returns integer.
>>>math.trunc(2.9) # 2

Try out some of the examples in the math module by running them in the console below 👇
>>>
This marks the end of our mathematical calculations and we will have working real life mathematical examples to the end of our data type series.