Python Numeric Types

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
We’ll cover more advanced calculations later in the Python Arithmetic Operators lesson.

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(2.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)

Will give you this:

<class 'float'>

Now we’ll convert it to an integer:

>>> my_int = int(my_float)

Print it to see the result:

>>> print(my_int)

You’ll see:

1

And when we look at the type:

>>> type(my_int)

You get this:

<class 'int'>

As you can see above, Python gets rid of everything after the decimal point.

Sign up for FREE to Continue Reading

Here's what you'll get:

  • Full Access to this lesson + 392 more.
  • Learn CCNA, CCNP and CCIE R&S. Explained as simple as possible.
  • Unlock Access to 424 lessons by becoming a member.
  • New Lessons Added Regularly. You'll Be the First to Know.
  • Content created by Rene Molenaar (CCIE #41726)

Takes 1 minute - No credit card needed


Forum Replies

  1. Avatar for pradyumnayadavgla pradyumnayadavgla says:

    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:

    type(my_integer) =  type(1.9)
    my_int = int(1.9)
    type(my_int) = type (int(1.9))
    

    so as per you it shows that class is integer(what integer) and How and how it get rid of decimal values?

  2. Avatar for lagapidis lagapidis says:

    Hello Pradyumna

    The code in this particular example is as follows:

    my_integer = 1.9

    The above code creates a variable called my_integer and 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_integer is. You can see this returns the following text: <class ‘float’> so it is verified that the my_integer variable is of float type.

    my_int = int(my_integer)

    The above code takes the my_integer variabl

    ... Continue reading in our forum

  3. Avatar for davidilles davidilles says:

    Hello, everyone.

    Just a short question, in this code snippet

    https://forum.networklessons.com

    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

  4. Avatar for lagapidis lagapidis says:

    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

Ask a question or join the discussion by visiting our Community Forum