Python
/
Functions
- 1 Language 9
-
Hello World
-
Variables
-
Functions
-
Conditional
-
Operators
-
While
-
Turtle
-
Script Mode
-
Debugging
- 2 Strings 6
-
Slice
-
Raw Strings
-
Regex
-
Validation
-
Config
-
Escape
- 3 Collections 5
-
Lists
-
Dictionaries
-
Efficiency
-
Tuples
-
References
- 4 Functions 5
-
Recursion
-
Factorial
-
Modulus
-
Reassignment
-
Approximate
- 5 Storage 8
-
Files
-
Databases
-
Pipes
-
With open
-
Shelve
-
Zip
-
Csv
-
Json
- 6 Class 4
-
Definition
-
Attributes
-
Functional
-
Methods
- 7 Goodies 5
-
Conditional Expression
-
List Comprehension
-
Generator
-
Named Tuple
-
Modules
- 8 Applications 5
-
Pythagora
-
Palindrome
-
Binary Search
-
Conway Game
-
Coin Flip
- 9 Scheduler 4
-
Time
-
Multithreading
-
Subprocess
-
Logging
- 10 Packages 2
-
Clipboard
-
Ocr
/
Modulus
➟
➟
Last update: 01-12-2021
Positive
p 103 In Python integer division always return floor division.
# Modulus operation:
#
# dividend % divisor = reminder
#
# Python always does floor division
assert 0 % 7 == 0
assert 1 % 7 == 1
assert 2 % 7 == 2
assert 3 % 7 == 3 # 3 // 7 = 0 reminder 3
assert 7 % 7 == 0
assert 8 % 7 == 1
assert 9 % 7 == 2
assert 25 % 7 == 4
assert 7 % 25 == 7
Negative
Python modulus always return a number with the same sign as the denominator.
"""Modulus with negative numbers:
Python modulus (%) always return a number with ...
the same sign as the denominator.
Python applies the distribute law of Modulo operator:
(a + b) mod n = ((a mod n) + (b mod n)) mod n
Python always does floor division."""
# 23 % 5 = ?
assert 23 // 5 == 4
assert 23 == 4*5 + 3 # reminder 3
assert 23 % 5 == 3
# 23 % -5 = ?
assert 23 // -5 == -5
assert 23 == -5 * -5 - 2 # reminder -2
assert 23 % -5 == -2
# -3 % 7 = ?
assert -3 // 7 == -1
assert -3 == -1*7 + 4 # remainder 4
assert -3 % 7 == 4
# Distributive law
assert (2 + 2) % 3 == 1
assert (2 + 2) % 3 == (2 % 3 + 2 % 3) % 3
assert (2 + 2) % 3 != 2 % 3 + 2 % 3
➥ Questions github Functions