Python’s real number (float) type
Python uses float
to represent real numbers such as 1.5
, 3.14
, or 2.7
. The name float came from the fact that real numbers are represented in the floating point format in computers.
For example: 3.14
can be represented as 3.14
, 0.314 * 10^1
, or 31.4 * 10^(-1)
. However, you can safely ignore this technical detail and think of a float
just as a real number.
Create a float
We can create a float
variable through an assignment with a real number literal, for example
x = 1.5
print(x)
print(type(x))1.5
<class 'float'>
Note that 5.0
and 5
are of different types
type(5)inttype(5.0)float
Although they are equal in value
5 == 5.0True
Also, 5.
, 5.0
, and 5.000
are all the same
print(5.)
print(5.0)
print(5.000)5.0
5.0
5.0
Similarly, 0.5
and .5
are the same
print(0.5)
print(.5)0.5
0.5
We can also create a float
variable through an assignment with an expression that produces a float
, for example
1.5 + 3.75.22 / 30.6666666666666666
Another way to create a float
is to use scientific notation
# One thousand
1e31000.0# Ten million
10e610000000.0# 1.23 billion
1.23e91230000000.0
When use float?
For storing real numbers in general. In many applications, we often treat 5.0
(float
) and 5
(int
) as if they are the same.
Typecasting
We use float()
function to convert values from other types to float
. However, not all values can be converted to float
(only float-like values can).
Example 1: values that can be converted to float
From bool
# True becomes 1.0
float(True)1.0# False becomes 0.0
float(False)0.0
From int
# Just change the type
float(12)12.0
From str
# Strings that look like a float
x = float("10.5")
print(x)
print(type(x))10.5
<class 'float'># White spaces at two ends are ignored
x = float(" -10.5 ")
print(x)
print(type(x))-10.5
<class 'float'># Leading zeros are also ignored
x = float(" -00010 ")
print(x)
print(type(x))-10.0
<class 'float'>
Operations on float
Arithmetic operations
Same as with integers. However, we cannot perform //
and %
on float
values, because they are only mathematically meaningful for integers.
Here are some simple examples
# Float plus float yields float
1.5 + 3.75.2# Int plus float yields float
# result is promoted to a more general type
5 + 1.06.0# Compute square root
# by raising to power 0.5
25**(0.5)5.0# Rounding
round(10 / 3, 3)3.333# Get absolute values
print(abs(10.5))
print(abs(-10.5))10.5
10.5
Comparision operations
Comparisons on numbers (int
or float
) always return a bool
. (Already covered in the previous chapter)
Math utilities
Note: this section applies to numbers, thus to both
int
andfloat
.
The Python standard libraries come with a module named math
containing utilities for dealing with numbers. You will learn more about modules later. For now, just follow some simple examples.
To use math
module, we need to import it. Then use the syntax math.<name_of_function>
to call the function we want to use.
# Import module math
import math
Ceiling function — round UP to the nearest integer.
(if you are not sure, get help using ?
such as ?math.ceil
)
# With positive value
math.ceil(3.333)4# With negative value
math.ceil(-3.333)-3
Floor function — round DOWN to the nearest integer.
# With positive value
math.floor(3.333)3# With negative value
math.floor(-3.333)-4
math
also provides useful constants, for example
# Constant pi
math.pi3.141592653589793# Constant e
math.e2.718281828459045
Trigonometry functions
# Sin pi/2 => 1
math.sin(math.pi / 2)1.0# Cos pi / 2 => 0
# Note that the result is not 0
# but very close (due to approximation error)
math.cos(math.pi / 2)6.123233995736766e-17
For the full list of what you can do with math
, visit its official documentation page at https://docs.python.org/3/library/math.html
However, strings like "1,000,000.123"
(contains ,
) are not valid. Try it yourself to see the error.
Practice
Ex 1
Do the following
- Create
float
variablex
with value100
- Show its value and type
Ex 2
Do the following
- Create a variable
x
from the division of10
by5
- Show the value and type of
x
Ex 3
How to quickly create a value of 7.5
billion?
Ex 4
Use math
module to compute
- The area of a circle with a radius
5
- The hypotenuse of a right triangle with
3
and4
being the lengths of the two other sides
Navigation
Previous: Python’s integer (int) type
Next: Python’s list type (part 1) — Indexing and slicing