4.2. Numeric Floatï
Represents floating point number (vide IEEE-754)
Could be both signed and unsigned
Default
floatsize is 64 bitPython automatically extends
floatwhen need bigger number
Example:
>>> height = 1.75
>>> weight = 75.0
>>> volume = 1.5
>>> price = 1000.00
>>> pressure = 1013.25
>>> pi = 3.14159265359
4.2.1. Syntaxï
>>> data = 1.0
>>> data = +1.0
>>> data = -1.0
Floating-point numbers are not real numbers, so the result of 1.0/3.0
cannot be represented exactly without infinite precision. In the decimal
(base 10) number system, one-third is a repeating fraction, so it has an
infinite number of digits. Even simple non-repeating decimal numbers can
be a problem. One-tenth (0.1) is obviously non-repeating, so we can
express it exactly with a finite number of digits. As it turns out, since
numbers within computers are stored in binary (base 2) form, even one-tenth
cannot be represented exactly with floating-point numbers.
When should you use integers and when should you use floating-point numbers? A good rule of thumb is this: use integers to count things and use floating-point numbers for quantities obtained from a measuring device. As examples, we can measure length with a ruler or a laser range finder; we can measure volume with a graduated cylinder or a flow meter; we can measure mass with a spring scale or triple-beam balance. In all of these cases, the accuracy of the measured quantity is limited by the accuracy of the measuring device and the competence of the person or system performing the measurement. Environmental factors such as temperature or air density can affect some measurements. In general, the degree of inexactness of such measured quantities is far greater than that of the floating-point values that represent them.
Despite their inexactness, floating-point numbers are used every day throughout the world to solve sophisticated scientific and engineering problems. The limitations of floating-point numbers are unavoidable since values with infinite characteristics cannot be represented in a finite way. Floating-point numbers provide a good trade-off of precision for practicality.
Note
Source [1]
4.2.2. Without Zero Notationï
.1- notation without leading zero (0.1)1.- notation without trailing zero (1.0)Used by
numpy
Notation without leading zero:
>>> data = .1
>>> print(data)
0.1
Notation without trailing zero (1.0):
>>> data = 1.
>>> print(data)
1.0
4.2.3. Engineering Notationï
The exponential is a number divisible by 3
Allows the numbers to explicitly match their corresponding SI prefixes
1e3is equal to1000.0(kilo)1e-3is equal to0.001(milli)
You can use both lower e or uppercase letter E:
>>> x = 1e3
>>> print(x)
1000.0
>>>
>>> x = 1E3
>>> print(x)
1000.0
Both negative and positive exponents are supported:
>>> x = 1e3
>>> print(x)
1000.0
>>>
>>> x = 1e-3
>>> print(x)
0.001
Both negative and positive numbers are supported:
>>> x = +1e3
>>> print(x)
1000.0
>>>
>>> x = -1e3
>>> print(x)
-1000.0
Engineering notation with prefixes:
>>> yocto = 1e-24 # 0.000000000000000000000001.0
>>> zepto = 1e-21 # 0.000000000000000000001.0
>>> atto = 1e-18 # 0.000000000000000001.0
>>> femto = 1e-15 # 0.000000000000001.0
>>> pico = 1e-12 # 0.000000000001.0
>>> nano = 1e-9 # 0.000000001.0
>>> micro = 1e-6 # 0.000001.0
>>> milli = 1e-3 # 0.001.0
>>> #
>>> kilo = 1e3 # 1000.0
>>> mega = 1e6 # 1000000.0
>>> giga = 1e9 # 1000000000.0
>>> tera = 1e12 # 1000000000000.0
>>> peta = 1e15 # 1000000000000000.0
>>> exa = 1e18 # 1000000000000000000.0
>>> zetta = 1e21 # 1000000000000000000000.0
>>> yotta = 1e24 # 1000000000000000000000000.0
4.2.4. Scientific notationï
1.23e3is equal to1230.01.23e-3is equal to1.23e-3
>>> 1.23e3
1230.0
>>>
>>> 1.23e-3
0.00123
For numbers below 4 decimal places, Python will use notation automatically:
>>> 1.23e-4
0.000123
>>>
>>> 1.23e-5
1.23e-05
Both negative and positive numbers are supported:
>>> 1.23e3
1230.0
>>>
>>> -1.23e3
-1230.0
>>>
>>> 1.23e-3
0.00123
>>>
>>> -1.23e-3
-0.00123
4.2.5. Conversionï
Builtin
float()converts argument tofloat
Builtin float() converts argument to float
>>> float(1)
1.0
>>>
>>> float('1.0')
1.0
>>> float('+1.0')
1.0
>>>
>>> float('-1.0')
-1.0
4.2.6. Thousand separatorï
Underscore (
_) can be used as a thousand separator
>>> data = 1_000_000.0
>>> data = 0.000_0001
>>> data = 1_000_000.000_0001
4.2.7. Decimal Separatorï
1.0- Decimal point1,0- Decimal comma0Ù«â1- Arabic decimal separator (Left to right)More information: [2]
>>> data = 1.0
>>> type(data)
<class 'float'>
>>>
>>> data = 1,0
>>> type(data)
<class 'tuple'>
>>> float('1.0')
1.0
>>>
>>> float('1,0')
Traceback (most recent call last):
ValueError: could not convert string to float: '1,0'
4.2.8. Round Numberï
round(number, ndigits)- Round a number to n decimal placesf-string rounding
>>> pi = 3.14159265359
>>>
>>>
>>> round(pi, 4)
3.1416
>>>
>>> round(pi, 2)
3.14
>>>
>>> round(pi, 0)
3.0
>>>
>>> round(pi)
3
Rounding a number in string formatting:
>>> pi = 3.14159265359
>>>
>>>
>>> print(f'Pi number is {pi}')
Pi number is 3.14159265359
>>>
>>> print(f'Pi number is {pi:.4f}')
Pi number is 3.1416
>>>
>>> print(f'Pi number is {pi:.2f}')
Pi number is 3.14
>>>
>>> print(f'Pi number is {pi:.0f}')
Pi number is 3
4.2.9. Further Readingï
Wikipedia. Decimal separator. Year: 2024. Retrieved: 2024-07-01. URL: https://en.wikipedia.org/wiki/Decimal_separator
4.2.10. Referencesï
4.2.11. Assignmentsï
# %% About
# - Name: Numeric Float Tax
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. The price of the service is 123.00 Euro-cent
# 2. Define variable `result` with price in EUR
# 3. Run doctests - all must succeed
# %% Polish
# 1. Cena usÅugi wynosi 123.00 Euro-cent
# 2. Zdefiniuj zmiennÄ
`result` z cenÄ
w EUR
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 1.23
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is float, \
'Variable `result` has an invalid type; expected: `float`.'
>>> pprint(result)
1.23
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: float
# %% Data
CENT = 1
EUR = 100*CENT
PRICE = 123.00*CENT
# %% Result
result = ...
# %% About
# - Name: Numeric Float Tax
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3
# %% 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
# %% English
# 1. The cost of the service is 100.00 EUR
# 2. Define variable `result1` with `PRICE` in Polish Zloty (PLN)
# 3. Define variable `result2` with `PRICE` in US Dollars (USD)
# 4. Define variable `result3` with `PRICE` in Australian Dollars (AUD)
# 5. Define variable `result4` with `PRICE` in Canadian Dollars (CAD)
# 3. Run doctests - all must succeed
# %% Polish
# 1. Cena usÅugi wynosi 100.00 EUR
# 2. Zdefiniuj zmiennÄ
`result1` z `PRICE` w polskich zÅotych (PLN)
# 3. Zdefiniuj zmiennÄ
`result2` z `PRICE` w dolarach amerykaÅskich (USD)
# 4. Zdefiniuj zmiennÄ
`result3` z `PRICE` w dolarach australijskich (AUD)
# 5. Zdefiniuj zmiennÄ
`result4` z `PRICE` w dolarach kanadyjskich (CAD)
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result1
# 435.0
#
# >>> result2
# 110.0
#
# >>> result3
# 166.0
#
# >>> result4
# 149.0
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is float, \
'Variable `result1` has an invalid type; expected: `float`.'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert type(result2) is float, \
'Variable `result2` has an invalid type; expected: `float`.'
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert type(result3) is float, \
'Variable `result3` has an invalid type; expected: `float`.'
>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'
>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'
>>> assert type(result4) is float, \
'Variable `result4` has an invalid type; expected: `float`.'
>>> result = round(result1, 1)
>>> pprint(result)
435.0
>>> result = round(result2, 1)
>>> pprint(result)
110.0
>>> result = round(result3, 1)
>>> pprint(result)
166.0
>>> result = round(result4, 1)
>>> pprint(result)
149.0
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result1: float
result2: float
result3: float
result4: float
# %% Data
EUR = 1
PLN = EUR / 4.35
USD = EUR / 1.10
AUD = EUR / 1.66
CAD = EUR / 1.49
PRICE = 100*EUR
# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...
# %% About
# - Name: Numeric Float Tax
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% 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
# %% English
# 1. The price `PRICE` of the service is 1013.25 EUR net
# 2. Tax `TAX` is 20.946%
# 3. Define variable `result` with gross values (price with tax)
# 4. Run doctests - all must succeed
# %% Polish
# 1. Cena `PRICE` wynosi 1013.25 EUR netto
# 2. Podatek `TAX` wynosi 20.946%
# 3. Zdefiniuj zmiennÄ
`result` z cenÄ
brutto (cena z podatkiem)
# 4. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 1225.49
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is float, \
'Variable `result` has an invalid type; expected: `float`.'
>>> result = round(result, 2)
>>> pprint(result)
1225.49
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: float
# %% Data
EUR = 1.00
TAX = 0.20946
PRICE = 1013.25*EUR
# %% Result
result = ...
# %% About
# - Name: Numeric Float Tax
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% 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
# %% English
# 1. The price `PRICE` of the service is 1013.25 EUR net
# 2. Tax `TAX` is 20.946%
# 3. Define variable `result` with tax value in EUR
# 4. Run doctests - all must succeed
# %% Polish
# 1. Cena `PRICE` wynosi 1013.25 EUR netto
# 2. Podatek `TAX` wynosi 20.946%
# 3. Zdefiniuj zmiennÄ
`result` z wartoÅciÄ
podatku w EUR
# 4. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 212.24
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is float, \
'Variable `result` has an invalid type; expected: `float`.'
>>> result = round(result, 2)
>>> pprint(result)
212.24
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: float
# %% Data
EUR = 1.00
VAT = 0.20946
PRICE = 1013.25*EUR
# %% Result
result = ...
# %% About
# - Name: Numeric Float Altitude
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% 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
# %% English
# 1. Plane altitude is 10 km
# 2. Convert to meters (m) in the metric system (SI)
# 3. Convert to feet (ft) in the imperial system (US)
# 4. Run doctests - all must succeed
# %% Polish
# 1. WysokoÅÄ lotu samolotem wynosi 10 km
# 2. Przelicz je na metry (m) w systemie metrycznym (ukÅad SI)
# 3. Przelicz je na stopy (ft) w systemie imperialnym (US)
# 4. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result_m
# 10000.0
#
# >>> result_ft
# 32808.4
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert altitude_m is not Ellipsis, \
'Variable `altitude_m` has an invalid value; assign result of your program to it.'
>>> assert altitude_ft is not Ellipsis, \
'Variable `altitude_ft` has an invalid value; assign result of your program to it.'
>>> assert type(altitude_m) is float, \
'Variable `altitude_m` has an invalid type; expected: `float`.'
>>> assert type(altitude_ft) is float, \
'Variable `altitude_ft` has an invalid type; expected: `float`.'
>>> result = round(altitude_m, 1)
>>> pprint(result)
10000.0
>>> result = round(altitude_ft, 1)
>>> pprint(result)
32808.4
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
altitude_m: float
altitude_ft: float
# %% Data
m = 1
km = 1000 * m
ft = 0.3048 * m
ALTITUDE = 10*km
# %% Result
altitude_m = ...
altitude_ft = ...
# %% About
# - Name: Numeric Float Volume
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% 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
# %% English
# 1. Bottle volume is 500 milliliter
# 2. Convert to liters (liter) in the metric system (SI)
# 3. Convert to fluid ounces (floz) in the imperial system (US)
# 4. Run doctests - all must succeed
# %% Polish
# 1. ObjÄtoÅÄ butelki wynosi 500 mililitrów
# 2. Przelicz je na litry (liter) w systemie metrycznym (ukÅad SI)
# 3. Przelicz je na uncje pÅynu (floz) w systemie imperialnym (US)
# 4. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result_floz
# 16.9
#
# >>> result_l
# 0.5
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert volume_floz is not Ellipsis, \
'Variable `volume_floz` has an invalid value; assign result of your program to it.'
>>> assert volume_l is not Ellipsis, \
'Variable `volume_l` has an invalid value; assign result of your program to it.'
>>> assert type(volume_floz) is float, \
'Variable `volume_floz` has an invalid type; expected: `float`.'
>>> assert type(volume_l) is float, \
'Variable `volume_l` has an invalid type; expected: `float`.'
>>> result = round(volume_floz, 1)
>>> pprint(result)
16.9
>>> result = round(volume_l, 1)
>>> pprint(result)
0.5
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
volume_l: float
volume_floz: float
# %% Data
liter = 1
milliliter = 0.001 * liter
floz = 0.02957344 * liter
VOLUME = 500 * milliliter
# %% Result
volume_l = ...
volume_floz = ...
# %% About
# - Name: Numeric Float Round
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% 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
# %% English
# 1. Pi number is 3.14159
# 2. Define variable `result1` with number rounded to 3 decimal places
# 3. Define variable `result2` with number rounded to 2 decimal places
# 4. Define variable `result3` with number rounded to 1 decimal place
# 5. Define variable `result4` with number rounded to 0 decimal places
# 6. Use `round()`
# 7. Run doctests - all must succeed
# %% Polish
# 1. Liczba Pi to 3.14159
# 2. Zdefiniuj zmiennÄ
`result1` z liczbÄ
zaokrÄ
glonÄ
do 3 miejsc po przecinku
# 3. Zdefiniuj zmiennÄ
`result2` z liczbÄ
zaokrÄ
glonÄ
do 2 miejsc po przecinku
# 4. Zdefiniuj zmiennÄ
`result3` z liczbÄ
zaokrÄ
glonÄ
do 1 miejsca po przecinku
# 5. Zdefiniuj zmiennÄ
`result4` z liczbÄ
zaokrÄ
glonÄ
do 0 miejsc po przecinku
# 6. Użyj `round()`
# 7. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result1
# 3.142
#
# >>> result2
# 3.14
#
# >>> result3
# 3.1
#
# >>> result4
# 3.0
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'
>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is float, \
'Variable `result1` has an invalid type; expected: `float`.'
>>> assert type(result2) is float, \
'Variable `result2` has an invalid type; expected: `float`.'
>>> assert type(result3) is float, \
'Variable `result3` has an invalid type; expected: `float`.'
>>> assert type(result4) is float, \
'Variable `result4` has an invalid type; expected: `float`.'
>>> pprint(result1)
3.142
>>> pprint(result2)
3.14
>>> pprint(result3)
3.1
>>> pprint(result4)
3.0
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result1: float
result2: float
result3: float
result4: float
# %% Data
PI = 3.14159
# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...
# %% About
# - Name: Numeric Float Round
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% 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
# %% English
# 1. Pi number is 3.14159
# 2. Define `result1: str` with number rounded to 3 decimal places
# 3. Define `result2: str` with number rounded to 2 decimal places
# 4. Define `result3: str` with number rounded to 1 decimal place
# 5. Define `result4: str` with number rounded to 0 decimal places
# 6. Use f-string formatting
# 7. Run doctests - all must succeed
# %% Polish
# 1. Liczba Pi to 3.14159
# 2. Zdefiniuj `result1: str` z liczbÄ
zaokrÄ
glonÄ
do 3 miejsc po przecinku
# 3. Zdefiniuj `result2: str` z liczbÄ
zaokrÄ
glonÄ
do 2 miejsc po przecinku
# 4. Zdefiniuj `result3: str` z liczbÄ
zaokrÄ
glonÄ
do 1 miejsca po przecinku
# 5. Zdefiniuj `result4: str` z liczbÄ
zaokrÄ
glonÄ
do 0 miejsc po przecinku
# 6. Użyj formatowania f-string
# 7. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result1
# 'Result: 3.142'
#
# >>> result2
# 'Result: 3.14'
#
# >>> result3
# 'Result: 3.1'
#
# >>> result4
# 'Result: 3'
# %% Hints
# - `f'{number:.2f}'`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'
>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is str, \
'Variable `result1` has an invalid type; expected: `str`.'
>>> assert type(result2) is str, \
'Variable `result2` has an invalid type; expected: `str`.'
>>> assert type(result3) is str, \
'Variable `result3` has an invalid type; expected: `str`.'
>>> assert type(result4) is str, \
'Variable `result4` has an invalid type; expected: `str`.'
>>> pprint(result1)
'Result: 3.142'
>>> pprint(result2)
'Result: 3.14'
>>> pprint(result3)
'Result: 3.1'
>>> pprint(result4)
'Result: 3'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result1: str
result2: str
result3: str
result4: str
# %% Data
PI = 3.14159
# %% Result
result1 = f'Result: {PI}'
result2 = f'Result: {PI}'
result3 = f'Result: {PI}'
result4 = f'Result: {PI}'
# %% About
# - Name: Numeric Float Velocity
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% 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
# %% English
# 1. The speed limit is 75 MPH (miles per hour)
# 2. Define variable `result` with value in kilometers per hour (kph)
# 3. Run doctests - all must succeed
# %% Polish
# 1. Ograniczenie prÄdkoÅci wynosi 75 MPH (mile na godzinÄ)
# 2. Zdefiniuj zmiennÄ
`result` z wartoÅciÄ
w kilometrach na godzinÄ (kph)
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 120.7
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is float, \
'Variable `result` has an invalid type; expected: `float`.'
>>> result = round(result, 1)
>>> pprint(result)
120.7
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: float
# %% Data
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
m = 1
km = 1000 * m
mi = 1609.344 * m
kph = km / HOUR
mph = mi / HOUR
SPEED_LIMIT = 75 * mph
# %% Result
result = ...