Python and datatypes

Every value has a datatype. But what are these datatypes? Let’s have a closer look.

We are starting our list with three different datatypes:

  • int
  • float
  • complex

These datatypes are used to store numeric values.

Int

Any real whole number (numbers without decimals / fractions) are of the int (integer) class. These numbers can both be positive or negative.

x = 5
y = 10
z = x * y

print(type(x))
print(type(y))
print(type(z))

Running this in your console or code editor will print the following:

<class 'int'>
<class 'int'>
<class 'int'>

Float

Numbers that do have decimals are of the float (floating point) class. Once again, these numbers can be positive or negative.

x = 10.0
y = -1.5
z = x * y

print(type(x))
print(type(y))
print(type(z))

Running this will print the following:

<class 'float'>
<class 'float'>
<class 'float'>

Note that 10.0 is a float, but 10 is an int.

Complex

Any value dealing with imaginary numbers will have the complex class. Imaginary numbers have a ‘j’ behind them.

x = 4j
y = 3 + 6j
z = x * y

print(type(x))
print(type(y))
print(type(z))

Running this will print the following:

<class 'complex'>
<class 'complex'>
<class 'complex'>

1 Response

  1. Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

Leave a Reply

Your email address will not be published.