1. int

1.1. Defining int

  • 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

value = 30              # 30
value = -30             # -30
million = 1000000        # 1000000
million = 1_000_000      # 1000000

1.2. Converting to int

  • Also known as "type casting"

  • int() converts argument to int

  • int() does not round numbers, it returns integer value

int(10)                 # 10
int(10.0)               # 10
int(10.9)               # 10
int(1.23)               # 1
int(-1.23)              # -1
int('1')                # 1
int('-1')               # -1
int('1.23')             # ValueError: invalid literal for int() with base 10: '1.23'
int('-1.23')            # ValueError: invalid literal for int() with base 10: '-1.23'

1.3. Numerical Operators

1.3.1. Addition

Table 6. Addition operators

Operand

Description

+x

x

x + y

Sum x and y

x += y

Incremental addition

value = 10 + 2

print(value)
# 12
value = 10
value += 2

print(value)
# 12

1.3.2. Subtraction

Table 7. Subtraction operators

Operand

Description

-x

x negation

x - y

Subtract x and y

x -= y

Incremental subtraction

value = 10 - 2

print(value)
# 8
value = 10
value -= 2

print(value)
# 8

1.3.3. Multiplication

Table 8. Multiplication operators

Operand

Description

x * y

Multiply x and y

x *= y

Incremental multiplication

x ** y

x to the power of y

value = 10 * 2

print(value)
# 20
value = 10
value *= 2

print(value)
# 20
10 ** 2         # 100
3 ** 4          # 81
-1 ** 2         # 1

1.3.4. Division

Table 9. Division operators

Operand

Description

x / y

Divide x and y

x /= y

Incremental division

x // y

Quotient of division x by y

x % y

Modulo. Reminder of division x by y

value = 10 / 2

print(value)
# 5
value = 10
value /= 2

print(value)
# 5
10 // 2         # 5
10 % 2          # 0

10 // 3         # 3
10 % 3          # 1

1.4. Numeric Functions

1.4.1. Minimal value

min(3, 1, 5)    # 1

1.4.2. Maximal value

max(3, 1, 5)    # 5

1.4.3. Absolute value

abs(1)          # 1
abs(-1)         # 1

1.4.4. Number to the n-th power

pow(10, 2)      # 100
pow(3, 4)       # 81
pow(-1, 2)      # 1

1.5. Assignments

1.5.1. Time

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

  • Filename: solution/int_time.py

English
  1. Calculate how many seconds is five minutes

  2. Calculate how many seconds is one hour

  3. Calculate how many seconds is eight hours

  4. Calculate how many seconds is month (22 days per 8 hours)

  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 osiem godzin

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

  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

1.5.2. Megabits and Megabytes

English
  1. Calculate how many bits is one Megabyte

  2. How many times Megabyte is larger than Megabit?

Polish
  1. Oblicz ile bitów to jeden Megabajt

  2. O ile różni się Megabajt od Megabita?

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

1.5.3. Download time

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

1.5.4. 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