2.1. Type Int

2.1.1. Definition

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

>>> data = 1337
>>>
>>> print(data)
1337
>>> data = +1337
>>>
>>> print(data)
1337
>>> data = -1337
>>>
>>> print(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.1.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.1.3. Binary

  • Base 2

  • Allowed: 0, 1

  • Prefix: 0b...

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

2.1.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.1.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.1.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.1.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.1.8. Assignments

Code 2.1. Solution
"""
* Assignment: Type Int Add
* 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°K = -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. Compare result with "Tests" section (see below)

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°K = -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. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

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

Tests:
    >>> type(mars_max)
    <class 'int'>
    >>> type(mars_min)
    <class 'int'>
    >>> type(mars_avg)
    <class 'int'>
    >>> mars_max
    293
    >>> mars_min
    120
    >>> mars_avg
    210
"""


# Given
Kelvin = 273


Code 2.2. Solution
"""
* Assignment: Type Int Sub
* 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. Compare result with "Tests" section (see below)

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. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

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

Tests:
    >>> type(moon_day)
    <class 'int'>
    >>> type(moon_night)
    <class 'int'>
    >>> moon_day
    180
    >>> moon_night
    -180
"""


# Given
Kelvin = 273


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

English:
    1. Calculate altitude in meters:
        a. Armstrong Line: 18 km
        b. Stratosphere: 20 km
        c. USAF Space Line: 80 km
        d. IAF Space Line: 100 km
    2. Compare result with "Tests" section (see below)

Polish:
    1. Oblicz wysokości w metrach:
        a. Linia Armstronga: 18 km
        b. Stratosfera: 20 km
        c. Granica kosmosu wg. USAF: 80 km
        d. Granica kosmosu wg. IAF 100 km
    2. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Hints:
    * 1 km = 1000 m

Tests:
    >>> armstrong_line
    18000
    >>> stratosphere
    20000
    >>> usaf_space
    80000
    >>> iaf_space
    100000
"""


# Given
m = 1
km = 1000 * m


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

English:
    1. Calculate altitude in kilometers:
        a. Kármán Line Earth: 100000 m
        b. Kármán Line Mars: 80000 m
        c. Kármán Line Venus: 250000 m
    2. In Calculations use truediv (`//`)
    3. Compare result with "Tests" section (see below)

Polish:
    1. Oblicz wysokości w kilometrach:
        a. Linia Kármána Ziemia: 100000 m
        b. Linia Kármána Mars: 80000 m
        c. Linia Kármána Wenus: 250000 m
    2. W obliczeniach użyj truediv (`//`)
    3. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Hints:
    * 1 km = 1000 m

Tests:
    >>> type(karman_line_earth)
    <class 'int'>
    >>> type(karman_line_mars)
    <class 'int'>
    >>> type(karman_line_venus)
    <class 'int'>
    >>> karman_line_earth
    100
    >>> karman_line_mars
    80
    >>> karman_line_venus
    250
"""


# Given
m = 1
km = 1000 * m


Code 2.5. Solution
"""
* Assignment: Type Int Time
* 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 truediv (`//`)
    8. Compare result with "Tests" section (see below)

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 truediv (`//`)
    8. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> type(day)
    <class 'int'>
    >>> type(week)
    <class 'int'>
    >>> type(month)
    <class 'int'>
    >>> type(workday)
    <class 'int'>
    >>> type(workweek)
    <class 'int'>
    >>> type(workmonth)
    <class 'int'>
    >>> day
    86400
    >>> week
    10080
    >>> month
    744
    >>> workday
    28800
    >>> workweek
    2400
    >>> workmonth
    176
"""


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


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

English:
    1. File size is 1.337 megabit
    2. Calculate size in bits
    3. Calculate size in kilobits
    4. Compare result with "Tests" section (see below)

Polish:
    1. Wielkość pliku to 1.337 megabit
    2. Oblicz wielkość w bitach
    3. Oblicz wielkość w kilobitach
    4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

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

Tests:
    >>> type(kb)
    <class 'int'>
    >>> type(Mb)
    <class 'int'>
    >>> type(size)
    <class 'int'>
    >>> b
    1
    >>> kb
    1024
    >>> Mb
    1048576
    >>> size // b
    1401946112
    >>> size // kb
    1369088
    >>> size // Mb
    1337
"""


# Given
b = 1


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

English:
    1. File size is 100 megabytes
    2. Calculate size in megabits
    3. Compare result with "Tests" section (see below)

Polish:
    1. Wielkość pliku to 100 megabajtów
    2. Oblicz wielkość w megabitach
    3. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

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

Tests:
    >>> type(kb)
    <class 'int'>
    >>> type(Mb)
    <class 'int'>
    >>> type(kB)
    <class 'int'>
    >>> type(MB)
    <class 'int'>
    >>> type(size)
    <class 'int'>
    >>> size // MB
    100
    >>> size // Mb
    800
"""


# Given
b = 1
B = 8 * b


Code 2.8. Solution
"""
* Assignment: Type Int Bandwidth
* 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. Note, that all values must be `int` (type cast if needed)
    3. In Calculations use truediv (`//`)
    4. Compare result with "Tests" section (see below)

Polish:
    1. Mając łącze internetowe 100 Mb/s
    2. Ile zajmie ściągnięcie pliku 100 MB?
    3. Zwróć uwagę, że wszystkie wartości mają być `int` (rzutuj typ jeżeli potrzeba)
    3. W obliczeniach użyj truediv (`//`)
    4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

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

Tests:
    >>> type(kb)
    <class 'int'>
    >>> type(Mb)
    <class 'int'>
    >>> type(kB)
    <class 'int'>
    >>> type(MB)
    <class 'int'>
    >>> type(size)
    <class 'int'>
    >>> type(speed)
    <class 'int'>
    >>> type(result)
    <class 'int'>
    >>> result // SECOND
    8
"""


# Given
SECOND = 1
b = 1
B = 8 * b