Lesson Contents
Python supports multiple data types, including a variety of different numeric data types:
- Integer
- Float
- Complex
Let’s look at some examples. Open Python so it shows the interpreter:
>>>
We’ll run the examples there.
Integer
An integer is a positive or negative whole number without a decimal point. For example:
>>> 1
Will result in this output:
1
Above, we see an integer with positive value 1. You can also have negative integers:
>>> -5
Will result in this output:
-5
Above, we see an integer with negative value 5. We can see that this is an integer by using the type function:
>>> type(-5)
Python tells us this is an integer:
<class 'int'>
We can also calculate with integers. For example:
>>> 5-2
Will result in this output:
3
Float
A float is used to represent real numbers, that is, numbers with a decimal point. For example:
>>> 2.0
Will result in this output:
2.0
How do we know this is a float? Let’s try the type function again:
>>> type(4.0)
Will result in this output:
<class 'float'>
This tells us this is a float. We can also calculate with floats:
>>> 5.0 / 1.3
Will result in this output:
3.846153846153846
When you calculate with integers, you might end up with a float:
>>> 9 / 2
Will result in this output:
4.5
Complex
A complex number is a combination of a real number and an imaginary number. Complex numbers are used in scientific, geometry, or calculus calculations. I never needed these in any of my scripts but for the sake of completeness, I’ll briefly mention them. Here’s an example in Python:
>>> type(3.14j)
Will result in this output:
<class 'complex'>
Conversion
You can also convert between the different numeric types. This can be useful when you have a float and want to get rid of everything after the decimal. Here’s an example of how we can convert a float into an integer:
>>> my_float = 1.9
>>> type(my_float)
>>> my_int = int(my_float)
>>> print(my_int)
>>> type(my_int)
Will result in this output:
<class 'float'>
1
<class 'int'>
As you can see above, Python gets rid of everything after the decimal point.
Hi Rene,
In given eg,
my_integer = 1.9
type(my_integer)
my_int = int(my_integer)
type(my_int)
i could not understand the output of last two commands , as per me it will be like this:
so as per you it shows that class is integer(what integer) and How and how it get rid of decimal values?
Hello Pradyumna
The code in this particular example is as follows:
my_integer = 1.9The above code creates a variable called
my_integerand assigns it a value of 1.9. This will automatically make this variable a float type because it is not an integer, but a real number.type(my_integer)This code causes output that indicates the type of variable that
my_integeris. You can see this returns the following text: <class ‘float’> so it is verified that themy_integervariable is of float type.my_int = int(my_integer)The above code takes the
... Continue reading in our forummy_integervariablHello, everyone.
Just a short question, in this code snippet
https://cdn-forum.networklessons.com/uploads/default/original/2X/4/4f4f07cbdc61aa89914a96e90cc7c48d500a1dd3.png
Which part of the number here is the real part and which one is the imaginary? I am not quite sure what the j at the end defines.
Thank you.
David
Hello David
In Python, complex numbers are represented in the form a + bj, where a is the real part and b is the imaginary part. The j suffix explicitly denotes the imaginary unit.
I hope this has been helpful!
Laz