Three more arithmetic operators.
// is floor division: it divides and drops the fractional part, keeping the whole number.
print(17 // 5)
Output:
3
% is the modulo operator: it gives the remainder after division. In other words, it tells you how much is left over after splitting a quantity into whole units.
print(17 % 5)
Output:
2
17 divided by 5 is 3 with 2 left over — // gives the 3, % gives the 2. Together they split a quantity into whole units and a remainder. A common use of %: a number is divisible by n exactly when number % n is 0.
** raises a number to a power:
print(2 ** 10)
Output:
1024
The starter code creates minutes holding 200.
hours — the number of whole hours in minutes, using //.leftover — the minutes that remain, using %.cube holding 4 ** 3.hours, leftover, and cube.Run your code to see the output, then press Submit.
import unittest
class TestMoreArithmetic(unittest.TestCase):
def test_hours_is_3(self):
self.assertEqual(hours, 3)
def test_leftover_is_20_minutes(self):
self.assertEqual(leftover, 20)
def test_cube_is_64(self):
self.assertEqual(cube, 64)
minutes = 200 hours = minutes // 60 leftover = minutes % 60 cube = 4 ** 3 print(hours) print(leftover) print(cube)
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.