Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python % Operator

00:00In the previous lesson, I introduced you to modular arithmetic. In this lesson,I’ll show you how to use the% operator to accomplish the same thing inPython.

00:12The% operator is the modulus operator.Just like in the previous lesson,13 mod 12 is 1. 13 divided by 12 has a remainder of 1,hence the result.

00:25Here’s another one for 15 mod 4. And a bigger value,240 mod 13. Because the mod operator involves division,you have the same problem if you do a mod by 0,as if you did a divide by 0.

00:40You’ll get aZeroDivisionError raised in this case.In the previous lesson, I showed you that negatives were possible.8 mod -3 is -1.You have to be a little careful with negative numbers.

00:54There’s two different ways of calculating mod, and you can get two differentresults depending on which mechanism you’re using.I’ll show you more about that a little later.

01:03The% operator also supports floating-point.Floating-point values going in result in floating-point values coming out.If you haven’t played much with floating-point before,you might be a little surprised at the following little piece of math.

01:18.1 + .1 + .1 isn’t0.3.This has to do with how your computer storesthese kinds of numbers and the floating-point standard. Mod on floating-point hasthe same kind of precision problem as this addition example I just showed you.

01:35Little inaccuracies sneak in.

01:39You should always be cautious with floating-point,and this is why you should never use floating-point values to store money.These kinds of errors accumulate over time.

01:49Python has a class calledDecimal, capitalD, inside of thedecimal module.It has arbitrary precision and doesn’t have this problem.

01:58I’ll show you more about these later.There are two ways of doing modular arithmetic in Python: the% operatorand inside of themath library,fmod().

02:14Generally, it’s recommended to use the% operator when dealing withintegers and thefmod() function when dealing with floating-point.

02:22Remember how I mentioned earlier that negatives are a problem?It turns out that% for integers and floats andfmod() calculate negativemodulus differently, and so you’ll get inconsistent results.

02:36Here’s a large exponent, and here’s the same using the% operator.You have a significantly different value showing up there.This problem with negatives that I’ve shown you isn’t just within Python.

02:49It’s also between different languages.Not all programming languages calculate the negative modulus the same way.8 % -3 in JavaScript is2. In Python,the same calculation is-1.

03:04There are two common ways of calculating mod.Here’s how Python’s% operator does it.The remainder is calculated asa minusn times the floor ofa divided byn.

03:18Here’s an example.13 mod 12 is 13 minus 12 times the floor of 13over 12.Or 13 minus 12 times the floor of 1.083.The floor of 1.083 is 1,so you end up with 13 minus 12 times 1,which is a remainder of 1. Now let’s do that againwith a negative number. 8 mod -3 gives you 8 minus -3times the floor of 8 divided by -3.

03:528 divided by -3 gives you minus 2.6 repeated,the floor of which is -3,giving you 9 on the right-hand side and remainder of -1.JavaScript’s% operator calculates things slightly differently. For thesame operation, instead of usingfloor(),it usestrunc(). For whole numbers, this isn’t a problem.

04:18The end result is the same calculation as you saw before.Unfortunately, for negative numbersit is a problem. When you get to thetrunc() step of 8 divided by -3,the truncation of -2.6 repeated is -2.

04:36This changes the value on the right-hand side and thus changes the result.

04:42In Python, the% operator usesfloor(),but themath.fmod() usestrunc().This is what causes the difference that I showed you earlier between negativevalues for% andfmod(). You need to be aware of thisif you’re doing modulus math on negative values.

05:018 % -3 is-1,whereas thefmod() on the same value is2.This is the same as the JavaScript versus Python example I just showed you.

05:13In addition to the mod methods I’ve shown you so far,Python also has a function calleddivmod().

05:20divmod() returns two values—both the result of the division and the remainder.12 goes into 13 once,so you get the first part of the tuple, and has a remainder of 1, giving you thesecond part of the tuple.

05:345 goes into 37 7 times and has a remainder of 2when it’s done. Thedivmod() function is the equivalent of running two differentPython operations at the same time.

05:47First off, strict integer division, expressed by the double slashes (//).37 // 5 is7. And secondly,% operator.37 % 5 is2,divmod(37, 5) giving you the same as these two resultsbut in one function call. Note thatdivmod() uses the same mechanism as%. So if you’re dealing with negative values,you’ll get the kind of calculation that the% operator does—not the kindof calculation thatfmod() uses. In Python,the% operator is considered to have the same operator precedence asdivision and multiplication.

06:314 times 10 is 40, 12goes into 40 3 times with a remainder of 4,4 minus 9 gives you -5.Adding some brackets to the same statement changes the value.Now you’re doing 40 mod 3 instead.

06:49That’s the basic usage of% andfmod()in Python. In the next lesson,I’ll show you some common uses of these operators inside of code.

Become a Member to join the conversation.

Course Contents

Overview
50%

[8]ページ先頭

©2009-2026 Movatter.jp