Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Integer Division

check

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:

  1. When performing floor division on two numbers:

      quotient = 60 // 35
    
  2. When performing floor division on a variable and a number:

      val = 53
      quotient = val // 5
    
  3. 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:

  1. Define a variable named days_in_march and assign it 31.

  2. Define a variable named days_per_week and assign it 7.

  3. Define a variable named full_weeks_in_march and, using the floor division operator and the previous variables, assign it the number of full weeks in March.

  4. Print the value contained in the full_weeks_in_march variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

    You should see the number of full weeks in March:

    > 4
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      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:

  1. Define a variable named integer_quotient and assign it the result of dividing 1850 by 10 by 5 by 7 to the integer_quotient variable.

  2. Print out the value of the integer_quotient variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

    You should see the correct result of the above calculation:

    > 5
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      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!