3.3. Numeric Recap

  • Recap information about int and float

3.3.1. Int

  • Represents an integer

  • Could be both signed and unsigned

  • Default int size is 64 bit

  • Python automatically extends int when need bigger number

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

  • Builtin function int() converts argument to int

  • Works with strings if they have numbers and +, -, _ (underscore)

Definition:

>>> data = 1
>>> data = +1
>>> data = -1

Thousand separator:

>>> million = 1000000
>>> million = 1_000_000

Conversion:

>>> int(1.0)
1
>>>
>>> int('1')
1

3.3.2. Float

  • Represents floating point number (vide IEEE-754)

  • Could be both signed and unsigned

  • Default float size is 64 bit

  • Python automatically extends float when need bigger number

  • .1 - notation without leading zero (0.1)

  • 1. - notation without trailing zero (1.0)

  • Engineering notation: mega = 1e6, micro = 1e-6

  • Scientific notation: 1.23e-4

  • Builtin float() converts argument to float

Definition:

>>> data = 1.0
>>> data = +1.0
>>> data = -1.0

Leading and trailing zero:

>>> data = .1    # 0.1
>>> data = 1.    # 1.0

Engineering and scientific notation:

>>> 1.23e-4
0.000123

Conversion:

>>> float(1)
1.0
>>>
>>> float('1.0')
1.0

Decimal separator:

>>> float('1.0')
1.0
>>>
>>> float('1,0')
Traceback (most recent call last):
ValueError: could not convert string to float: '1,0'

Rounding:

>>> pi = 3.14159265359
>>>
>>>
>>> round(pi, 2)
3.14
>>>
>>> print(f'Pi number is {pi:.2f}')
Pi number is 3.14

3.3.3. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Numeric Recap Bits
# - Difficulty: medium
# - Lines: 3
# - Minutes: 2

# %% English
# 1. File size is 1337 megabits [Mb]
# 2. Calculate size in bits [b]
# 3. Calculate size in kilobits [kb]
# 4. Use floordiv (`//`) operator
# 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. Użyj operatora floordiv (`//`)
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `//` - floordiv
# - 1 kb = 1024 b
# - 1 Mb = 1024 Kb

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert size_bits is not Ellipsis, \
'Assign your result to variable `size_bits`'
>>> assert size_kilobits is not Ellipsis, \
'Assign your result to variable `size_kilobits`'
>>> assert size_megabits is not Ellipsis, \
'Assign your result to variable `size_megabits`'
>>> assert type(size_bits) is int, \
'Variable `size_bits` has invalid type, should be int'
>>> assert type(size_kilobits) is int, \
'Variable `size_kilobits` has invalid type, should be int'
>>> assert type(size_megabits) is int, \
'Variable `size_megabits` has invalid type, should be int'

>>> assert size_bits == 1_401_946_112, \
'Invalid value for `size_bits`. Check you calculation'
>>> assert size_kilobits == 1_369_088, \
'Invalid value for `size_kilobits`. Check you calculation'
>>> assert size_megabits == 1337, \
'Invalid value for `size_megabits`. Check you calculation'
"""

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

SIZE = 1337 * Mb

# SIZE in bits
# Use floordiv (`//`) operator
# type: int
size_bits = ...

# SIZE in kilobits
# Use floordiv (`//`) operator
# type: int
size_kilobits = ...

# SIZE in megabits
# Use floordiv (`//`) operator
# type: int
size_megabits = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Numeric Recap Bytes
# - Difficulty: medium
# - Lines: 2
# - Minutes: 2

# %% 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
# - `//` - floordiv
# - 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'


>>> assert size_kilobytes is not Ellipsis, \
'Assign your result to variable `size_kilobytes`'
>>> assert size_megabits is not Ellipsis, \
'Assign your result to variable `size_megabits`'
>>> assert type(size_kilobytes) is int, \
'Variable `size_kilobytes` has invalid type, should be int'
>>> assert type(size_megabits) is int, \
'Variable `size_megabits` has invalid type, should be int'

>>> assert size_kilobytes == 102_400, \
'Invalid value for `size_kilobytes`. Check you calculation'
>>> assert size_megabits == 800, \
'Invalid value for `size_megabits`. Check you calculation'
"""

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

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

SIZE = 100 * MB

# SIZE in kilobytes
# Use floordiv (`//`) operator
# type: int
size_kilobytes = ...

# SIZE in megabits
# Use floordiv (`//`) operator
# type: int
size_megabits = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Numeric Recap Bandwidth
# - Difficulty: medium
# - Lines: 3
# - Minutes: 3

# %% 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
# 4. Note, that all values must be `int` (type cast if needed)
# 5. Use floordiv (`//`) operator
# 6. 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ść
# 4. Zwróć uwagę, że wszystkie wartości mają być `int`
#    (rzutuj typ jeżeli potrzeba)
# 5. Użyj operatora floordiv (`//`)
# 6. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `*` - mul operator
# - `//` - floordiv
# - 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert bandwidth is not Ellipsis, \
'Assign your result to variable `bandwidth`'
>>> assert size is not Ellipsis, \
'Assign your result to variable `size`'
>>> assert duration is not Ellipsis, \
'Assign your result to variable `duration`'

>>> 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'

>>> assert bandwidth == 104_857_600, \
'Invalid value for `bandwidth`. Check you calculation'
>>> assert size == 838_860_800, \
'Invalid value for `size`. Check you calculation'
>>> assert duration == 8, \
'Invalid value for `duration`. Check you calculation'
"""

SECOND = 1

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

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

# bandwidth is 100 megabits per second
# Use floordiv (`//`) operator
# type: int
bandwidth = ...

# size is 100 megabytes
# type: int
size = ...

# Duration is size by bandwidth in seconds
# Use floordiv (`//`) operator
# type: int
duration = ...


../../_images/type-float-spacesuits.png

Figure 3.1. EMU and Orlan

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Numeric Recap Pressure
# - Difficulty: medium
# - Lines: 2
# - Minutes: 3

# %% English
# 1. Operational pressure of EMU spacesuit: 4.3 PSI
# 2. Operational pressure of ORLAN spacesuit: 40 kPa
# 3. Calculate operational pressure in hPa for EMU
# 4. Calculate operational pressure in hPa for Orlan
# 5. Run doctests - all must succeed

# %% Polish
# 1. Ciśnienie operacyjne skafandra kosmicznego EMU (NASA): 4.3 PSI
# 2. Ciśnienie operacyjne skafandra kosmicznego ORLAN (Roscosmos): 40 kPa
# 3. Oblicz ciśnienie operacyjne skafandra EMU w hPa
# 4. Oblicz ciśnienie operacyjne skafandra Orlan w hPa
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert emu is not Ellipsis, \
'Assign your result to variable `emu`'
>>> assert orlan is not Ellipsis, \
'Assign your result to variable `orlan`'
>>> assert type(emu) is float, \
'Variable `emu` has invalid type, should be float'
>>> assert type(orlan) is float, \
'Variable `orlan` has invalid type, should be float'

>>> round(orlan, 1)
400.0
>>> round(emu, 1)
296.5
"""

Pa = 1
hPa = 100 * Pa
kPa = 1000 * Pa
psi = 6894.757 * Pa

# 4.3 pounds per square inch in hectopascals
# type: float
emu = ...

# 40 kilopascals in hectopascals
# type: float
orlan = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Numeric Recap Percent
# - Difficulty: medium
# - Lines: 2
# - Minutes: 3

# %% English
# 1. Pressure in International Standard Atmosphere (ISA)
#    at sea level is: 1 ata = 1013.25 hPa
# 2. Calculate `pO2` - partial pressure of oxygen at sea level in hPa
# 3. To calculate partial pressure use ratio
#    100% --- 1013.25 hPa
#    20.946% --- ? hPa
# 4. Run doctests - all must succeed

# %% Polish
# 1. Ciśnienie w Międzynarodowej Standardowej Atmosfera (ISA)
#    na poziomie morza wynosi: 1 ata = 1013.25 hPa
# 2. Oblicz `pO2` - ciśnienie parcjalne tlenu na poziomie morza w hPa
# 3. Aby policzyć ciśnienie parcjalne skorzystaj z proporcji
#    100% --- 1013.25 hPa
#    20.946% --- ? hPa
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - 1 hPa = 100 Pa
# - 1 kPa = 1000 Pa
# - 1 ata = 1013.25 hPa
# - ISA - International Standard Atmosphere
# - Nitrogen 78.084%
# - Oxygen 20.946%
# - Argon 0.9340%
# - Carbon Dioxide 0.0407%
# - Others 0.001%

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert ata is not Ellipsis, \
'Assign your result to variable `ata`'
>>> assert type(ata) is float, \
'Variable `ata` has invalid type, should be float'

>>> assert pO2 is not Ellipsis, \
'Assign your result to variable `pO2`'
>>> assert type(pO2) is float, \
'Variable `pO2` has invalid type, should be float'

>>> ata
101325.0
>>> round(pO2, 1)
212.2
"""

PERCENT = 100
N2 = 78.084 / PERCENT
O2 = 20.946 / PERCENT
Ar = 0.9340 / PERCENT
CO2 = 0.0407 / PERCENT
Others = 0.001 / PERCENT

Pa = 1
hPa = 100 * Pa
kPa = 1000 * Pa

# Pressure in ISA at sea level is: 1 ata = 1013.25 hPa
# type: float
ata = ...

# 20.946% of pressure at sea level in hPa
# type: float
pO2 = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Numeric Recap Gradient
# - Difficulty: medium
# - Lines: 2
# - Minutes: 3

# %% English
# 1. At what altitude above sea level, pressure is equal
#    to partial pressure of oxygen
# 2. Print result in meters rounding to two decimal places
# 3. To calculate partial pressure use ratio
#    (100% is 1013.25 hPa, 20.946% is how many hPa?)
# 4. Calculated altitude is pressure at sea level minus
#    oxygen partial pressure divided by gradient
# 5. Mind the operator precedence
# 6. Run doctests - all must succeed

# %% Polish
# 1. Na jakiej wysokości nad poziomem morza panuje ciśnienie
#    równe ciśnieniu parcjalnemu tlenu?
# 2. Wypisz rezultat w metrach zaokrąglając do dwóch miejsc po przecinku
# 3. Aby policzyć ciśnienie parcjalne skorzystaj z proporcji
#    (100% to 1013.25 hPa, 20.946% to ile hPa?)
# 4. Wyliczona wysokość to ciśnienie atmosferyczne na poziomie morza minus
#    ciśnienie parcjalne tlenu podzielone przez gradient
# 5. Zwróć uwagę na kolejność wykonywania działań
# 6. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - pressure gradient (decrease) = 11.3 Pa / 1 m
# - 1 hPa = 100 Pa
# - 1 kPa = 1000 Pa
# - 1 ata = 1013.25 hPa (ISA - International Standard Atmosphere)
# - Nitrogen 78.084%
# - Oxygen 20.946%
# - Argon 0.9340%
# - Carbon Dioxide 0.0407%
# - Others 0.001%

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert gradient is not Ellipsis, \
'Assign your result to variable `gradient`'
>>> assert type(gradient) is float, \
'Variable `gradient` has invalid type, should be float'

>>> assert altitude is not Ellipsis, \
'Assign your result to variable `altitude`'
>>> assert type(altitude) is float, \
'Variable `altitude` has invalid type, should be float'

>>> pO2
21223.5345
>>> gradient
11.3
>>> round(altitude/m, 2)
7088.63
"""

PERCENT = 100
N2 = 78.084 / PERCENT
O2 = 20.946 / PERCENT
Ar = 0.9340 / PERCENT
CO2 = 0.0407 / PERCENT
Others = 0.001 / PERCENT

m = 1
Pa = 1
hPa = 100 * Pa
ata = 1013.25 * hPa
pO2 = O2 * ata

# 11.3 Pascals per meter
# type: float
gradient = ...

# Altitude is ata minus pO2 all that divided by gradient
# type: float
altitude = ...