home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
lab06-casting
quotient vs division
casting ints/floats
Recall that + means two different things, depending on context
(whether the +-sign is surrounded by strings or by numbers1
It turns out, / means two different things, depending on context:
- 7.0 / 3.0 = 2.333333, as you might expect.
This is “floating-point division”; it's like the long-division
you learned in school.
- 7 / 3 = 2.
This is “integer division”, also called “quotient”;
it's the division you did in grade school before learning long-division:
If you have 7 copies of a foil Charmander pokemon card, and three friends,
then dividing equally gives two copies each (with one left over).
It can be a gotcha, to have the two types of division sharing the same name
(unlike +, which is rarely confusing, since
the two types of “addition” are different enough from each other).
Consider:
-
2/3
-
2.0/3.0
-
2/3.0
-
2.0/3
The problem becomes more pernicious when it's part of a larger expression:
-
2 / 3 * 100. What they hey?
Well, python evaluates left-to-right,
so the intermediate 2/3 returns 0 as we've already seen, and 0*100 = 0.
-
100 * 2 / 3 Can you explain this, going left-to-right?
-
100 * (2/3) Can you explain this?
We already mentioned that python follows the convention PEMDAS (or, “my dear aunt sally”).
More formally:
When evaluating an expression, python has the following “order of operations”:
- Do things inside parentheses first (including arguments to function calls)
- Going left-to-right, do any multiplication/divisions2.
- Finally, go left-to-right and do any addition/subtraction (including string-concatenation)
hint:If it helps human-readability, add parentheses (as well as spaces) to your code,
even if they aren't strictly necessary for python's order-of-operations.
Ideally, a programmer can read your code-expression and not even have to think/wonder for an instant,
whether the order-of-operations is doing anything unexpected.
What is the result from the following? Explain it to your neighbor:
-
(2/3) * 100.0
-
2.0/3.0 * 100
Converting between int and float
Sometimes you want to convert an int to a float,
and you can't just add a trailing “.0”.
Consider the ratio of letters in the word "Virginia Polytechnic Institute" (30) to the letters in "R.U." (4):
we want 30.0/4.0, not 30/4:
len("Virginia Polytechnic Institute") / len("R.U.")
The solution is: call a function whose task is to convert integers to floats.
This function is named “float”:
e.g. float(3) = 3.0.
By the way, there is also a function that converts floats to ints. Can you guess its name?
What result does it answer, if you give it 3.9? -2.8?
- TODO:
Write an expression that computes the ratio of
letters in
"The Town of Our Lady the Queen of Angels of the Porciúncula River"
to the letters of "L.A.".
(Your answer should be a fraction (float).)
- TODO:
Consider the eleven-character string "abcdefghijk".
What is the index of 'a'?
Of 'e'?
Of 'k'?
What is the index of the letter that is (most nearly) 2/3 of the way through the string?
Write an expression which computes how far
which letter is (closest to) 2/3 of the way
through the string "abcdefghijk".
(Your answer should involve only 2, 3, "abcdefghijk",
*, parentheses, and calling functions.)
Challenge:
What is the index of the character that is 2/3 of the way through?
Can you even use substring from yesterday,
to pull out that one character?
1
I guess technically, + gives a slightly-different answer
for 2.0 + 3.0 vs. 2+3, too. ↩
2
The remainder operator (“%”) is a form of division.
↩
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs