There are 3 types of division
When you do 10 / 4 on your calculator you get 2.5 as the answer. If you cut a 10 m long plank of wood into 4 equal pieces each piece will be 2.5 m long.
We code this in Python using the forward slash for the divide and we get the float answer as we'd expect.
plank_length = 10num_pieces = 4piece_length = plank_length / num_piecesprint("If you cut a", plank_length, " m plank of wood into", num_pieces, "equal pieces each piece will be", piece_length, " m long.")We get a float even if the numbers being divided are integers and the answer is actually an integer. This is because you usually get a float when you divide. 3/2 = 1,5, 7/8 = 0.875, 4/3 = 1.333333333, etc.
This type of dividing is great for sharing out things you can cut up or measure out, or finding averages, or a whole host of other things, but there are problems that need a different sort of division.
Sometimes we need to know how many whole lots of a number go into another number. This is 32 / 5 but that gives us 6.4, we only want to know the integer part, we don't care about the decimal bit. We're not going to eat 0.4 of a lollie and then give it to the next person to have their 0.4 of it!!
In the above example we want to know how many 5s in 32 to which the answer is 6 with 2 lollies left over. We can code this problem using double /
num_people = 5num_lollies = 32lollies_each = num_lollies // num_peopleprint("If", num_people, "share", num_lollies, "lollies they get", lollies_each, "each")The // tells Python to just give us just the integer bit of the answer.
This type of dividing is used when you can't cut the things up like working out how many busses you'll need to transport a lot of people, working out how many trays of kiwifruit you can fill or how many sevens teams you can make from the people who sign up.
In the above example each person got 6 lollies and there were 2 left over in the bag. The 2 left over are often called the remainder and we often need to know how many are left, or at least check if there are any left over or not. We can get the remainder by using %
full_bus = 48num_people = 578num_busses = num_people // full_bus #This line calculates how many full busses are needednum_left = num_people % full_bus #This line calculates how many people are left overif num_left > 0: #This line checks if there are any people left over num_busses = num_busses + 1 #and if there are it orders another busprint("To transport", num_people, "you will need", num_busses," busses.")A number is even if it divides exactly by 2, so there is no remainder when you divide it by 2. In Python terms this is number % 2 == 0
You can use this to test if a number is even like this
number = int(input("Enter number: ")if number % 2 == 0: print("That number is even")else: print("That number is odd")You can check if a number divides by other numbers by changing the number after the %
This modulo division is used to encrypt your communications. Everytime your phone or computer sends a message it is encrypted to stop other people reading it. This encryption makes use of modulo division because it can't be reversed. There are lots of numbers that give the same answer after modulo division.
eg 8 % 7 = 1 15 % 7 = 1 22 % 7 = 1 29 % 7 = 1 as do 36, 43, 50, ..., 701, 708, 715, ..., 2498800, and so on
So even if you know that the character being transmitted is a 1, and you know that % 7 was used, you still don't know what the original letter was - it could be 8 or 15 or 36 or 2,498,800.