Python Numeric Types

Python supports a variety of different numeric data types:

  • Integer
  • Float
  • Complex

Let’s look at some examples.

Let’s look at some examples.

Integer

An integer is a positive or negative whole number without a decimal point. For example:

>>> 1
1

What you see above, is an integer with positive value 1. One more example:

>>> -5
-5

Above, we see an integer with negative value 5. We can see that this is an integer by using the type function:

>>> type(1)

We can also calculate with integers:

>>> 1+1
2
>>> 5-2
3
>>> -2*5
-10
>>> 2*3
6

Float

A float is similat to an integer but does have a decimal point. For example:

>>> 1.0
1.0

Let’s try the type function one more time:

>>> type(1.0)
\

This tells us this is a float. We can also calculate with floats:

>>> 1.2 / 5.1
0.23529411764705882

When you calculate with integers, you might end up with a float:

>>> 9/2
4.5

Complex

>>> 3.14j
3.14j

check:

>>> type(3.14j)

Conversion

Sometimes, it can be useful to convert between different numeric types. For example, we can convert a float into an integer:

>>> int(1.0)
1
>>> int(1.9)
1

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


Ask a question or start a discussion by visiting our Community Forum