4.1. Numeric Int
Represents an integer
Could be both signed and unsigned
Default
intsize is 64 bitPython automatically extends
intwhen need bigger number
Example:
>>> value = 1
>>> temperature = 21
>>> distance = 1000
4.1.1. Unsigned
Unsigned integers are always non-negative
>>> data = 1
>>> data = 0
4.1.2. Signed
Signed integers can be positive or negative
Positive integers can be written with
+sign, but it is optional
>>> data = -1
>>> data = +1
>>> 1 == +1
True
4.1.3. Underscores
Use
_for thousand separatorYou can use
_for easier read especially with big numbers
You can use _ for easier read especially with big numbers:
>>> million = 1000000
>>> million = 1_000_000
>>> million = 10_00_000
The underscore character _ will not change the value of the number.
Python will simply ignore it. So it could be used for better readability:
>>> million = 1_000_000
>>> print(million)
1000000
4.1.4. Conversion
Builtin function
int()converts argument tointIt is not rounding
Works with strings if they have numbers and
+,-,_(underscore)
Builtin function int() converts argument to int:
>>> int(1.0)
1
>>>
>>> int(-1.0)
-1
>>> int('1')
1
>>>
>>> int('-1')
-1
Builtin function int() fails when in argument there are parameters
other than a digit, + or - sign and _:
>>> int('1.0')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '1.0'
4.1.5. Rounding
Builtin function
int()does not round numbers - only converts tointUse
round()for numbers rounding
Builtin function int() does not round numbers, it just cuts off decimals:
>>> int(1.11)
1
>>>
>>> int(1.99)
1
Builtin function round() does that:
>>> round(1.11)
1
>>>
>>> round(1.99)
2
It works according to the standard rounding rules, so it rounds to the nearest integer:
>>> round(0.4999)
0
>>>
>>> round(0.5001)
1
If the fractional part is exactly 0.5, it rounds to the nearest even integer:
>>> round(0.5000)
0
>>>
>>> round(1.5000)
2
>>>
>>> round(2.5000)
2
>>>
>>> round(3.5000)
4
4.1.6. Recap
Represents an integer
Could be signed or unsigned
Default
intsize is 64 bitPython automatically extends
intwhen need bigger numberBuiltin function
int()converts argument toint(does not round)Builtin function
round()rounds numbers to the nearest integer
Unsigned:
>>> data = 1
>>> data = 0
Signed:
>>> data = +1
>>> data = -1
Underscores:
>>> x = 1000000
>>> x = 1_000_000
>>> x = 10_00_000
Conversion:
>>> int(1.0)
1
>>>
>>> int('1')
1
Rounding:
>>> round(1.11)
1
>>>
>>> round(1.99)
2
4.1.7. Use Case - 1
>>> SECOND = 1
>>> MINUTE = 60 * SECOND
>>> HOUR = 60 * MINUTE
>>> DAY = 24 * HOUR
>>>
>>>
>>> duration = 123456 * SECOND
>>>
>>> duration // DAY
1
>>> duration // HOUR
34
>>> duration // MINUTE
2057
>>> duration // SECOND
123456
4.1.8. Use Case - 2
>>> m = 1
>>> km = 1000 * m
>>> mi = 1652 * m
>>>
>>>
>>> distance = 123*mi
>>>
>>> distance // m
203196
>>> distance // km
203
4.1.9. Use Case - 3
>>> PLN = 1
>>> EUR = 4.71 * PLN # 2023-03-19 17:00 GMT+1
>>> USD = 4.41 * PLN # 2023-03-19 17:00 GMT+1
>>>
>>>
>>> price = 100*PLN
>>>
>>> round(price // EUR)
21
>>> round(price // USD)
22
4.1.10. Assignments
# %% About
# - Name: Numeric Int Sub
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. Alice was born in year 1988
# 2. Assume it's year 2026 now
# 3. Define variable `result` with age of Alice
# 4. Run doctests - all must succeed
# %% Polish
# 1. Alice urodziła się w roku 1988
# 2. Przyjmij, że obecnie jest rok 2026
# 3. Zdefiniuj zmienną `result` z wiekiem Alice
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# 37
# %% Hints
# - `-` - sub operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is int, \
'Variable `result` has an invalid type; expected: `int`.'
>>> assert result == 38, \
'Variable `result` has invalid value. Check your calculation.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: int
# %% Data
BIRTHDATE = 1988
TODAY = 2026
# %% Result
result = ...
# %% About
# - Name: Numeric Int Sub
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. How many full years is in the period of 3022 days
# 2. Define variable `result` with the solution
# 3. Assume year is 365 days
# 4. Run doctests - all must succeed
# %% Polish
# 1. Ile pełnych lat mieści się w okresie 3022 dni?
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Przyjmij, że rok to 365 dni
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# 8
# %% Hints
# - `//` - floordiv operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is int, \
'Variable `result` has an invalid type; expected: `int`.'
>>> assert result == 8, \
'Variable `result` has invalid value. Check your calculation.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: int
# %% Data
PERIOD = 3022
YEAR = 365
# %% Result
result = ...
# %% About
# - Name: Numeric Int Floordiv
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% 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
# %% English
# 1. Define `result1` with number of full weeks in a month
# 2. Define `result2` with number of full weeks in a year
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result1` z liczbą pełnych tygodni w miesiącu
# 2. Zdefiniuj `result2` z liczbą pełnych tygodni w roku
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result1
# 4
#
# >>> result2
# 52
# %% Hints
# - `//` - floordiv operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is int, \
'Variable `result1` has an invalid type; expected: `int`.'
>>> assert result1 == 4, \
'Variable `result1` has invalid value. Check your calculation.'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert type(result2) is int, \
'Variable `result2` has an invalid type; expected: `int`.'
>>> assert result2 == 52, \
'Variable `result2` has invalid value. Check your calculation.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result1: int
result2: int
# %% Data
DAY = 1
YEAR = 365*DAY
MONTH = 30*DAY
WEEK = 7*DAY
# %% Result
result1 = ...
result2 = ...
# %% About
# - Name: Numeric Int Floordiv
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. Image has dimensions 256x128 pixels
# 2. Screen resolution is 1920x1080 pixels
# 3. Define `result` with number of images you can display in one row
# 4. Run doctests - all must succeed
# %% Polish
# 1. Obrazek ma wymiary 256x128 pikseli
# 2. Rozdzielczość ekranu to 1920x1080 pikseli
# 3. Zdefiniuj `result` z liczbą obrazków, które można wyświetlić w jednym rzędzie
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# 7
# %% Hints
# - `//` - floordiv operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is int, \
'Variable `result` has an invalid type; expected: `int`.'
>>> assert result == 7, \
'Variable `result` has invalid value. Check your calculation.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: int
# %% Data
px = 1
IMG_WIDTH = 256*px
IMG_HEIGHT = 128*px
SCREEN_WIDTH = 1920*px
SCREEN_HEIGHT = 1080*px
# %% Result
result = ...
# %% About
# - Name: Numeric Int Constants
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% 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
# %% English
# 1. Define "constant" SECOND with value of 1 second
# 2. Define "constant" MINUTE with value of 1 minute (60 seconds)
# 3. Define "constant" HOUR with value of 1 hour (60 minutes)
# 4. Define "constant" DAY with value of 1 day (24 hours)
# 5. Define "constant" WEEK with value of 1 week (7 days)
# 6. All variables must be in seconds
# 7. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj "stałą" SECOND z wartością dla 1 sekundy
# 2. Zdefiniuj "stałą" MINUTE z wartością dla 1 minuty (60 sekund)
# 3. Zdefiniuj "stałą" HOUR z wartością dla 1 godziny (60 minut)
# 4. Zdefiniuj "stałą" DAY z wartością dla 1 dzień (24 godziny)
# 5. Zdefiniuj "stałą" WEEK z wartością dla 1 tydzień (7 dni)
# 6. Wszystkie zmienne muszą być wyrażone w sekundach
# 7. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> SECOND
# 1
#
# >>> MINUTE
# 60
#
# >>> HOUR
# 3600
#
# >>> DAY
# 86400
#
# >>> WEEK
# 604800
# %% Hints
# - `*` - mul operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert SECOND is not Ellipsis, \
'Variable `SECOND` has an invalid value; assign result of your program to it.'
>>> assert MINUTE is not Ellipsis, \
'Variable `MINUTE` has an invalid value; assign result of your program to it.'
>>> assert HOUR is not Ellipsis, \
'Variable `HOUR` has an invalid value; assign result of your program to it.'
>>> assert DAY is not Ellipsis, \
'Variable `DAY` has an invalid value; assign result of your program to it.'
>>> assert WEEK is not Ellipsis, \
'Variable `WEEK` has an invalid value; assign result of your program to it.'
>>> assert type(SECOND) is int, \
'Variable `SECOND` has an invalid type; expected: `int`.'
>>> assert type(MINUTE) is int, \
'Variable `MINUTE` has an invalid type; expected: `int`.'
>>> assert type(HOUR) is int, \
'Variable `HOUR` has an invalid type; expected: `int`.'
>>> assert type(DAY) is int, \
'Variable `DAY` has an invalid type; expected: `int`.'
>>> assert type(WEEK) is int, \
'Variable `WEEK` has an invalid type; expected: `int`.'
>>> assert SECOND == 1, \
'Variable `SECOND` has invalid value. Check your calculation.'
>>> assert MINUTE == 60, \
'Variable `MINUTE` has invalid value. Check your calculation.'
>>> assert HOUR == 3600, \
'Variable `HOUR` has invalid value. Check your calculation.'
>>> assert DAY == 86400, \
'Variable `DAY` has invalid value. Check your calculation.'
>>> assert WEEK == 604800, \
'Variable `WEEK` has invalid value. Check your calculation.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
SECOND: int
MINUTE: int
HOUR: int
DAY: int
WEEK: int
# %% Data
# %% Result
SECOND = ...
MINUTE = ...
HOUR = ...
DAY = ...
WEEK = ...
# %% About
# - Name: Numeric Int Duration
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% 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
# %% English
# 1. Define variable `result1` with value: 69 seconds
# 2. Define variable `result2` with value: 13 minutes 37 seconds
# 3. Define variable `result3` with value: 2 hours 56 minutes
# 4. Define variable `result4` with value: 1 day 2 hours 8 minutes
# 5. Define variable `result5` with value: 3 weeks 1 day 3 hours 3 minutes 7 seconds
# 6. All values must be in seconds
# 7. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result1` z wartością: 69 sekund
# 2. Zdefiniuj zmienną `result2` z wartością: 13 minut 37 sekund
# 3. Zdefiniuj zmienną `result3` z wartością: 2 godziny 56 minut
# 4. Zdefiniuj zmienną `result4` z wartością: 1 dzień 2 godziny 8 minut
# 5. Zdefiniuj zmienną `result5` z wartością: 3 tygodnie 1 dzień 3 godziny 3 minuty 7 sekund
# 6. Wszystkie zmienne muszą być wyrażone w sekundach
# 7. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result1
# 69
#
# >>> result2
# 817
#
# >>> result3
# 10560
#
# >>> result4
# 94080
#
# >>> result5
# 1911787
# %% Hints
# - `*` - mul operator
# - `+` - add
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'
>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'
>>> assert 'result5' in globals(), \
'Variable `result5` is not defined; assign result of your program to it.'
>>> assert result5 is not Ellipsis, \
'Variable `result5` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is int, \
'Variable `result1` has an invalid type; expected: `int`.'
>>> assert type(result2) is int, \
'Variable `result2` has an invalid type; expected: `int`.'
>>> assert type(result3) is int, \
'Variable `result3` has an invalid type; expected: `int`.'
>>> assert type(result4) is int, \
'Variable `result4` has an invalid type; expected: `int`.'
>>> assert type(result5) is int, \
'Variable `result5` has an invalid type; expected: `int`.'
>>> assert result1 == 69, \
'Variable `result1` has invalid value. Check your calculation.'
>>> assert result2 == 817, \
'Variable `result2` has invalid value. Check your calculation.'
>>> assert result3 == 10560, \
'Variable `result3` has invalid value. Check your calculation.'
>>> assert result4 == 94080, \
'Variable `result4` has invalid value. Check your calculation.'
>>> assert result5 == 1911787, \
'Variable `result5` has invalid value. Check your calculation.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result1: int
result2: int
result3: int
result4: int
result5: int
# %% Data
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...
result5 = ...
# %% About
# - Name: Numeric Int Variables
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3
# %% 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
# %% English
# 1. Define variable `workbreak` with value: 5 minutes
# 2. Define variable `workday` with value: 8 workbreak
# 3. Define variable `workweek` with value: 5 workday
# 4. Define variable `workmonth` with value: 4 workweek
# 5. All variables must be in seconds
# 6. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `workbreak` z wartością: 5 minut
# 2. Zdefiniuj zmienną `workday` z wartością: 8 workbreak
# 3. Zdefiniuj zmienną `workweek` z wartością: 5 workday
# 4. Zdefiniuj zmienną `workmonth` z wartością: 4 workweek
# 5. Wszystkie zmienne muszą być wyrażone w sekundach
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> workbreak
# 300
#
# >>> workday
# 2400
#
# >>> workweek
# 12000
#
# >>> workmonth
# 48000
# %% Hints
# - `*` - mul operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert workbreak is not Ellipsis, \
'Variable `workbreak` has an invalid value; assign result of your program to it.'
>>> assert workday is not Ellipsis, \
'Variable `workday` has an invalid value; assign result of your program to it.'
>>> assert workweek is not Ellipsis, \
'Variable `workweek` has an invalid value; assign result of your program to it.'
>>> assert workmonth is not Ellipsis, \
'Variable `workmonth` has an invalid value; assign result of your program to it.'
>>> assert type(workbreak) is int, \
'Variable `workbreak` has an invalid type; expected: `int`.'
>>> assert type(workday) is int, \
'Variable `workday` has an invalid type; expected: `int`.'
>>> assert type(workweek) is int, \
'Variable `workweek` has an invalid type; expected: `int`.'
>>> assert type(workmonth) is int, \
'Variable `workmonth` has an invalid type; expected: `int`.'
>>> pprint(workbreak)
300
>>> pprint(workday)
2400
>>> pprint(workweek)
12000
>>> pprint(workmonth)
48000
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
workbreak: int
workday: int
workweek: int
workmonth: int
# %% Data
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
# %% Result
workbreak = ...
workday = ...
workweek = ...
workmonth = ...
# %% About
# - Name: Numeric Int Conversion
# - Difficulty: easy
# - Lines: 6
# - Minutes: 3
# %% 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
# %% English
# 1. Define variable `day` with number of seconds in 24 hours
# 2. Define variable `week` with number of minutes in 7 days
# 3. Define variable `month` with number of hours in 31 days
# 4. Define variable `workday` with number of seconds in 8 hours
# 5. Define variable `workweek` with number of minutes in 5 work days
# 6. Define variable `workmonth` with number of hours in 22 work days
# 7. Use floordiv (`//`) operator
# 8. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `day` z liczbą sekund w ciągu 24 godzin
# 2. Zdefiniuj zmienną `week` z liczbą minut w ciągu 7 dni
# 3. Zdefiniuj zmienną `month` z liczbą godzin w ciągu 31 dni
# 4. Zdefiniuj zmienną `workday` z liczbą sekund w ciągu 8 godzin
# 5. Zdefiniuj zmienną `workweek` z liczbą minut w ciągu 5 dni pracy
# 6. Zdefiniuj zmienną `workmonth` z liczbą godzin w ciągu 22 dni pracy
# 7. Użyj operatora floordiv (`//`)
# 8. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> day
# 86400
#
# >>> week
# 10080
#
# >>> month
# 744
#
# >>> workday
# 28800
#
# >>> workweek
# 2400
#
# >>> workmonth
# 176
# %% Hints
# - `*` - mul operator
# - `//` - floordiv
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert day is not Ellipsis, \
'Variable `day` has an invalid value; assign result of your program to it.'
>>> assert week is not Ellipsis, \
'Variable `week` has an invalid value; assign result of your program to it.'
>>> assert month is not Ellipsis, \
'Variable `month` has an invalid value; assign result of your program to it.'
>>> assert workday is not Ellipsis, \
'Variable `workday` has an invalid value; assign result of your program to it.'
>>> assert workweek is not Ellipsis, \
'Variable `workweek` has an invalid value; assign result of your program to it.'
>>> assert workmonth is not Ellipsis, \
'Variable `workmonth` has an invalid value; assign result of your program to it.'
>>> assert type(day) is int, \
'Variable `day` has an invalid type; expected: `int`.'
>>> assert type(week) is int, \
'Variable `week` has an invalid type; expected: `int`.'
>>> assert type(month) is int, \
'Variable `month` has an invalid type; expected: `int`.'
>>> assert type(workday) is int, \
'Variable `workday` has an invalid type; expected: `int`.'
>>> assert type(workweek) is int, \
'Variable `workweek` has an invalid type; expected: `int`.'
>>> assert type(workmonth) is int, \
'Variable `workmonth` has an invalid type; expected: `int`.'
>>> assert day == 86400, \
'Variable `day` has invalid value. Check your calculation.'
>>> assert week == 10080, \
'Variable `week` has invalid value. Check your calculation.'
>>> assert month == 744, \
'Variable `month` has invalid value. Check your calculation.'
>>> assert workday == 28800, \
'Variable `workday` has invalid value. Check your calculation.'
>>> assert workweek == 2400, \
'Variable `workweek` has invalid value. Check your calculation.'
>>> assert workmonth == 176, \
'Variable `workmonth` has invalid value. Check your calculation.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
day: int
week: int
month: int
workday: int
workweek: int
workmonth: int
# %% Data
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
# %% Result
day = ...
week = ...
month = ...
workday = ...
workweek = ...
workmonth = ...
# %% About
# - Name: Numeric Int Bits
# - Difficulty: medium
# - Lines: 3
# - Minutes: 3
# %% 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
# %% English
# 1. File size `SIZE` is 1337 megabits [Mb]
# 2. Define variable `size_bits` with `SIZE` in bits [b]
# 3. Define variable `size_kilobits` with `SIZE` in kilobits [kb]
# 4. Define variable `size_megabits` with `SIZE` in megabits [Mb]
# 5. Use floordiv (`//`) operator
# 6. Run doctests - all must succeed
# %% Polish
# 1. Wielkość pliku `SIZE` to 1337 megabits [Mb]
# 2. Zdefiniuj zmienną `size_bits` z rozmiarem `SIZE` w bitach [b]
# 3. Zdefiniuj zmienną `size_kilobits` z rozmiarem `SIZE` w kilobitach [kb]
# 4. Zdefiniuj zmienną `size_megabits` z rozmiarem `SIZE` w megabitach [Mb]
# 5. Użyj operatora floordiv (`//`)
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> size_bits
# 1_401_946_112
#
# >>> size_kilobits
# 1_369_088
#
# >>> size_megabits
# 1337
# %% Hints
# - `//` - floordiv
# - 1 kb = 1024 b
# - 1 Mb = 1024 Kb
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert size_bits is not Ellipsis, \
'Variable `size_bits` has an invalid value; assign result of your program to it.'
>>> assert size_kilobits is not Ellipsis, \
'Variable `size_kilobits` has an invalid value; assign result of your program to it.'
>>> assert size_megabits is not Ellipsis, \
'Variable `size_megabits` has an invalid value; assign result of your program to it.'
>>> assert type(size_bits) is int, \
'Variable `size_bits` has an invalid type; expected: `int`.'
>>> assert type(size_kilobits) is int, \
'Variable `size_kilobits` has an invalid type; expected: `int`.'
>>> assert type(size_megabits) is int, \
'Variable `size_megabits` has an invalid type; expected: `int`.'
>>> assert size_bits == 1_401_946_112, \
'Invalid value for `size_bits`. Check you calculation'
>>> assert size_kilobits == 1_369_088, \
'Invalid value for `size_kilobits`. Check you calculation'
>>> assert size_megabits == 1337, \
'Invalid value for `size_megabits`. Check you calculation'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
size_bits: int
size_kilobits: int
size_megabits: int
# %% Data
b = 1
kb = 1024 * b
Mb = 1024 * kb
SIZE = 1337 * Mb
# %% Result
size_bits = ...
size_kilobits = ...
size_megabits = ...
# %% About
# - Name: Numeric Int Bytes
# - Difficulty: medium
# - Lines: 2
# - Minutes: 2
# %% 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
# %% English
# 1. File size `SIZE` is 100 megabytes [MB]
# 2. Define variable `size_kilobytes` with size `SIZE` in kilobytes [kB]
# 3. Define variable `size_megabits` with size `SIZE` in megabits [Mb]
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wielkość pliku `SIZE` to 100 megabajtów [MB]
# 2. Zdefiniuj zmienną `size_kilobytes` z rozmiarem `SIZE` w kilobajtach [kB]
# 3. Zdefiniuj zmienną `size_megabits` z rozmiarem `SIZE` w megabitach [Mb]
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> size_kilobytes
# 102_400
#
# >>> size_megabits
# 800
# %% Hints
# - `//` - floordiv
# - 1 Kb = 1024 b
# - 1 Mb = 1024 Kb
# - 1 B = 8 b
# - 1 KB = 1024 B
# - 1 MB = 1024 KB
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert size_kilobytes is not Ellipsis, \
'Variable `size_kilobytes` has an invalid value; assign result of your program to it.'
>>> assert size_megabits is not Ellipsis, \
'Variable `size_megabits` has an invalid value; assign result of your program to it.'
>>> assert type(size_kilobytes) is int, \
'Variable `size_kilobytes` has an invalid type; expected: `int`.'
>>> assert type(size_megabits) is int, \
'Variable `size_megabits` has an invalid type; expected: `int`.'
>>> assert size_kilobytes == 102_400, \
'Invalid value for `size_kilobytes`. Check you calculation'
>>> assert size_megabits == 800, \
'Invalid value for `size_megabits`. Check you calculation'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
size_kilobytes: int
size_megabits: int
# %% Data
b = 1
kb = 1024 * b
Mb = 1024 * kb
B = 8 * b
kB = 1024 * B
MB = 1024 * kB
SIZE = 100 * MB
# %% Result
size_kilobytes = ...
size_megabits = ...
# %% About
# - Name: Numeric Int Bandwidth
# - Difficulty: medium
# - Lines: 3
# - Minutes: 3
# %% 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
# %% English
# 1. Having internet connection with speed (`bandwidth`) 100 Mb/s
# 2. How long will take to download (`size`) 100 MB?
# 3. To calculate time divide file size by bandwidth and type cast to `int`
# 4. Define variable `duration` with the solution in seconds
# 5. Use floordiv (`//`) operator
# 6. Run doctests - all must succeed
# %% Polish
# 1. Mając łącze internetowe (`bandwidth`) 100 Mb/s
# 2. Ile zajmie ściągnięcie pliku (`size`) 100 MB?
# 3. Aby wyliczyć czas podziel wielkość pliku przez prędkość i rzutuj na `int`
# 4. Zdefiniuj zmienną `duration` z rozwiązaniem w sekundach
# 5. Użyj operatora floordiv (`//`)
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> bandwidth
# 104_857_600
#
# >>> size
# 838_860_800
#
# >>> duration
# 8
# %% Hints
# - `*` - mul operator
# - `//` - floordiv
# - 1 Kb = 1024 b
# - 1 Mb = 1024 Kb
# - 1 B = 8 b
# - 1 KB = 1024 B
# - 1 MB = 1024 KB
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert bandwidth is not Ellipsis, \
'Variable `bandwidth` has an invalid value; assign result of your program to it.'
>>> assert size is not Ellipsis, \
'Variable `size` has an invalid value; assign result of your program to it.'
>>> assert duration is not Ellipsis, \
'Variable `duration` has an invalid value; assign result of your program to it.'
>>> assert type(bandwidth) is int, \
'Variable `bandwidth` has an invalid type; expected: `int`.'
>>> assert type(size) is int, \
'Variable `size` has an invalid type; expected: `int`.'
>>> assert type(duration) is int, \
'Variable `duration` has an invalid type; expected: `int`.'
>>> assert bandwidth == 104_857_600, \
'Variable `bandwidth` has an invalid value; expected: `104_857_600`.'
>>> assert size == 838_860_800, \
'Variable `size` has an invalid value; expected: `838_860_800`.'
>>> assert duration == 8, \
'Variable `duration` has an invalid value; expected: `8`.'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
bandwidth: int
size: int
duration: int
# %% Data
SECOND = 1
b = 1
kb = 1024 * b
Mb = 1024 * kb
B = 8 * b
kB = 1024 * B
MB = 1024 * kB
# %% Result
bandwidth = ...
size = ...
duration = ...