2.1. Type Int

2.1.1. Type Definition

  • In Python 3 there is not maximal int value

  • Python 3 dynamically extends int, when it's too big

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

Listing 2.1. int Type Definition
data = 30                   # 30
data = -30                  # -30
million = 1000000           # 1000000
million = 1_000_000         # 1000000

2.1.2. Type Casting

  • int() - converts argument to int

  • int() - does not round numbers

Listing 2.2. int() converts argument to int
int(10)                     # 10
int(10.0)                   # 10
int(10.9)                   # 10
int(13.37)                  # 13
int(-13.37)                 # -13

int('1')                    # 1
int('-1')                   # -1
int('1_000_000')            # 1000000
int('13.37')                # ValueError: invalid literal for int() with base 10: '1.23'
int('-13.37')               # ValueError: invalid literal for int() with base 10: '-1.23'

2.1.3. Type Checking

  • type() - Returns type of an argument

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

2.1.4. Assignments

2.1.4.1. Example

English
  1. File size is one megabyte

  2. Calculate size in bits

  3. Calculate size in megabits

Polish
  1. Wielkość pliku to jeden megabajt

  2. Oblicz wielkość w bitach

  3. Oblicz wielkość w megabitach

Solution
BIT = 1
KILOBIT = 1024 * BIT
MEGABIT = 1024 * KILOBIT

BYTE = 8 * BIT
KILOBYTE = 1024 * BYTE
MEGABYTE = 1024 * KILOBYTE

size = 1 * MEGABYTE

print(f'1 MB in bit = {size / BIT} bits')
print(f'1 MB in Mb = {size / MEGABIT} Mb')
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

Hint
  • 1 Kb = 1024 b

  • 1 Mb = 1024 Kb

  • 1 B = 8 b

  • 1 KB = 1024 B

  • 1 MB = 1024 KB

2.1.4.2. Int Calculation

English
  1. Calculate how many seconds is five minutes

  2. Calculate how many seconds is one hour

  3. Calculate how many seconds is work day (8 hours)

  4. Calculate how many hours is work month (22 work days)

  5. Calculate how many minutes is work week (40 hours)

Polish
  1. Oblicz ile sekund to pięć minut

  2. Oblicz ile sekund to jedna godzina

  3. Oblicz ile sekund to dzień pracy (8 godzin)

  4. Oblicz ile godzin to miesiąc pracy (22 dni pracy)

  5. Oblicz ile minut to tydzień pracy (40 godzin)

The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

Hint
  • 1 h = 60 min

  • 1 min = 60 s

2.1.4.3. Int Ratio

  • Complexity level: medium

  • Lines of code to write: 10 lines

  • Estimated time of completion: 5 min

  • Solution: solution/type_int_ratio.py

English
  1. Having internet connection with speed up to 100 Mb/s

  2. How long will take to download 100 MB?

Polish
  1. Mając łącze internetowe do 100 Mb/s

  2. Ile zajmie ściągnięcie pliku 100 MB?

The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

Hint
  • 1 Kb = 1024 b

  • 1 Mb = 1024 Kb

  • 1 B = 8 b

  • 1 KB = 1024 B

  • 1 MB = 1024 KB

2.1.4.4. Int Temperature

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 Kelvins and Celsius degrees has average temperatures at surface [dACSICINTA19]:

    • Lunar day: 180 °C

    • Lunar night: 93 K

    • Mars average: −63 °C

    • Mars highest: 20 °C

    • Mars lowest: 120 K

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 Kelwinów, a ile stopni Celsiusza wynoszą średnie temperatury powierzchni [dACSICINTA19]:

    • Księżyca w dzień: 180 °C

    • Księżyca w nocy: 93 K

    • Mars średnia: −63 °C

    • Mars najwyższa: 20 °C

    • Mars najniższa: 120 K

The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Print formatting

  • Mathematical operations