# 2.8. Syntax Operators

## 2.8.1. Arithmetic Operators

• + - Addition

• - - Subtraction

• * - Multiplication

• / - Division

>>> 10 + 2
12
>>>
>>> 10 - 2
8
>>>
>>> 10 * 2
20
>>>
>>> 10 / 2
5.0

>>> x = 10
>>> y = 2
>>>
>>> x + y
12


## 2.8.2. Power and Root

• a ** b - b power of the number a

• a ** (1/b) - b-th root of the number a

>>> 10 ** 2
100
>>>
>>> 2 ** -1
0.5

>>> 4 ** (1/2)
2.0
>>>
>>> 8 ** (1/3)
2.0

>>> 4 ** 0.5
2.0
>>>
>>> 2 ** 0.5
1.4142135623730951
>>>
>>> 27 ** 0.333
2.9967059728946346


## 2.8.3. Divisions

There are three (and even four if counting divmod) ways of dividing numbers in Python:

• / - True Division (changes type to float)

• // - Floor division (preserving data type)

• % - Modulo division (reminder)

The most common is true division, which changes type to float to preserve mathematical correctness:

>>> 12 / 6
2.0
>>>
>>> 12 / 5
2.4


Note, that the floor division preserves types, so it is more correct in computer science way. However it will produce invalid values from math perspective:

>>> 12 // 6
2
>>>
>>> 12 // 5
2


There is also a modulo division, which is more frequently used than you might think. Modulo division is the reminder from true division:

>>> 12 % 6
0
>>>
>>> 12 % 5
2


## 2.8.4. Parity Check

Modulo division is most frequently used to test if value is even or odd. In such case, you need to modulo divide number by 2 and check the reminder. If the reminder is 0, than the original number was even, if the reminder is false, the original number was odd:

>>> 10 % 2
0
>>>
>>> 11 % 2
1
>>>
>>> 12 % 2
0
>>>
>>> 13 % 2
1

>>> 10 % 2 == 0
True
>>>
>>> 11 % 2 == 0
False
>>>
>>> 12 % 2 == 0
True
>>>
>>> 13 % 2 == 0
False


## 2.8.5. Operator Precedence

• Python uses mathematical operator precedence

>>> 1+2 * 3
7

>>> (1+2) * 3
9


## 2.8.6. Assignments

"""
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
1. Define variable result with
a. result_a with value 1 plus 2
b. result_b with value 1 minus 2
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj zmienną:
a. result_a z wartością 1 plus 2
b. result_b z wartością 1 minus 2
2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable result_a'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable result_b'

>>> assert type(result_a) is int, \
'Variable result_a has invalid type, should be int'
>>> assert type(result_b) is int, \
'Variable result_b has invalid type, should be int'

>>> assert result_a == 3, \
'Variable result_a has invalid value, should be 3'
>>> assert result_b == -1, \
'Variable result_b has invalid value, should be -1'

>>> pprint(result_a)
3
>>> pprint(result_b)
-1
"""

# Value of 1 plus 2
# type: int
result_a = ...

# Value of 1 minus 2
# type: int
result_b = ...


"""
* Assignment: Syntax Operators Mul/Pow
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
1. Define variable:
a. result_a with value 2 times 3
b. result_b with value 2 to the power of 3
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj zmienną:
a. result_a z wartością 2 razy 3
b. result_b z wartością 2 do potęgi 3
2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable result_a'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable result_b'

>>> assert type(result_a) is int, \
'Variable result_a has invalid type, should be int'
>>> assert type(result_b) is int, \
'Variable result_b has invalid type, should be int'

>>> assert result_a == 6, \
'Variable result_a has invalid value, should be 6'
>>> assert result_b == 8, \
'Variable result_b has invalid value, should be 8'

>>> pprint(result_a)
6
>>> pprint(result_b)
8
"""

# Value 2 times 3
# type: int
result_a = ...

# Value 2 to the power of 3
# type: int
result_b = ...


"""
* Assignment: Syntax Operators Division
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
1. Define variable:
a. result_a with value 10 divided by 8 (truediv)
b. result_b with value 10 divided by 8 (floordiv)
c. result_c with a reminder of 10 divided by 8 (modulo)
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj zmienną:
a. result_a z wartością 10 podzielone przez 8 (truediv)
b. result_b z wartością 10 podzielone przez 8 (floordiv)
c. result_c z resztą z dzielenia 10 przez 8 (modulo)
2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable result_a'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable result_b'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable result_c'

>>> assert type(result_a) is float, \
'Variable result_a has invalid type, should be float'
>>> assert type(result_b) is int, \
'Variable result_b has invalid type, should be int'
>>> assert type(result_c) is int, \
'Variable result_c has invalid type, should be int'

>>> assert result_a == 1.25, \
'Variable result_a has invalid value, should be 1.25'
>>> assert result_b == 1, \
'Variable result_b has invalid value, should be 1'
>>> assert result_c == 2, \
'Variable result_c has invalid value, should be 2'

>>> pprint(result_a)
1.25
>>> pprint(result_b)
1
>>> pprint(result_c)
2
"""

# value 10 divided by 8 (truediv)
# type: float
result_a = ...

# value 10 divided by 8 (floordiv)
# type: int
result_b = ...

# reminder of 10 divided by 8 (modulo)
# type: int
result_c = ...


"""
* Assignment: Syntax Operators Increment
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Increment variable result by 1
2. Use += operator
3. Run doctests - all must succeed

Polish:
1. Zwiększ zmienną result o 1
2. Użyj operatora +=
3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> assert result is not Ellipsis, \
'Assign your result to variable result'

>>> assert type(result) is int, \
'Variable result has invalid type, should be int'

>>> assert result == 1, \
'Variable result has invalid value, should be 1'

>>> pprint(result)
1
"""

result = 0

# Increment result by 1
# Use += operator
# type: int
...


"""
* Assignment: Syntax Operators Even
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define variable result with result
of checking if NUMBER is even
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj zmienną result z wynikiem
sprawdzenia czy NUMBER jest parzyste
2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint

>>> assert result is not Ellipsis, \
'Assign your result to variable result'

>>> assert type(result) is bool, \
'Variable result has invalid type, should be bool'

>>> assert result == 1, \
'Variable result has invalid value, should be 1'

>>> pprint(result)
1
"""

NUMBER = 4

# Result of checking if NUMBER is even
# type: bool
result = ...