Summary of Expression TypesΒΆ
Expression | Arithmetic meaning |
1 + 2 | Addition, the result is 3 |
3 * 4 | Multiplication, the result is 12 |
1 / 3 | Integer division, the result is 0 in older Python environments, but 0.333333333333 in Python 3 |
2.0 / 4.0 | Division, the result is 0.5, since you are using decimal numbers in the calculation |
1 // 3 | Floor division, the result is 0 in Python 3 |
2 % 3 | Modulo (remainder), the result is 2 |
-1 | Negation, the result is -1 |
-
csp-3-3-1: What is the result of
- 0
- If the two values are both integers (whole numbers) you will normally get an integer (whole number) result in older Python environments. But, this book is using Python 3 so you get a decimal result.
- 1
- This would be correct if the result was rounded up before the values after the decimal point were thrown away, but it does not do this.
- 0.75
- While this isn't the what older Pyton development environments would return, in this book we are using Python 3 so it returns a decimal result.
- 0.25
- This would be correct if it was
1 / 4
,1.0 / 4
, or1 / 4.0
3 / 4
?
-
csp-3-3-2: What is the result of
- 0
- This would be correct if it was
18 % 2
, but what is the remainder of 18 divided by 5? - 1
- This would be correct if it was
18 % 17
, since 17 goes into 18 one time and the remainder is 18 - 17 = 1. - 2
- What is the highest multiple of 5 that is less than or equal to 18? The remainder is 18 - that number.
- 3
- The reminder is 3 since 5 goes into 18 three times (15) and 18 - 15 = 3.
18 % 5
?
-
csp-3-3-3: What is the result of
- 0
- This would be correct if it was
6 % 2
. - 1
- This would be correct if it was some odd number divided by 2, but it is not.
- 2
- 6 goes into 2 zero times with 2 left over.
- 6
- If you have a smaller number divided by a larger number the remainder is always the smaller number.
2 % 6
?