In this section, we will do some work on mathematical operation which are multiplication, division, addition, subtraction, modulus, exponent, and floor division.
You might want to look at my previous post, it has some example of mathematics operations (arithmetic and algebra).
On the editor window of Python, enter the following :
a=35
b=22.78
Let's add these a and b together using the addition operator. For the recommendation practice, may we add a comment before the lines of code
#Addition
print(a+b)
print()
Using subtraction operation :
#Subtraction
print(a-b)
print()
Save the file, run the module (Run>Run Module) or press F5.
The output will be :
Next, we go with multiplication and division :
#Multiplication
print(a*b)
print()
#Division
print(a/b)
print()
Save the file and run the file again, you will get the following output :
Other operation that we will try is modulus, which is denoted by % sign.
Modulus operator displays the remainder of a division operation.
Let's try with below examples :
#Modulus
print(8%4)
We will see 0 on the displayed shell window, because 8/4 doesn't have a remainder.
Let's try again :
print(9%4)
After saving and running the file, the result will of 1 will be appeared on the shell window, because 9 divided by 4 is 2 remainder 1.
Let's try other example :
print(15%6)
The result will be 3, since 15 divided by 6 is 2 remainder 3.
Next, let's try on exponential operation.
Exponentiation raises a number to the power of another number. The resulting number is known as the exponent.
Example, 2 raised to the 3rd power, or 2*2*2, is 8In Python, the operation will be :
print(2**3)
Let's try other example :
print(12**2)
The result in the shell window will be :
Another operation to discuss is Floor Division or Integer, which symbolize by 2 forward slashes (//).
Floor division displays the product of a division operation as a rounded down number.
Let's try this operation :
print(9//4)
You will get 2 on the result. This is because 9 divided by 4 is 2, when rounded down.
We can also type the command as follow :
print(int(9/4))
Which get the same result as INT function is rounded the nearest whole number.
Other example :
print(20//6)
or
print(int(20/6))
The result shown on the shell window will be 3. This is because 20 divided by 6 is 3, not counting the remainder.
How to write code that not just show the result only, but also the statements :
Comments
Post a Comment