Lesson 6. Arithmetic Operators
The arithmetic operators in Python should be just as familiar to you as they were in grade school. However, there are three unusual operators that we need to talk about real quick.
The first strange operator is integer division which is performed by a double forward slash between the numerator and denominator. Integer division rounds down to the nearest integer value.
Modulo, which is performed by the percent sign between the numerator and denominator, returns the remainder value from a division operation.
Exponent you should be familiar with. The only odd thing about it is exponent operations are usually performed by ^ in other languages. In this case, it is performed with two asterisks.
Examples
Basic Math Operations
I want to point out two things. The first is the print statements.
Do you see how each variable is preceded by str? That is because those variables are either integers or floats. If we want to smoosh a number together with a string, we have to cast the number as a string and that is what the str method does.
The second thing is the value of the division operation. If you are sharp you will see that we divided two integer values. Despite the fact that the result was an integer, the data type of division resulted in a float.
Now you try it!
Don't copy and past. Type the code yourself!
Last updated