3.1. Numeric Int
Represents an integer
Could be both signed and unsigned
Default
int
size is 64 bitPython automatically extends
int
when need bigger number
Example:
value = 1
temperature = 21
distance = 1000
3.1.1. Syntax
Signed and Unsigned
data = 1
data = +1
data = -1
3.1.2. Thousands Separator
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
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
3.1.3. Conversion
Builtin function
int()
converts argument toint
It 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'
3.1.4. Rounding
Builtin function
int()
does not round numbers - only converts toint
Use
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
3.1.5. 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
3.1.6. Use Case - 2
m = 1
km = 1000 * m
mi = 1652 * m
distance = 123*mi
distance // m
203196
distance // km
203
3.1.7. 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
3.1.8. 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 2025 now
# 3. How old is Alice?
# 4. Run doctests - all must succeed
# %% Polish
# 1. Alice urodziła się w roku 1988
# 2. Przyjmij, że obecnie jest roku 2025
# 3. Ile lat ma Alice?
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `-` - sub operator
# %% Doctests
"""
>>> 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 == 37, \
'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 -v myfile.py`
# %% Imports
# %% Types
result: int
# %% Data
BIRTHDATE = 1988
TODAY = 2025
# %% 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. Given period of 3022 days
# 2. How many full years is that?
# 3. Assume year is 365 days
# 4. Run doctests - all must succeed
# %% Polish
# 1. Dany jest okres 3022 dni
# 2. Ile to pełnych lat?
# 3. Przyjmij, że rok to 365 dni
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `//` - floordiv operator
# %% Doctests
"""
>>> 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 == 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 -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 `result_a` with number of full weeks in a month
# 2. Define `result_b` with number of full weeks in a year
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result_a` z liczbą pełnych tygodniu w miesiącu
# 2. Zdefiniuj `result_b` z liczbą pełnych tygodni w roku
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `//` - floordiv operator
# %% Doctests
"""
>>> 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 int, \
'Variable `result_a` has invalid type, should be int'
>>> assert result_a == 4, \
'Variable `result_a` has invalid value. Check your calculation.'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is int, \
'Variable `result_b` has invalid type, should be int'
>>> assert result_b == 52, \
'Variable `result_b` 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 -v myfile.py`
# %% Imports
# %% Types
result_a: int
result_b: int
# %% Data
DAY = 1
YEAR = 365*DAY
MONTH = 30*DAY
WEEK = 7*DAY
# %% Result
result_a = ...
result_b = ...
# %% 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 800x600 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ść
# %% Hints
# - `//` - floordiv operator
# %% Doctests
"""
>>> 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 == 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 -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ść
# %% Hints
# - `*` - mul operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert SECOND is not Ellipsis, \
'Assign your result to variable `SECOND`'
>>> assert MINUTE is not Ellipsis, \
'Assign your result to variable `MINUTE`'
>>> assert HOUR is not Ellipsis, \
'Assign your result to variable `HOUR`'
>>> assert DAY is not Ellipsis, \
'Assign your result to variable `DAY`'
>>> assert WEEK is not Ellipsis, \
'Assign your result to variable `WEEK`'
>>> assert type(SECOND) is int, \
'Variable `SECOND` has invalid type, should be int'
>>> assert type(MINUTE) is int, \
'Variable `MINUTE` has invalid type, should be int'
>>> assert type(HOUR) is int, \
'Variable `HOUR` has invalid type, should be int'
>>> assert type(DAY) is int, \
'Variable `DAY` has invalid type, should be int'
>>> assert type(WEEK) is int, \
'Variable `WEEK` has invalid type, should be 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 -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 `result_a` with: 69 seconds
# 2. Define `result_b` with: 13 minutes 37 seconds
# 3. Define `result_c` with: 2 hours 56 minutes
# 4. Define `result_d` with: 1 day 2 hours 8 minutes
# 5. Define `result_e` with: 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 `result_a` z: 69 sekund
# 2. Zdefiniuj `result_b` z: 13 minut 37 sekund
# 3. Zdefiniuj `result_c` z: 2 godziny 56 minut
# 4. Zdefiniuj `result_d` z: 1 dzień 2 godziny 8 minut
# 5. Zdefiniuj `result_e` z: 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ść
# %% Hints
# - `*` - mul operator
# - `+` - add
# %% Doctests
"""
>>> 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 result_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert result_e is not Ellipsis, \
'Assign your result to variable `result_e`'
>>> 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 int, \
'Variable `result_c` has invalid type, should be int'
>>> assert type(result_d) is int, \
'Variable `result_d` has invalid type, should be int'
>>> assert type(result_e) is int, \
'Variable `result_e` has invalid type, should be int'
>>> assert result_a == 69, \
'Variable `result_a` has invalid value. Check your calculation.'
>>> assert result_b == 817, \
'Variable `result_b` has invalid value. Check your calculation.'
>>> assert result_c == 10560, \
'Variable `result_c` has invalid value. Check your calculation.'
>>> assert result_d == 94080, \
'Variable `result_d` has invalid value. Check your calculation.'
>>> assert result_e == 1911787, \
'Variable `result_e` 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 -v myfile.py`
# %% Imports
# %% Types
result_a: int
result_b: int
result_c: int
result_d: int
result_e: int
# %% Data
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
# %% Result
result_a = ...
result_b = ...
result_c = ...
result_d = ...
result_e = ...
# %% 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 `workbreak` with: 5 minutes
# 2. Define `workday` with: 8 workbreak
# 3. Define `workweek` with: 5 workday
# 4. Define `workmonth` with: 4 workweek
# 5. All variables must be in seconds
# 6. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `workbreak` z: 5 minut
# 2. Zdefiniuj `workday` z: 8 workbreak
# 3. Zdefiniuj `workweek` z: 5 workday
# 4. Zdefiniuj `workmonth` z: 4 workweek
# 5. Wszystkie zmienne muszą być wyrażone w sekundach
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `*` - mul operator
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert workbreak is not Ellipsis, \
'Assign your result to variable `workbreak`'
>>> assert workday is not Ellipsis, \
'Assign your result to variable `workday`'
>>> assert workweek is not Ellipsis, \
'Assign your result to variable `workweek`'
>>> assert workmonth is not Ellipsis, \
'Assign your result to variable `workmonth`'
>>> assert type(workbreak) is int, \
'Variable `workbreak` has invalid type, should be int'
>>> assert type(workday) is int, \
'Variable `workday` has invalid type, should be int'
>>> assert type(workweek) is int, \
'Variable `workweek` has invalid type, should be int'
>>> assert type(workmonth) is int, \
'Variable `workmonth` has invalid type, should be 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 -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 `day` with: number of seconds in 24 hours
# 2. Define `week` with: number of minutes in 7 days
# 3. Define `month` with: number of hours in 31 days
# 4. Define `workday` with: number of seconds in 8 hours
# 5. Define `workweek` with: number of minutes in 5 work days
# 6. Define `workmonth` with: number of hours in 22 work days
# 7. Use floordiv (`//`) operator
# 8. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `day` z: liczbą sekund w ciągu 24 godzin
# 2. Zdefiniuj `week` z: liczbą minut w ciągu 7 dni
# 3. Zdefiniuj `month` z: liczbą godzin w ciągu 31 dni
# 4. Zdefiniuj `workday` z: liczbą sekund w ciągu 8 godzin
# 5. Zdefiniuj `workweek` z: liczbą minut w ciągu 5 dni pracy
# 6. Zdefiniuj `workmonth` z: liczbą godzin w ciągu 22 dni pracy
# 7. Użyj operatora floordiv (`//`)
# 8. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `*` - mul operator
# - `//` - floordiv
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert day is not Ellipsis, \
'Assign your result to variable `day`'
>>> assert week is not Ellipsis, \
'Assign your result to variable `week`'
>>> assert month is not Ellipsis, \
'Assign your result to variable `month`'
>>> assert workday is not Ellipsis, \
'Assign your result to variable `workday`'
>>> assert workweek is not Ellipsis, \
'Assign your result to variable `workweek`'
>>> assert workmonth is not Ellipsis, \
'Assign your result to variable `workmonth`'
>>> assert type(day) is int, \
'Variable `day` has invalid type, should be int'
>>> assert type(week) is int, \
'Variable `week` has invalid type, should be int'
>>> assert type(month) is int, \
'Variable `month` has invalid type, should be int'
>>> assert type(workday) is int, \
'Variable `workday` has invalid type, should be int'
>>> assert type(workweek) is int, \
'Variable `workweek` has invalid type, should be int'
>>> assert type(workmonth) is int, \
'Variable `workmonth` has invalid type, should be 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 -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 is 1337 megabits [Mb]
# 2. Calculate size in bits [b]
# 3. Calculate size in kilobits [kb]
# 4. Use floordiv (`//`) operator
# 5. Run doctests - all must succeed
# %% Polish
# 1. Wielkość pliku to 1337 megabits [Mb]
# 2. Oblicz wielkość w bitach [b]
# 3. Oblicz wielkość w kilobitach [kb]
# 4. Użyj operatora floordiv (`//`)
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `//` - floordiv
# - 1 kb = 1024 b
# - 1 Mb = 1024 Kb
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert size_bits is not Ellipsis, \
'Assign your result to variable `size_bits`'
>>> assert size_kilobits is not Ellipsis, \
'Assign your result to variable `size_kilobits`'
>>> assert size_megabits is not Ellipsis, \
'Assign your result to variable `size_megabits`'
>>> assert type(size_bits) is int, \
'Variable `size_bits` has invalid type, should be int'
>>> assert type(size_kilobits) is int, \
'Variable `size_kilobits` has invalid type, should be int'
>>> assert type(size_megabits) is int, \
'Variable `size_megabits` has invalid type, should be 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 -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 is 100 megabytes
# 2. Calculate size in kilobytes
# 2. Calculate size in megabits
# 3. Run doctests - all must succeed
# %% Polish
# 1. Wielkość pliku to 100 megabajtów
# 2. Oblicz wielkość w kilobajtach
# 2. Oblicz wielkość w megabitach
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% 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 3.9+ required'
>>> assert size_kilobytes is not Ellipsis, \
'Assign your result to variable `size_kilobytes`'
>>> assert size_megabits is not Ellipsis, \
'Assign your result to variable `size_megabits`'
>>> assert type(size_kilobytes) is int, \
'Variable `size_kilobytes` has invalid type, should be int'
>>> assert type(size_megabits) is int, \
'Variable `size_megabits` has invalid type, should be 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 -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 100 Mb/s
# 2. How long will take to download 100 MB?
# 3. To calculate time divide file size by speed
# 4. Note, that all values must be `int` (type cast if needed)
# 5. Use floordiv (`//`) operator
# 6. Run doctests - all must succeed
# %% Polish
# 1. Mając łącze internetowe 100 Mb/s
# 2. Ile zajmie ściągnięcie pliku 100 MB?
# 3. Aby wyliczyć czas podziel wielkość pliku przez prędkość
# 4. Zwróć uwagę, że wszystkie wartości mają być `int`
# (rzutuj typ jeżeli potrzeba)
# 5. Użyj operatora floordiv (`//`)
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% 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 3.9+ required'
>>> assert bandwidth is not Ellipsis, \
'Assign your result to variable `bandwidth`'
>>> assert size is not Ellipsis, \
'Assign your result to variable `size`'
>>> assert duration is not Ellipsis, \
'Assign your result to variable `duration`'
>>> assert type(bandwidth) is int, \
'Variable `bandwidth` has invalid type, should be int'
>>> assert type(size) is int, \
'Variable `size` has invalid type, should be int'
>>> assert type(duration) is int, \
'Variable `duration` has invalid type, should be int'
>>> assert bandwidth == 104_857_600, \
'Invalid value for `bandwidth`. Check you calculation'
>>> assert size == 838_860_800, \
'Invalid value for `size`. Check you calculation'
>>> assert duration == 8, \
'Invalid value for `duration`. Check you calculation'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -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 = ...