
Syntax: Integer Division
Dividing one integer by another with the Python integer division operator.
Floor Division in Python
Division comes in two forms in Python, integer division and floating division. In this exercise you will be learning about the first of these two types, integer division.
Integer division can be defined as a form of division in which the quotient value after division is floored to the next integer value. For example, using integer division the calculation 13 divided by 4 would result in 3 as the quotient value since 3 is the whole number that remains after 13 is divided by 4.
There is a specific operator for performing integer division which is the symbol // and that is the operator you will be using in this exercise to perform floor division.
Typical syntax usage for the floor division operator are as follows:
-
When performing floor division on two numbers:
quotient = 60 // 35 -
When performing floor division on a variable and a number:
val = 53 quotient = val // 5 -
When performing floor division on two variable values:
val1 = 15 val2 = 4 quotient = val1 // val2
Practice: Integer Floor Division
Based on the above information, please complete the following steps:
-
Define a variable named
days_in_marchand assign it31. -
Define a variable named
days_per_weekand assign it7. -
Define a variable named
full_weeks_in_marchand, using the floor division operator and the previous variables, assign it the number of full weeks in March. -
Print the value contained in the
full_weeks_in_marchvariable.HINT*: Click here for a hint
You should see the number of full weeks in March:
> 4
SOLUTION*: Click here for an example solution
days_in_march = 31 days_per_week = 7 full_weeks_in_march = days_in_march // days_per_week print(full_weeks_in_march)
The floor division operator can also be used multiple times in a single statement:
quotient = 1050 // 5 // 6 // 2
Based on the above information, please complete the following steps:
-
Define a variable named
integer_quotientand assign it the result of dividing 1850 by 10 by 5 by 7 to theinteger_quotientvariable. -
Print out the value of the
integer_quotientvariable.HINT*: Click here for a hint
You should see the correct result of the above calculation:
> 5
SOLUTION*: Click here for an example solution
integer_quotient = 1850 // 10 // 5 // 7 print(integer_quotient)
Once you have completed the above steps and you are getting the correct output in the interpreter. You are ready to move on to the next exercise!