A. Introduction
Float is one of the data types under the numeric category. See the Integer type in this tutorial. Python has integer, float, complex, bool, list, tuple, set, range, dictionary, string, and bytes data types. In this tutorial I will only cover the Float type.
B. Numeric data type
There are three built-in numeric data types in Python.
Integer
Float
Complex
There are also two non-built-in numeric data types.
Fraction
Decimal
Built-in means you don't need to import such data or functionality.
C. Float
Float or floating-point number may represent a number that is equal to one or more, or a number that is less than one with decimal point. It can be a positive number such as 1.25 or 0.75. It can also be a negative number such as -100.5 or -0.0015. It can also be just 0.0. It can also be a whole number with decimal point such as 40.0.
Python's float has a maximum value of 1.7976931348623157e+308. Its minimum positive value is 2.2250738585072014e-308.
You can get these values by running the Python interpreter from your console or command line. Assuming that you already installed Python.
C:\>python
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> max_value = sys.float_info.max
>>> max_value
1.7976931348623157e+308
>>> min_value = sys.float_info.min
>>> min_value
2.2250738585072014e-308
Output the full digits for maximum value.
>>> int(max_value)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
D. Operators
We usually use operators to manipulate data. I will only cover some common operators that are usually applied for float. The full operator tutorial will cover this.
Arithmetic operators.
Operator | Name |
---|---|
+ | addition |
- | subtraction |
/ | float division |
% | remainder, a % b |
** | power, a ** b or a ^ b |
Comparison operators.
Operator | Name |
---|---|
> | greater than |
>= | greater than or equal |
< | less than |
<= | less than or equal |
== | equal, a == b |
!= | not equal, a != b |
1. Arithmetic Operators
Addition and subtraction
Open your python interpreter from the terminal by typing python.
c:\>python
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10.5 + 1.5
12.0
>>> 20.0 - 5.5
14.5
Multiplication
Use an asterisk symbol for multiplication. The "type()" method gets the type or class of a variable or object.
>>> a = 100.5
>>> b = 2.5
>>> c = a * b
>>> c
251.25
>>> type(c)
<class 'float'>
>>> type(a)
<class 'float'>
>>> type(b)
<class 'float'>
Division
Use the "/" symbol to get the quotient.
>>> a = 300.9
>>> b = 200.5
>>> c = a / b
>>> c
1.5007481296758103
Remainder
Python has an operator that can calculate the remainder. It is called the modulo operator with symbol %
.
>>> a = 2.5
>>> b = 6.2
>>> a % b
2.5
Power
The built-in function pow() will return a number given base and exponent. It can also be expressed using double asterisk.
>>> a = 6.4
>>> b = 1.25
>>> pow(a, b)
10.17946532821825
>>> a ** b
10.17946532821825
2. Comparison operators
Greater than operator ">".
>>> a = 1.9
>>> b = 2.1
>>> a > b
False
Greater than or equal to with symbol ">=".
>>> a = 150.0
>>> b = 120.8
>>> a >= b
True
Less than with symbol "<".
>>> a = 10.8
>>> b = 20.0
>>> a < b
True
Less than or equal to with symbol "<=".
>>> a = 250.5
>>> b = 250.5
>>> if a <= b:
... print("a is less than or equal to b")
... else:
... print("a is not less than or equal to b")
...
a is less than or equal to b
Equal with symbol "==".
>>> a = 0.36
>>> b = 0.36
>>> a == b
True
>>>
>>> # another example
>>> a = 50.45
>>> b = 50.40
>>> a == b
False
Not equal with symbol "!=".
>>> a = 1000.36
>>> b = 1000.361
>>> a != b
True
E. Class float() Constructor
Python has a built-in function called "float()" or a class constructor that can set another type into a float type.
Convert an integer to a float.
>>> a = 100
>>> a
100
>>> type(a)
<class 'int'>
>>> b = float(a)
>>> b
100.0
>>> type(b)
<class 'float'>
Convert a numeric string to a float.
>>> a = "75.5"
>>> a
'75.5'
>>> type(a)
<class 'str'>
>>> b = float(a)
>>> b
75.5
>>> type(b)
<class 'float'>
F. Float Example
Write a python script called "fruit.py" that takes the price of apple per kilogram, the number kilograms to buy and output the total cost.
fruit.py
# Ask the cost per kilograms.
cost_per_kg = input("How much is the cost of apple per kilogram? ")
cost_per_kg = float(cost_per_kg) # convert string to float
# Ask how many kilograms to buy.
num_kg = input("How many kilograms to buy? ")
num_kg = float(num_kg)
# Calculate the total cost.
total_cost = cost_per_kg * num_kg
print("total cost: " + str(total_cost))
execute the script
c:\>python fruit.py
Put your fruit.py in C drive.
If the file is in C drive under python_tutorial folder for example, "cd" or change directory to that folder. The command will look like this:
c:\python_tutorial>python fruit.py
output
How much is the cost of apple per kilogram? 1.44
How many kilograms to buy? 1.5
total cost: 2.16
G. Float method as_integer_ratio
The class float has a method that returns an integer numerator and denominator. It is defined as "float.as_interger_ratio()".
>>> a = 0.25
>>> b = a.as_integer_ratio()
>>> b
(1, 4)
>>> numerator = b[0]
>>> denominator = b[1]
>>> numerator
1
>>> denominator
4
>>> type(numerator)
<class 'int'>
>>> type(denominator)
<class 'int'>
>>> q = numerator / denominator
>>> q
0.25
>>> type(q)
<class 'float'>
H. Rounding a float
Python provides a built-in function "round()" to round a float up to a given decimal places. This is useful if you don't need a very accurate number for simplification.
Round off a number up to two decimal places.
>>> a = 7.35514552
>>> type(a)
<class 'float'>
>>> b = round(a, 2)
>>> b
7.36
Top comments (0)