Skip to content

Commit

Permalink
Added info on modulus
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybrown8 committed Nov 20, 2015
1 parent ba2909f commit 7499e1f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lesson4.html
Expand Up @@ -159,6 +159,34 @@ <h3>Single Responsibility</h3>
one answer. So you have to keep its responsibilities very narrow and focused.</p>


<h1>Arithmetic: A special kind of function</h1>

<P>You're famiilar with arithmetric; plus (+) for add, minus (-) for subtract,
star (*) for multiply, and slash (/) for divide. These are called mathematical
operators. The operators are also functions, but they take very specifically two
numerical parameters, and return one numerical answer.</p>

<p>There's one more mathematical operator you should know about. It is
called mod, short for modulus, which means "the remainder that's left over
after doing division." Computers use the % symbol for modulus (not for percent, oddly enough).
For example, 9 divided by 2 is 4 with remainder 1. So 9 / 2 equals 4, and 9 % 2 equals 1.</p>

<div class="code">a = 9
b = 4
print("a divided by b equals ", a / b)
print("a mod b equals ", a % b)
</div>

<p>A number that is evenly divisible by another gives a remainder of zero.
You can check that the modulus (remainder) is zero to verify that a number was
evenly divided. For example:
<ul>
<li>6 / 2 = 3 remainder 0</li>
<li>20 / 5 = 4 remainder 0</li>
<li>100 / 10 = 10 remainder 0</li>
<li>10 / 3 = 3 remainder 1 (not divisible!)</li>
</ul>


<h1>Review</h1>

Expand Down
30 changes: 30 additions & 0 deletions lesson4exercises.ipynb
Expand Up @@ -163,6 +163,36 @@
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's a reminder of how modulus works to show the remainder after division."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('a divided by b equals ', 2)\n",
"('a mod b equals ', 1)\n"
]
}
],
"source": [
"a = 9\n",
"b = 4\n",
"print(\"a divided by b equals \", a / b)\n",
"print(\"a mod b equals \", a % b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 7499e1f

Please sign in to comment.