

Likewise, the expression 105 % 10 gives us the integer 5. The second line divides 4 by 3, which leaves a remainder of 1. What happens is that 4 is divided by 2, leaving a remainder of 0. What this operator does, is that it performs a division and then returns the remainder of the division. Modulo OperatorĪnother math operator, is the modulo operator. As you can see, using the exponential operator is much more convenient than typing all of that out manually. Without getting too much into math, this is the equivalent of multiplying 2 by itself 10 times, i.e. This raises 2 to the power of 10, also noted as 2 10, where 10 is the exponent. Exponential OperatorĪpart from the most common mathematical operators - being +, –, * and /) - Python also provides a handy operator for working with exponents. If we multiply by at least one decimal number, the result is a float value. Note that if we multiply two integers, the result is of the data type int. I’m sure it comes as no surprise to you that the above outputs 4. There is nothing special about multiplication in Python we just use an asterisk ( *) in the same way we did with addition, subtraction, and division. So the fact that divisions always result in floating point numbers is just convenient for us in case we need to perform some further operations on the number. whole numbers), but that might not necessarily be the case. In this case we are dividing two integers (i.e. Note that this number is actually of the type float - being numbers with decimals - instead of int.
PYTHON SIMPLE MATH QUIZ CODE
We will get back to all of this later, so don’t worry if that went over your head.Įither way, the above line of code prints out the number 3.0. For this argument, we can pass in a value directly, such as “Hello”, 22, or a mathematical expression as in this case. The point is that this function takes an argument. We haven’t talked about functions yet, so we will get back to that later.

When printing out values, we are using a function named print. Notice how I printed out the result of the division - being an expression - directly, instead of storing the result within a variable first. Let’s now try to divide c by two and print out the result. a = 4Įven though we used variables for the addition, we could just as well have entered numbers instead.Īs I am sure you can imagine, the above prints out the number 6. Let’s start out simple and add two numbers together, store the result in a variable and print out the result. Other ones include the exponentiation and modulo operators, which you will see in a moment. The basic ones are addition, subtraction, multiplication, and division.

Python supports all of the math operations that you would expect. That’s about to change, because now we are going to be working a bit with the basic math operators that Python provides. So far, we haven’t really done anything dynamically yet we declared and initialized variables and output their values.
