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 = 1337                 # 1337
data = -1337                # -1337
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(13)                     # 13
int(13.0)                   # 13
int(13.3)                   # 13
int(13.37)                  # 13

int(-13.37)                 # -13

int('1')                    # 1
int('+1')                   # 1
int('-1')                   # -1
int('1_000_000')            # 1000000

int('13.37')                # ValueError: invalid literal for int() with base 10: '13.37'
int('13,37')                # ValueError: invalid literal for int() with base 10: '13,37'
int('-13.37')               # ValueError: invalid literal for int() with base 10: '-13.37'
int('-13,37')               # ValueError: invalid literal for int() with base 10: '-13,37'

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. Type Int Distance

  • Assignment name: Type Int Distance

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 3 lines

  • Estimated time of completion: 5 min

  • Solution: solution/type_int_distance.py

English
  1. Calculate altitude in meters:

    • Armstrong Line: 18 km

    • Stratosphere: 20 km

    • USAF Space Line: 80 km

  2. Calculate altitude in kilometers:

    • Kármán Line Earth: 100000 m

    • Kármán Line Mars: 80000 m

    • Kármán Line Venus: 250000 m

  3. In Calculations use truediv (//)

  4. Compare result with "Output" section (see below)

Polish
  1. Calculate altitude in meters:

    • Linia Armstronga: 18 km

    • Stratosfera: 20 km

    • Granica kosmosu wg. USAF: 80 km

  2. Calculate altitude in kilometers:

    • Linia Kármána Ziemia: 100000 m

    • Linia Kármána Mars: 80000 m

    • Linia Kármána Wenus: 250000 m

  3. W obliczeniach użyj truediv (//)

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
Armstrong Line: 18000 m
Stratosphere: 20000 m
USAF Space: 80000 m
Kármán Line Earth: 100 km
Kármán Line Mars: 80 km
Kármán Line Venus: 250 km
Hints
  • 1 km = 1000 m

2.1.4.2. Type Int Time

  • Assignment name: Type Int Time

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 12 lines

  • Estimated time of completion: 8 min

  • Solution: solution/type_int_time.py

English
  1. Calculate how many seconds is one day

  2. Calculate how many minutes is one day

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

  4. Calculate how many minutes is work week (5 work days)

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

  6. In Calculations use truediv (//)

Polish
  1. Oblicz ile sekund to jedna doba

  2. Oblicz ile minut to je jedna doba

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

  4. Oblicz ile minut to tydzień pracy (5 dni pracy)

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

  6. W obliczeniach użyj truediv (//)

Output
Day: 86400 sec
Day: 1440 min
Work day: 28800 sec
Work week: 2400 min
Work month: 176 h
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

Hints
  • 1 h = 60 min

  • 1 min = 60 s

2.1.4.3. Type Int Bits

  • Assignment name: Type Int Bits

  • Last update: 2020-10-01

  • Complexity level: medium

  • Lines of code to write: 6 lines

  • Estimated time of completion: 3 min

  • Solution: solution/type_int_bits.py

English
  1. File size is 1 megabit

  2. Calculate size in bits

  3. Calculate size in kilobits

  4. In Calculations use truediv (//)

  5. Compare result with "Output" section (see below)

Polish
  1. Wielkość pliku to 1 megabit

  2. Oblicz wielkość w bitach

  3. Oblicz wielkość w kilobitach

  4. W obliczeniach użyj truediv (//)

  5. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
Size: 1048576 b
Size: 1024 kb
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

Hints
  • 1 Kb = 1024 b

  • 1 Mb = 1024 Kb

2.1.4.4. Type Int Bytes

  • Assignment name: Type Int Bytes

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 9 lines

  • Estimated time of completion: 3 min

  • Solution: solution/type_int_bytes.py

English
  1. File size is 1 megabyte

  2. Calculate size in megabits

  3. Print result in megabytes and megabits

  4. Compare result with "Output" section (see below)

Polish
  1. Wielkość pliku to 1 megabajt

  2. Oblicz wielkość w megabitach

  3. Wypisz wielkość w megabajtach oraz megabitach

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
Size: 1 MB
Size: 8 Mb
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

Hints
  • 1 Kb = 1024 b

  • 1 Mb = 1024 Kb

  • 1 B = 8 b

  • 1 KB = 1024 B

  • 1 MB = 1024 KB

2.1.4.5. Type Int Bandwidth

  • Assignment name: Type Int Bandwidth

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 10 lines

  • Estimated time of completion: 3 min

  • Solution: solution/type_int_bandwidth.py

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

  2. How long will take to download 100 MB?

  3. W obliczeniach użyj truediv (//)

  4. Compare result with "Output" section (see below)

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

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

  3. In Calculations use truediv (//)

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
File size: 100 MB
Download speed: 12 sec
Download time: 8 sec
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Mathematical operations

Hints
  • 1 Kb = 1024 b

  • 1 Mb = 1024 Kb

  • 1 B = 8 b

  • 1 KB = 1024 B

  • 1 MB = 1024 KB

2.1.4.6. Type Int Temperature

  • Assignment name: Type Int Temperature

  • Last update: 2020-10-01

  • Complexity level: medium

  • Lines of code to write: 18 lines

  • Estimated time of completion: 8 min

  • Solution: solution/type_int_temperature.py

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: 453 K

    • Lunar night: 93 K

    • Mars highest: 20 °C

    • Mars lowest: -153 °C

    • Mars average: −63 °C

  5. Compare result with "Output" 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 Kelwinów, a ile stopni Celsiusza wynoszą średnie temperatury powierzchni [dACSICINTA19]:

    • Księżyca w dzień: 453 K

    • Księżyca w nocy: 93 K

    • Mars najwyższa: 20 °C

    • Mars najniższa: -153 °C

    • Mars średnia: −63 °C

  5. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Output
Moon day: 453K, 726°C
Moon night: 93K, 93°C
Mars high: -253K, 20°C
Mars low: -393K, -153°C
Mars avg: -336K, -63°C
The whys and wherefores
  • Defining constants and variables

  • Naming convention

  • Print formatting

  • Mathematical operations