Anyone Can Code.
There are three numeric types in Python:
int
- integer
float
- floating point number
complex
- complex number
Integers are positive and negative whole numbers, just like those you would see in math. They can be
assigned to a variable easily. For example, we have
x = 5
x
and an integer value of 5. Therefore
the object is of int
type.
Under integers, we have a subdivision of Boolean values. Boolean values are True
and
False
, which have their
respective integer counterparts as 1
and 0
. Boolean values can be used to evaluate
any expression in Python through comparison. Conditional statements in Python also return boolean values and
other actions can be taken on the basis of the returned value.
print(10 > 9) print(10 < 9)
True False
Floats are floating point numbers. They are positive and negative decimal values. Now, while floats
are very accurate, after a certain decimal place, they are not.
In the Python Docs this problem is
stated as follows.
Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine.
If you are not familiar with binary numbers, a binary number system consists of only 0s and 1s. If you want to see how you can convert binary numbers to decimal numbers you can check this link out.
To assign a float value to a variable, you would do the same thing as that for an integer. You can also use
scientific notation:
y = 9.34e4
If you run print(y)
, the output should be 93400.0
. Even if there are no decimal
places, a float will always have a decimal point.
Complex numbers are written with 'j' as an imaginary part. If you don't already know what complex numbers are, they consist of a real number component and imaginary number component. In mathematics, j would be replaced by i which is also known as iota and is the square root of negative one.
In Python, you simply use a j instead of an i. For example the complex number 3 + 4i would be written
as 3 + 4j
in Python.
To get the real part of a complex number, we use the real
method (a method performs an
operation on an object). We can use imag
to get the
imaginary part as well.
import math y = 3 + 4j print(y.real,y.imag)
3.0 4.0
These return float values of each component. Here you see that we have used the statement import
math
. This is used to import the code inside the math module
file here. Essentially, it allows us to use functions created there. You will learn more about all
this in our functions tutorial. For now think of it as us borrowing an operation
from another file.
Python provides built-in functions to convert between number values. The three functions are
int()
,float
and complex
. Let's see how they can be used.
a = 3 b = 4.0 c = 2 + 5j print(int(b),int(c)) print(float(a),float(c)) print(complex(a),complex(b))
4 3.0 (3+0j) (4+0j)
You can convert int
and float
to complex
, but the vice-versa will
not work and will produce an error.
That's it for this tutorial! Click Next to access the next tutorial!
This site uses cookies to improve your experience as well as for site metrics. To see more, check out our Privacy Policy