2.2. Numeric Int

2.2.1. Definition

Python 3 dynamically extends int when it's too big, hence there is no maximal or minimal int value:

>>> data = 1337
>>> data = +1337
>>> data = -1337

You can use _ for easier read especially with big numbers:

>>> million = 1000000
>>>
>>> print(million)
1000000
>>> million = 1_000_000
>>>
>>> print(million)
1000000

2.2.2. Type Casting

Builtin function int() converts argument to int:

>>> int(1)
1
>>> int(+1)
1
>>> int(-1)
-1
>>> int(1.337)
1
>>> int(+1.1337)
1
>>> int(-1.337)
-1
>>> int('1')
1
>>> int('+1')
1
>>> int('-1')
-1

Builtin function int() does not round numbers:

>>> int(1.001)
1
>>> int(1.999)
1
>>> int('1_000_000')
1000000

Builtin function int() fails when in argument there are parameters other than a digit, + or - sign and _

>>> int('1.337')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '13.37'
>>> int('+1.337')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '+13.37'
>>> int('-1.337')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '-13.37'
>>> int('1,337')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '13,37'
>>> int('+1,337')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '+13,37'
>>> int('-1,337')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '-13,37'

2.2.3. Binary

  • Base 2

  • Allowed: 0, 1

  • Prefix: 0b...

>>> int('100', base=2)
4
>>> int('0b1000101', base=2)
69

2.2.4. Octal

  • Base 8

  • Allowed: 0, 1, 2, 3, 4, 5, 6, 7

  • Prefix: 0o...

>>> int('100', base=8)
64
>>> int('0o105', base=8)
69
>>> int('0o754', base=8)
492

2.2.5. Decimal

  • Base 10

  • Allowed: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

>>> int('100', base=10)
100
>>> int('69', base=10)
69

2.2.6. Hexadecimal

  • Base 16

  • Allowed: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, F

  • Prefix: 0x...

>>> int('100', base=16)
256
>>> int('0x45', base=16)
69
>>> int('0x69', base=16)
105
>>> int('0x3C', base=16)
60

2.2.7. Type Checking

  • type() - Returns type of an argument

>>> type(1)
<class 'int'>
>>> type(+1)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(0)
<class 'int'>
>>> type(+0)
<class 'int'>
>>> type(-0)
<class 'int'>

2.2.8. Assignments

Code 2.1. Solution
"""
* Assignment: Type Int Add
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. One Kelvin is equal to 1 Celsius degree (1K = 1°C)
    2. Zero Celsius degrees is equal to 273.15 Kelvins
    3. For calculation use round number 273 (0°C = -273K)
    4. How many Kelvins has average temperatures at surface [1]:
        a. Mars highest: 20 °C
        b. Mars lowest: -153 °C
        c. Mars average: −63 °C
    5. Run doctests - all must succeed

Polish:
    1. Jeden Kelwin to jeden stopień Celsiusza (1K = 1°C)
    2. Zero stopni Celsiusza to 273.15 Kelwiny
    3. W zadaniu przyjmij równe 273°C (0°C = -273K)
    4. Ile Kelwinów wynoszą średnie temperatury powierzchni [1]:
        a. Mars najwyższa: 20 °C
        b. Mars najniższa: -153 °C
        c. Mars średnia: −63 °C
    5. Uruchom doctesty - wszystkie muszą się powieść

References:
    [1] Centro de Astrobiología (CSIC-INTA).
        Rover Environmental Monitoring Station, Mars Science Laboratory (NASA).
        Year: 2019.
        Retrieved: 2019-08-06.
        URL: http://cab.inta-csic.es/rems/marsweather.html

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

    >>> assert mars_max is not Ellipsis, 'Assignment solution must be in `mars_max` instead of ... (Ellipsis)'
    >>> assert mars_min is not Ellipsis, 'Assignment solution must be in `mars_min` instead of ... (Ellipsis)'
    >>> assert mars_min is not Ellipsis, 'Assignment solution must be in `mars_min` instead of ... (Ellipsis)'
    >>> assert type(mars_max) is int, 'Variable `mars_max` has invalid type, should be int'
    >>> assert type(mars_min) is int, 'Variable `mars_min` has invalid type, should be int'
    >>> assert type(mars_min) is int, 'Variable `mars_avg` has invalid type, should be int'

    >>> mars_max
    293
    >>> mars_min
    120
    >>> mars_avg
    210
"""

Celsius = 1
Kelvin = 273

mars_max = ...  # int: 20 Celsius in Kelvin
mars_min = ...  # int: -153 Celsius in Kelvin
mars_avg = ...  # int: -63 Celsius in Kelvin

Code 2.2. Solution
"""
* Assignment: Type Int Sub
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
    1. One Kelvin is equal to 1 Celsius degree (1K = 1°C)
    2. Zero Kelvin (absolute) is equal to -273.15 Celsius degrees
    3. For calculation use round number -273 (0K = -273°C)
    4. How many Celsius degrees has average temperatures at surface [1]:
        a. Lunar day: 453 K
        b. Lunar night: 93 K
    5. Run doctests - all must succeed

Polish:
    1. Jeden Kelwin to jeden stopień Celsiusza (1K = 1°C)
    2. Zero Kelwina (bezwzględne) to -273.15 stopni Celsiusza
    3. W zadaniu przyjmij równe -273°C (0K = -273°C)
    4. Ile stopni Celsiusza wynoszą średnie temperatury powierzchni [1]:
        a. Księżyca w dzień: 453 K
        b. Księżyca w nocy: 93 K
    5. Uruchom doctesty - wszystkie muszą się powieść

References:
    [1] Centro de Astrobiología (CSIC-INTA).
        Rover Environmental Monitoring Station, Mars Science Laboratory (NASA).
        Year: 2019.
        Retrieved: 2019-08-06.
        URL: http://cab.inta-csic.es/rems/marsweather.html

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

    >>> assert moon_day is not Ellipsis, 'Assignment solution must be in `moon_day` instead of ... (Ellipsis)'
    >>> assert moon_night is not Ellipsis, 'Assignment solution must be in `moon_night` instead of ... (Ellipsis)'
    >>> assert type(moon_day) is int, 'Variable `moon_day` has invalid type, should be int'
    >>> assert type(moon_night) is int, 'Variable `moon_night` has invalid type, should be int'

    >>> moon_day
    180
    >>> moon_night
    -180
"""

Celsius = 273
Kelvin = 1

moon_day = ...  # int: 453 Kelvins in Celsius
moon_night = ...  # int: 93 Kelvins in Celsius

Code 2.3. Solution
"""
* Assignment: Type Int Mul
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
    1. Calculate altitude in meters:
        a. Armstrong Limit: 19 km
        b. Stratosphere: 20 km
        c. USAF Space Line: 80 km
        d. IAF Space Line: 100 km
    2. Run doctests - all must succeed

Polish:
    1. Oblicz wysokości w metrach:
        a. Linia Armstronga: 19 km
        b. Stratosfera: 20 km
        c. Granica kosmosu wg. USAF: 80 km
        d. Granica kosmosu wg. IAF 100 km
    2. Uruchom doctesty - wszystkie muszą się powieść

References:
    * USAF - United States Air Force
    * IAF - International Astronautical Federation
    * Kármán line (100 km) - boundary between Earth's atmosphere and outer space
    * Armstrong limit (19 km) - altitude above which atmospheric pressure is
      sufficiently low that water boils at the normal temperature of the human body

Hints:
    * 1 km = 1000 m

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

    >>> assert armstrong_limit is not Ellipsis, 'Assignment solution must be in `armstrong_limit` instead of ... (Ellipsis)'
    >>> assert stratosphere is not Ellipsis, 'Assignment solution must be in `stratosphere` instead of ... (Ellipsis)'
    >>> assert usaf_space is not Ellipsis, 'Assignment solution must be in `usaf_space` instead of ... (Ellipsis)'
    >>> assert iaf_space is not Ellipsis, 'Assignment solution must be in `iaf_space` instead of ... (Ellipsis)'
    >>> assert type(armstrong_limit) is int, 'Variable `armstrong_limit` has invalid type, should be int'
    >>> assert type(stratosphere) is int, 'Variable `stratosphere` has invalid type, should be int'
    >>> assert type(usaf_space) is int, 'Variable `usaf_space` has invalid type, should be int'
    >>> assert type(iaf_space) is int, 'Variable `iaf_space` has invalid type, should be int'

    >>> armstrong_limit
    19000
    >>> stratosphere
    20000
    >>> usaf_space
    80000
    >>> iaf_space
    100000
"""

m = 1
km = 1000 * m

armstrong_limit = ...  # int: 19 kilometers in meters
stratosphere = ...  # int: 20 kilometers in meters
usaf_space = ...  # int: 80 kilometers in meters
iaf_space = ...  # int: 100 kilometers in meters

Code 2.4. Solution
"""
* Assignment: Type Int Bits
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
    1. Calculate altitude in kilometers:
        a. Kármán Line Earth: 100_000 m
        b. Kármán Line Mars: 80_000 m
        c. Kármán Line Venus: 250_000 m
    2. In Calculations use floordiv (`//`)
    3. Run doctests - all must succeed

Polish:
    1. Oblicz wysokości w kilometrach:
        a. Linia Kármána Ziemia: 100_000 m
        b. Linia Kármána Mars: 80_000 m
        c. Linia Kármána Wenus: 250_000 m
    2. W obliczeniach użyj floordiv (`//`)
    3. Uruchom doctesty - wszystkie muszą się powieść

References:
    * Kármán line (100 km) - boundary between planets's atmosphere and outer space

Hints:
    * 1 km = 1000 m

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

    >>> assert karman_line_earth is not Ellipsis, 'Assignment solution must be in `karman_line_earth` instead of ... (Ellipsis)'
    >>> assert karman_line_mars is not Ellipsis, 'Assignment solution must be in `karman_line_mars` instead of ... (Ellipsis)'
    >>> assert karman_line_venus is not Ellipsis, 'Assignment solution must be in `karman_line_venus` instead of ... (Ellipsis)'
    >>> assert type(karman_line_earth) is int, 'Variable `karman_line_earth` has invalid type, should be int'
    >>> assert type(karman_line_mars) is int, 'Variable `karman_line_mars` has invalid type, should be int'
    >>> assert type(karman_line_venus) is int, 'Variable `karman_line_venus` has invalid type, should be int'

    >>> karman_line_earth
    100
    >>> karman_line_mars
    80
    >>> karman_line_venus
    250
"""

m = 1
km = 1000 * m

karman_line_earth = ...  # int: 100_000 meters in km
karman_line_mars = ...  # int: 80_000 meters in km
karman_line_venus = ...  # int: 250_000 meters in km

Code 2.5. Solution
"""
* Assignment: Type Int Time
* Required: yes
* Complexity: easy
* Lines of code: 12 lines
* Time: 8 min

English:
    1. Calculate how many seconds is one day (24 hours)
    2. Calculate how many minutes is one week (7 days)
    3. Calculate how many hours is in one month (31 days)
    4. Calculate how many seconds is work day (8 hours)
    5. Calculate how many minutes is work week (5 work days)
    6. Calculate how many hours is work month (22 work days)
    7. In Calculations use floordiv (`//`)
    8. Run doctests - all must succeed

Polish:
    1. Oblicz ile sekund to jedna doba (24 godziny)
    2. Oblicz ile minut to jeden tydzień (7 dni)
    3. Oblicz ile godzin to jeden miesiąc (31 dni)
    4. Oblicz ile sekund to dzień pracy (8 godzin)
    5. Oblicz ile minut to tydzień pracy (5 dni pracy)
    6. Oblicz ile godzin to miesiąc pracy (22 dni pracy)
    7. W obliczeniach użyj floordiv (`//`)
    8. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert day is not Ellipsis, 'Assignment solution must be in `day` instead of ... (Ellipsis)'
    >>> assert week is not Ellipsis, 'Assignment solution must be in `week` instead of ... (Ellipsis)'
    >>> assert month is not Ellipsis, 'Assignment solution must be in `month` instead of ... (Ellipsis)'
    >>> assert workday is not Ellipsis, 'Assignment solution must be in `workday` instead of ... (Ellipsis)'
    >>> assert workweek is not Ellipsis, 'Assignment solution must be in `workweek` instead of ... (Ellipsis)'
    >>> assert workmonth is not Ellipsis, 'Assignment solution must be in `workmonth` instead of ... (Ellipsis)'
    >>> 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'

    >>> day
    86400
    >>> week
    10080
    >>> month
    744
    >>> workday
    28800
    >>> workweek
    2400
    >>> workmonth
    176
"""

SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR

day = ...  # int: 1 day in seconds
week = ...  # int: 7 days in minutes
month = ...  # int: 31 days in hours
workday = ...  # int: 8 hours in seconds
workweek = ...  # int: 5 workdays in minutes
workmonth = ...  # int: 22 workdays in hours

Code 2.6. Solution
"""
* Assignment: Type Int Bits
* Required: no
* Complexity: medium
* Lines of code: 4 lines
* Time: 3 min

English:
    1. File size is 1337 megabits [Mb]
    2. Calculate size in bits [b]
    3. Calculate size in kilobits [kb]
    4. In Calculations use floordiv (`//`)
    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. W obliczeniach użyj floordiv (`//`)
    5. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * 1 kb = 1024 b
    * 1 Mb = 1024 Kb

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

    >>> assert size_b is not Ellipsis, 'Assignment solution must be in `size_b` instead of ... (Ellipsis)'
    >>> assert size_kb is not Ellipsis, 'Assignment solution must be in `size_kb` instead of ... (Ellipsis)'
    >>> assert size_Mb is not Ellipsis, 'Assignment solution must be in `size_Mb` instead of ... (Ellipsis)'
    >>> assert type(size_b) is int, 'Variable `size_b` has invalid type, should be int'
    >>> assert type(size_kb) is int, 'Variable `size_kb` has invalid type, should be int'
    >>> assert type(size_Mb) is int, 'Variable `size_Mb` has invalid type, should be int'

    >>> size_b
    1401946112
    >>> size_kb
    1369088
    >>> size_Mb
    1337
"""

b = 1
kb = 1024 * b
Mb = 1024 * kb

size = ...  # int: 1337 megabits
size_b = ...  # int: size in bytes
size_kb = ...  # int: size in kilobytes
size_Mb = ...  # int: size in megabytes

Code 2.7. Solution
"""
* Assignment: Type Int Bytes
* Required: no
* Complexity: easy
* Lines of code: 7 lines
* Time: 3 min

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:
    * 1 Kb = 1024 b
    * 1 Mb = 1024 Kb
    * 1 B = 8 b
    * 1 KB = 1024 B
    * 1 MB = 1024 KB

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

    >>> assert size is not Ellipsis, 'Assignment solution must be in `size` instead of ... (Ellipsis)'
    >>> assert size_kB is not Ellipsis, 'Assignment solution must be in `size_kB` instead of ... (Ellipsis)'
    >>> assert size_Mb is not Ellipsis, 'Assignment solution must be in `size_Mb` instead of ... (Ellipsis)'
    >>> assert type(size) is int, 'Variable `size` has invalid type, should be int'
    >>> assert type(size_kB) is int, 'Variable `size_kB` has invalid type, should be int'
    >>> assert type(size_Mb) is int, 'Variable `size_Mb` has invalid type, should be int'

    >>> size_kB
    102400
    >>> size_Mb
    800
"""

b = 1
kb = 1024 * b
Mb = 1024 * kb

B = 8 * b
kB = 1024 * B
MB = 1024 * kB

size = ...  # int: 100 Megabytes
size_kB = ...  # int: size in kilobytes
size_Mb = ...  # int: size in megabits

Code 2.8. Solution
"""
* Assignment: Type Int Bandwidth
* Required: no
* Complexity: easy
* Lines of code: 10 lines
* Time: 3 min

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
    3. Note, that all values must be `int` (type cast if needed)
    3. In Calculations use floordiv (`//`)
    4. 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ękość
    3. Zwróć uwagę, że wszystkie wartości mają być `int` (rzutuj typ jeżeli potrzeba)
    3. W obliczeniach użyj floordiv (`//`)
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * 1 Kb = 1024 b
    * 1 Mb = 1024 Kb
    * 1 B = 8 b
    * 1 KB = 1024 B
    * 1 MB = 1024 KB

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

    >>> assert bandwidth is not Ellipsis, 'Assignment solution must be in `bandwidth` instead of ... (Ellipsis)'
    >>> assert size is not Ellipsis, 'Assignment solution must be in `size` instead of ... (Ellipsis)'
    >>> assert duration is not Ellipsis, 'Assignment solution must be in `duration` instead of ... (Ellipsis)'
    >>> 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'

    >>> duration
    8
"""

SECOND = 1

b = 1
kb = 1024 * b
Mb = 1024 * kb

B = 8 * b
kB = 1024 * B
MB = 1024 * kB

bandwidth = ...  # int: 100 megabits per second
size = ...  # int: 100 megabytes
duration = ...  # int: size by bandwidth in seconds