Skip to content
Snippets Groups Projects
Commit 7bace4bf authored by Blaise Li's avatar Blaise Li
Browse files

Update exercise with divisions.

parent 960814c9
No related branches found
No related tags found
No related merge requests found
Pipeline #83342 passed
...@@ -17,39 +17,28 @@ Assume that we execute the following assignment statements: :: ...@@ -17,39 +17,28 @@ Assume that we execute the following assignment statements: ::
width = 17 width = 17
height = 12.0 height = 12.0
delimiter ='.'
For each of the following expressions, write the value of the expression and the type (of the value of For each of the following expressions, write the value of the expression and the type (of the value of
the expression) and explain. the expression) and explain.
#. width / 2 #. width / 2
#. width / 2.0 #. width // 2
#. height / 3 #. height / 3
#. 1 + 2 * 5 #. 1 + 2 * 5
Use the Python interpreter to check your answers. ::
>>> width = 17 Use the Python interpreter to check your answers. ::
>>> height = 12.0
>>> delimiter ='.'
>>>
>>> width / 2
8
>>> # both operands are integer so python done an euclidian division and threw out the remainder
>>> width / 2.0
8.5
>>> height / 3
4.0
>>> # one of the operand is a float (2.0 or height) then python pyhton perform a float division but keep in mind that float numbers are approximation.
>>> # if you need precision you need to use Decimal. But operations on Decimal are slow and float offer quite enough precision
>>> # so we use decimal only if wee need great precision
>>> # Euclidian division
>>> 2 // 3
0
>>> # float division
>>> 2 / 3
0.6666666666666666
>>> width = 17
>>> height = 12.0
>>> width / 2
8.5
>>> width // 2
8
>>> # // is to perform an euclidean division
>>> height / 3
4.0
>>> 1 + 2 * 5
11
Exercise Exercise
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment