Simple arithmetic Flashcards

1
Q

Symbol for ‘exponent/to the power of’

A

**

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Symbol for ‘floor division’

A

//

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Order of operations in Python

A

BODMAS: Brackets, power Of (exponent), Division & Multiplication, addition, subtraction

Multiplication & division are ranked equally in precedence and therefor are evaluated left to right.

Exponents are ‘right-associative’ (ie top down, or right to left): eg,
CORRECT: 2 ** 2 ** 3 = 2 ** (2 ** 3) = 2 ** 8 = 256
NOT: 2 ** 2 ** 3 != (2 ** 2) ** 3 = 4 ** 3 = 64

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the use of ‘_’ (underscore) in simple arithmetic in the Python interactive interpreter?

A
Recalls the previous calculated value, eg:
>> 2 ** 3
8
>> _ + 3
11
Since _ will recall 8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain floor division.

A

Floor division rounds towards zero:
Positive division rounds ‘down’, eg 10//3 = 3
Negative division rounds ‘up’, eg 12//-5 = -2

Also called integer division as it only returns a number of type Int even if the calculation used floating point, eg:
11.0//4 = 2 (where 2 is type Int, 2.0 type Float)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Symbol for ‘modulo’

A

%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain %

A

Returns the remainder of a division, eg:
10 % 3 = 3 r 1 = 1
-12 % 5 = -2 r -2 = -2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The modulo operator always yields a result with the same sign as its [first/second] operand (or [???])

A

The modulo operator always yields a result with the same sign as its SECOND operand (or ZERO)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give the answer to the follow arithmetic in Python 2x vs Python 3x:
x = 3/4

A

Python 2: 0 (truncated floor division, now represented by ‘//’ operator in Python 3)
Python 3: 0.75 (true division - conversion of operands to floating point)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give an example of how you can ‘break’ Python’s simple arithmetic.

A

Divide by 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly