Operators are special tokens that represent computations like addition, multiplication and division. The values the operator works on are called operands. The following are all legal Python expressions whose meaning is more or less clear: The tokens When a variable name appears in the place of an operand, it is replaced with the value that it refers to before the operation is performed. For example, what if we wanted to convert 645 minutes into hours. In Python 3, division is denoted by the operator
token What if, on the other hand, we had wanted to know how many whole hours there are and how many minutes remain. To help answer this question, Python gives us a second flavor of the division operator. This version, called integer division, uses the token Pay particular attention
to the first two examples above. Notice that the result of floating point division is The modulus operator, sometimes also called the remainder operator
or integer remainder operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign ( In the above example, 7 divided by 3 is 2 when we use integer division and there is a remainder of 1 when we use the modulus operator. The modulus operator turns out to be surprisingly useful. For example, you can
check whether one number is divisible by another—if Finally, returning to our time example, the remainder operator is extremely useful for doing conversions, say from seconds, to hours, minutes and seconds. If we start with a number of seconds, say 7684, the following program uses integer division and remainder to convert to an easier form. Step through it to be sure you understand how the division and remainder operators are being used to compute the correct values. Activity: CodeLens 2.7.5 (ch02_19_codelens) Check your understanding
What value is printed when the following statement executes?
What value is printed when the following statement executes?
What value is printed when the following statement executes? You have attempted of activities on this page What is the modulus operator used for?The modulus operator returns the remainder of a division of one number by another. In most programming languages, modulo is indicated with a percent sign. For example, "4 mod 2" or "4%2" returns 0, because 2 divides into 4 perfectly, without a remainder.
What operation is used for modulus division remainder?The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division. produces the remainder when x is divided by y.
Can modulus operator be used for integer?The modulo operator, like the other arithmetic operators, can be used with the numeric types int and float .
What is the result of a modulus operation?In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the modulus of the operation).
|