Data Type: Numbers
There are two data types to represent numbers:
1. float
2. integers
There aren't many differences between them aside from the fact that a float has decimals. However, the operations we can perform in them are the same.
Python converts integers to floats automatically when an operation results in a number with decimals. Alright, let's get started by creating some variables and finding the type of these variables.
As you can see the type of num1 is an integer (int)
Now let's create a new variable called results and perform some operations.
results = num1 divided by num2!
Hit enter key, type results and it executes in the python terminal.
Let's have a pictorial view of it๐:
And now we have decimals, and hence our variable results are floats.
Verify with the code:
type(results)
<class 'float'>
So int and float can work together, as we can see, and most of the time you do not need to worry if your variable is an integer or a float because Python will take care of it.
Note: When dealing with thousands of numbers, you do not need to use thousand separators like a comma (,)) because you might have unexpected results. For example, 100,323.60
Also note that the dot (.)) is used to separate decimals.
There is also the modulus operator (%), which works by dividing the first number by the second number; it takes the result; and instead of continuing the division, it stops there and returns the remainder. This can be used in some situations, like figuring out if a number is odd or even. Let's look into it.
The result is going to be 1 for an odd number.
Another operator is the double asterisk, which makes the power operation, i.e., 5 raised to the power 4 can be coded as 5 ** 4.
You can use the parenthesis to represent the order of our operation. For example: (2+2)/3
and even ((2+2)/3)/4. For your exercise you try it out and see the hierarchy in which the operations are executed. Let me know in the comment section if you have any problems.
These are some of the basic operators you can do with numbers. There are also some built-in functions that can be applied to numbers, for example, the round() function we used to convert kilometers to miles. If we pass only one argument, which is the number we want to round, 4.87765, and nothing more, it's going to return an integer. Let's see below:
We can also pass a second argument in the function which is the numbe of decimal places we want to round our number to.
To avoid confusing this with a thousand separator ( , ), you can space out the number of decimal places you are rounding to.
What if we want to do more complex mathematical operations like logarithmic functions and Trigonometric functions ?
One of the best things in Python is that we can import modules and extend our possibilities, and because of that there's nothing we can't do with Python.
In order to use the functions i just mentioned we must use a module we call math that comes with the Python installation. We can call this module by writing import math. Inside this module, the are hundreds of maths functions we can use! One of them is the factorial function. For example: math.factorial(5) which is 120. It can be solved mathematically as 5 * 4 * 3 * 2 * 1 = 120
We also have more round opotions; we can round up or down using the ceil or floor function.
We also find the log this way; This gives us the natural logarithm of 20 to the base e.
As well as the pi value:
These are just some examples, to learn learn more about the maths module go to google and type: python maths module.
This is for python 3.
Go to the documentation, here you can explore functions you have available in the maths module.
There are also thousands of external modules made by other people that we need to install first, and then we can also import them.
I will also teach you how you can build your own modules. That's all we are going to learn about the Python math module ATM. Good luck and happy coding!
No comments:
Post a Comment