
Syntax: Division
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:
-
When dividing two integer values:
>15 / 3 5.0 -
When dividing an integer and a float value:
>15 / 3.5 4.285714285714286 -
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:
- Define a variable named
cook_time_in_seconds, and assign it the value135.0. - 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. - Print the value contained in the
cook_time_in_minutesvariable. - Check the type of the cook_time_in_minutes variable.
HINT*: Click here for a hint
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
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!