2.5. Syntax Operators
2.5.1. Summary
Arithmetic Operators
Incremental Operators
Comparison Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
2.5.2. Arithmetic Operators
+
- Addition-
- Subtraction*
- Multiplication**
- Power and Root/
- Division/
- True Division (changes type to float)//
- Floor division (preserving data type)%
- Modulo division (reminder)a ** b
-b
power of the numbera
a ** (1/b)
-b
-th root of the numbera
Addition:
>>> 10 + 2
12
Subtraction:
>>> 10 - 2
8
Multiplication:
>>> 10 * 2
20
Division:
>>> 10 / 2
5.0
Power:
>>> 10 ** 2
100
>>>
>>> 2 ** -1
0.5
Root:
>>> 4 ** (1/2)
2.0
>>>
>>> 4 ** 0.5
2.0
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.5.3. Incremental Operators
+=
- Incremental addition-=
- Incremental subtraction*=
- Incremental multiplication**=
- Incremental power/=
- Incremental true division//=
- Incremental floor division%=
- Incremental modulo divisionx += 1
- Incremental addition by 1x -= 1
- Incremental subtraction by 1x++
and++x
are not supported in PythonUse of
+=
and-=
is commonOthers are used rarely
In Python for each operator there is also an increment version of it.
However, most of a time only +=
and -=
are used. Others uses are rare.
Incremental Addition:
>>> x = 10
>>> x += 1
>>>
>>> print(x)
11
Incremental Subtraction:
>>> x = 10
>>> x -= 1
>>>
>>> print(x)
9
In other programming languages you may find postfix and prefix increment notation. There is no such thing in Python.
>>> x = 1
>>> x++
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> x = 1
>>> ++x
1
2.5.4. Comparison Operators
==
- Equals!=
- Not Equals>
- Greater Than>=
- Greater or Equal Then<
- Less Than<=
- Less or Equal Then
Greater Than:
>>> 1 > 2
False
Greater or Equal Then:
>>> 1 >= 2
False
Less Than:
>>> 1 < 2
True
Less or Equal Then:
>>> 1 <= 2
True
Equals:
>>> 1 == 2
False
Not Equals:
>>> 1 != 2
True
2.5.5. Equals vs. Double Equals
=
- Equals - assigns value to the variable==
- Double equals - compares values
Equals (=
) assigns value to the variable:
>>> x = 1
>>> x
1
Double equals (==
) compares value of the variable:
>>> x == 1
True
2.5.6. Values and Variables
Compare values
Compare variable and value
Compare variables
Compare values:
>>> 1 > 2
False
Compare variable and value:
>>> x = 1
>>> x > 2
False
Compare variables:
>>> x = 1
>>> y = 2
>>> x > y
False
2.5.7. Operator Precedence
Python uses mathematical operator precedence
Use brackets to change precedence
>>> 1+2 * 3
7
>>> (1+2) * 3
9
2.5.8. Use Case - 1
Parity Check with Modulo Division
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.5.9. Recap
Arithmetic Operators:
+
,-
,*
,**
,/
,//
,%
Comparison Operators:
>
,>=
,<
,<=
,==
,!=
Incremental Operators:
+=
,-=
,*=
,**=
,/=
,//=
,%=
You can use operators with values, variables or variables and values
Python uses mathematical operator precedence, you can change it with brackets
Modulo
%
can be used for parity check=
- Equals - assigns value to the variable==
- Double equals - compares values
Arithmetic Operators
>>> result = 10 + 2
>>> result = 10 - 2
>>> result = 10 * 2
>>> result = 10 / 2
>>> result = 10 ** 2
>>> result = 2 ** -1
>>> result = 4 ** (1/2)
>>> result = 4 ** 0.5
Comparison Operators:
>>> result = 1 > 2
>>> result = 1 >= 2
>>> result = 1 < 2
>>> result = 1 <= 2
>>> result = 1 == 2
>>> result = 1 != 2
Incremental Operators:
>>> x += 1
>>> x -= 1
>>> x *= 1
>>> x /= 1
>>> x //= 1
>>> x %= 1
2.5.10. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Addition
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define variable:
# - `result_a` with value 1 plus 2
# - `result_b` with value 1 minus 2
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną:
# - `result_a` z wartością 1 plus 2
# - `result_b` z wartością 1 minus 2
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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'
>>> from pprint import pprint
>>> 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 = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Multiplication
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define variable:
# - `result_a` with the value of 2 times 3
# - `result_b` with the value of 2 to the power of 3
# - `result_c` with the value of 2 under the cube root (cube=3)
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną:
# - `result_a` z wartością 2 razy 3
# - `result_b` z wartością 2 do potęgi 3
# - `result_c` z wartością 2 pod pierwiastkiem 3 stopnia
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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 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 type(result_c) is float, \
'Variable `result_c` has invalid type, should be float'
>>> 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'
>>> assert result_c == 1.2599210498948732, \
'Variable `result_c` has invalid value, should be 1.2599210498948732'
>>> from pprint import pprint
>>> pprint(result_a)
6
>>> pprint(result_b)
8
>>> pprint(result_c)
1.2599210498948732
"""
# Value of 2 times 3
# type: int
result_a = ...
# Value of 2 to the power of 3
# type: int
result_b = ...
# Value of 2 under the cube root (cube=3)
# type: int
result_c = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Division
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Define variable:
# - `result_a` with value 10 divided by 8 (truediv)
# - `result_b` with value 10 divided by 8 (floordiv)
# - `result_c` with a reminder of 10 divided by 8 (modulo)
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną:
# - `result_a` z wartością 10 podzielone przez 8 (truediv)
# - `result_b` z wartością 10 podzielone przez 8 (floordiv)
# - `result_c` z resztą z dzielenia 10 przez 8 (modulo)
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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'
>>> from pprint import pprint
>>> 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 = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Even/Odd
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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'
>>> from pprint import pprint
>>> pprint(result)
1
"""
NUMBER = 4
# Result of checking if `NUMBER` is even
# type: bool
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Increment
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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 == 11, \
'Variable `result` has invalid value, should be 11'
>>> from pprint import pprint
>>> pprint(result)
11
"""
result = 10
# Increment result by 1
# Use `+=` operator
# type: int
...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Decrement
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Decrement variable `result` by 1
# 2. Use `-=` operator
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zmniejsz zmienną `result` o 1
# 2. Użyj operatora `-=`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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 == 9, \
'Variable `result` has invalid value, should be 9'
>>> from pprint import pprint
>>> pprint(result)
9
"""
result = 10
# Decrement variable `result` by 1
# Use `-=` operator
# type: int
...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Comparison
# - Difficulty: easy
# - Lines: 4
# - Minutes: 2
# %% English
# 1. Define variable:
# - `result_a` with result of checking if 1 is greater than 2
# - `result_b` with result of checking if 1 is greater or equal than 2
# - `result_c` with result of checking if 1 is less than 2
# - `result_d` with result of checking if 1 is less or equal than 2
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną:
# - `result_a` z wynikiem sprawdzenia, czy 1 jest większe od 2
# - `result_b` z wynikiem sprawdzenia, czy 1 jest większe lub równe 2
# - `result_c` z wynikiem sprawdzenia, czy 1 jest mniejsze od 2
# - `result_d` z wynikiem sprawdzenia, czy 1 jest mniejsze lub równe od 2
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'
>>> assert result_a == False, \
'Variable `result_a` has invalid value, should be False'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'
>>> assert result_b == False, \
'Variable `result_b` has invalid value, should be False'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable `result_c`'
>>> assert type(result_c) is bool, \
'Variable `result_c` has invalid type, should be bool'
>>> assert result_c == True, \
'Variable `result_c` has invalid value, should be True'
>>> assert result_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert type(result_d) is bool, \
'Variable `result_d` has invalid type, should be bool'
>>> assert result_d == True, \
'Variable `result_d` has invalid value, should be True'
>>> from pprint import pprint
>>> pprint(result_a)
False
>>> pprint(result_b)
False
>>> pprint(result_c)
True
>>> pprint(result_d)
True
"""
# Define `result_a` with result of checking if 1 is greater than 2
# type: bool
result_a = ...
# Define `result_b` with result of checking if 1 is greater or equal than 2
# type: bool
result_b = ...
# Define `result_c` with result of checking if 1 is less than 2
# type: bool
result_c = ...
# Define `result_d` with result of checking if 1 is less or equal than 2
# type: bool
result_d = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Syntax Operators Equality
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define variable:
# - `result_a` with result of checking if 1 is equal to 2
# - `result_b` with result of checking if 1 is not equal 2
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną:
# - `result_a` z wynikiem sprawdzenia, czy 1 jest równe 2
# - `result_b` z wynikiem sprawdzenia, czy 1 jest różne od 2
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'
>>> assert result_a == False, \
'Variable `result_a` has invalid value, should be False'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'
>>> assert result_b == True, \
'Variable `result_b` has invalid value, should be True'
>>> from pprint import pprint
>>> pprint(result_a)
False
>>> pprint(result_b)
True
"""
# `result_a` with result of checking if 1 is equal to 2
# type: bool
result_a = ...
# `result_b` with result of checking if 1 is not equal 2
# type: bool
result_b = ...