|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-modulo-summary/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/python-modulo-summary/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fpython-modulo-summary%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-modulo-operator |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-modulo-operator |
| https://realpython.com/courses/python-modulo-operator/#team |
| Python Modulo: Using the % Operator | https://realpython.com/courses/python-modulo-operator/ |
| Christopher Trudeau | https://realpython.com/courses/python-modulo-operator/#team |
| Recommended Tutorial | https://realpython.com/python-modulo-operator/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-modulo-operator/downloads/modulo-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-modulo-operator/downloads/modulo-sample-code/ |
| Ask a Question | https://realpython.com/lessons/python-modulo-summary/#discussion |
| https://realpython.com/feedback/survey/course/python-modulo-operator/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-modulo-operator/disliked/?from=lesson-title |
| Contents | https://realpython.com/lessons/python-modulo-summary/#description |
| Transcript | https://realpython.com/lessons/python-modulo-summary/#transcript |
| Discussion | https://realpython.com/lessons/python-modulo-summary/#discussion |
| Python Expressions | https://docs.python.org/3/reference/expressions.html#index-68 |
| Python Floating Point | https://docs.python.org/3/tutorial/floatingpoint.html |
| IEEE Standard for Floating-Point Arithmetic (IEEE 754) | https://en.wikipedia.org/wiki/IEEE_754 |
| Exploring HTTPS and Cryptography in Python | https://realpython.com/courses/exploring-https-cryptography/ |
| Decimal Module | https://docs.python.org/3/library/decimal.html |
| Standard operators as functions | https://docs.python.org/3/library/operator.html |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson_preview&utm_content=python-modulo-operator |
| Sign-In | https://realpython.com/account/login/ |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson_preview&utm_content=python-modulo-operator |
| Sign-In | https://realpython.com/account/login/ |
| 00:00 | https://realpython.com/lessons/python-modulo-summary/#t=0.48 |
| In the previous lesson, | https://realpython.com/lessons/python-modulo-summary/#t=0.48 |
| I showed you how to use .__mod__() and the % operator in your own classes. | https://realpython.com/lessons/python-modulo-summary/#t=1.56 |
| In this lesson, I’ll wrap up and summarize the course. | https://realpython.com/lessons/python-modulo-summary/#t=6.6 |
| 00:11 | https://realpython.com/lessons/python-modulo-summary/#t=11.64 |
| Python’s % operator and divmod() function are used to calculate division and | https://realpython.com/lessons/python-modulo-summary/#t=11.64 |
| remainders. 14 divided by 4 is 3 remainder 2. Using divmod(), | https://realpython.com/lessons/python-modulo-summary/#t=16.14 |
| you can do exactly that calculation, getting the 3 remainder 2. | https://realpython.com/lessons/python-modulo-summary/#t=22.77 |
| 00:26 | https://realpython.com/lessons/python-modulo-summary/#t=26.97 |
| Or using the % operator, you can get just the remainder. | https://realpython.com/lessons/python-modulo-summary/#t=26.97 |
| The mod operation can be done on both int and float. | https://realpython.com/lessons/python-modulo-summary/#t=32.1 |
| The precision problems that you can encounter with float numbers are also | https://realpython.com/lessons/python-modulo-summary/#t=36.09 |
| present when you do mod on floats. | https://realpython.com/lessons/python-modulo-summary/#t=39.66 |
| 00:44 | https://realpython.com/lessons/python-modulo-summary/#t=44.22 |
| Additionally, | https://realpython.com/lessons/python-modulo-summary/#t=44.22 |
| the math module has the fmod() function that also does the modulus operation. | https://realpython.com/lessons/python-modulo-summary/#t=44.97 |
| An important thing to note is that the % operator and fmod() calculate | https://realpython.com/lessons/python-modulo-summary/#t=50.91 |
| negative mods differently. 8 % -3 is -1, | https://realpython.com/lessons/python-modulo-summary/#t=56.43 |
| whereas fmod() returns 2. | https://realpython.com/lessons/python-modulo-summary/#t=62.13 |
| 01:06 | https://realpython.com/lessons/python-modulo-summary/#t=66.12 |
| In a compound statement, | https://realpython.com/lessons/python-modulo-summary/#t=66.12 |
| the % operator has the same precedence as multiplication and division, | https://realpython.com/lessons/python-modulo-summary/#t=67.35 |
| so it may be important to use brackets to get the result you’re intending. | https://realpython.com/lessons/python-modulo-summary/#t=72.39 |
| 01:17 | https://realpython.com/lessons/python-modulo-summary/#t=77.76 |
| One of the most common uses of the mod operator in coding is to determine | https://realpython.com/lessons/python-modulo-summary/#t=77.76 |
| whether or not a number is even. | https://realpython.com/lessons/python-modulo-summary/#t=81.75 |
| You could do that by doing % 2 and checking for 0. Mod is also very | https://realpython.com/lessons/python-modulo-summary/#t=84.21 |
| helpful when you need to cycle through a list, | https://realpython.com/lessons/python-modulo-summary/#t=90.21 |
| wanting to loop it back to the beginning | https://realpython.com/lessons/python-modulo-summary/#t=92.7 |
| without having to check whether or not you’ve passed the end. | https://realpython.com/lessons/python-modulo-summary/#t=94.62 |
| 01:37 | https://realpython.com/lessons/python-modulo-summary/#t=97.65 |
| The appropriate use of the mod operator can make your code more succinct and | https://realpython.com/lessons/python-modulo-summary/#t=97.65 |
| require fewer boundary checks. | https://realpython.com/lessons/python-modulo-summary/#t=101.76 |
| Mod is often used for unit conversion as well. | https://realpython.com/lessons/python-modulo-summary/#t=105.45 |
| 01:48 | https://realpython.com/lessons/python-modulo-summary/#t=108.75 |
| This tends to be a good place to use divmod(). Converting a total number of | https://realpython.com/lessons/python-modulo-summary/#t=108.75 |
| minutes into the number of days, hours, and minutes that is | https://realpython.com/lessons/python-modulo-summary/#t=113.55 |
| can be done with two quick calls to divmod(). | https://realpython.com/lessons/python-modulo-summary/#t=117.36 |
| 02:00 | https://realpython.com/lessons/python-modulo-summary/#t=120.81 |
| days and extra_minutes are the divisor and the remainder of the 1440 minutes in | https://realpython.com/lessons/python-modulo-summary/#t=120.81 |
| a day, | https://realpython.com/lessons/python-modulo-summary/#t=127.14 |
| and then taking the result from the previous line and running divmod() on that | https://realpython.com/lessons/python-modulo-summary/#t=128.31 |
| for 60 will give you the remaining hours and minutes in the calculation. | https://realpython.com/lessons/python-modulo-summary/#t=133.17 |
| 02:19 | https://realpython.com/lessons/python-modulo-summary/#t=139.11 |
| Like all operators in Python, % maps to a class method. | https://realpython.com/lessons/python-modulo-summary/#t=139.11 |
| The % method maps to .__mod__(). | https://realpython.com/lessons/python-modulo-summary/#t=144.33 |
| This can be found in Python’s actual source code and is something that you can do | https://realpython.com/lessons/python-modulo-summary/#t=147.93 |
| yourself when you’re writing your own classes | https://realpython.com/lessons/python-modulo-summary/#t=151.98 |
| if you wish to support the mod operator. | https://realpython.com/lessons/python-modulo-summary/#t=154.2 |
| 02:38 | https://realpython.com/lessons/python-modulo-summary/#t=158.37 |
| One of the best places to get answers to your questions about Python is the | https://realpython.com/lessons/python-modulo-summary/#t=158.37 |
| Python documentation. Here’s the link to the expressions page. | https://realpython.com/lessons/python-modulo-summary/#t=161.82 |
| The subsection on binary arithmetic operations includes information on the mod | https://realpython.com/lessons/python-modulo-summary/#t=166.35 |
| operator. To better understand floating-point, | https://realpython.com/lessons/python-modulo-summary/#t=171.9 |
| you can look at the Python floating-point | https://realpython.com/lessons/python-modulo-summary/#t=175.5 |
| documentation, or Wikipedia has an article on the IEEE 754 standard | https://realpython.com/lessons/python-modulo-summary/#t=177.42 |
| that specifies how floating-point arithmetic is meant to work. | https://realpython.com/lessons/python-modulo-summary/#t=184.3 |
| 03:09 | https://realpython.com/lessons/python-modulo-summary/#t=189.97 |
| If cryptography is your thing, | https://realpython.com/lessons/python-modulo-summary/#t=189.97 |
| the course on exploring HTTPS and cryptography shows you how to use mod to | https://realpython.com/lessons/python-modulo-summary/#t=191.65 |
| create Caesar ciphers as well as the generation of RSA keys. | https://realpython.com/lessons/python-modulo-summary/#t=197.2 |
| 03:23 | https://realpython.com/lessons/python-modulo-summary/#t=203.08 |
| Here’s the link to the Python decimal module, | https://realpython.com/lessons/python-modulo-summary/#t=203.08 |
| if you wish to dig into that a little further. | https://realpython.com/lessons/python-modulo-summary/#t=205.51 |
| And this page describes all of the dunder methods and what operators map to | https://realpython.com/lessons/python-modulo-summary/#t=208.42 |
| them. I hope this course has been useful for you. | https://realpython.com/lessons/python-modulo-summary/#t=213.55 |
| 03:38 | https://realpython.com/lessons/python-modulo-summary/#t=218.17 |
| Thanks for your attention. | https://realpython.com/lessons/python-modulo-summary/#t=218.17 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/overriding-dunder-mod/ |
| Overview | https://realpython.com/courses/python-modulo-operator/ |
|
Python Modulo: Using the % Operator (Overview) 01:44
| https://realpython.com/videos/python-modulo-overview/ |
|
Modular Arithmetic 03:36
| https://realpython.com/videos/modular-arithmetic/ |
|
Python % Operator 07:00
| https://realpython.com/lessons/python-mod-operator/ |
|
Common Uses of % 08:57
| https://realpython.com/lessons/common-uses-mod/ |
|
Advanced Uses and Overriding .__mod__ 09:00
| https://realpython.com/lessons/overriding-dunder-mod/ |
|
Python Modulo: Using the % Operator (Summary) 03:40
| https://realpython.com/lessons/python-modulo-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover