Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Division

check

Divide one number by another with the Python addition operator.


Floating-Point Division in Python

Now that you have some experience with float values and a handful of the mathematical operators, it is time to learn about another operator, the division operator (/).

Unlike the floor division operator (//) which returns an integer value and throws away the remainder, the division operator (/) returns a float value which represents the quotient of the calculation.

Note the examples below:

  1. When dividing two integer values:

     >15 / 3
      5.0
    
  2. When dividing an integer and a float value:

     >15 / 3.5
      4.285714285714286
    
  3. When dividing two float values:

     >16.5 / 3.1
      5.32258064516129
    

As you can see, whatever combination is used with the division operator (/), the value returned will be a float.

Practice: Floating-Point Division

Based on the above information, please complete the following steps:

  1. Define a variable named cook_time_in_seconds, and assign it the value 135.0.
  2. Define a variable named cook_time_in_minutes, and using the division operator with the variable above, assign it the cook time converted to minutes.
  3. Print the value contained in the cook_time_in_minutes variable.
  4. Check the type of the cook_time_in_minutes 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 cook time in minutes, and the type for the cook_time_in_minutes variable:

> 2.25
> <class 'float'>
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.
cook_time_in_seconds = 135.0
cook_time_in_minutes = cook_time_in_seconds / 60.0
print(cook_time_in_minutes)
2.25
type(cook_time_in_minutes)
<class 'float'>

When the above has been completed, you are ready to move on to the next exercise!