Skip to content

Data Types

Data Types

Python has many useful built-in data types. Python variables can store different types of data. A variable's data type is created dynamically, without the need to explicitly define a data type when the variable is created. It is useful for problem solvers to understand a couple of Python's core data types in order to write well-constructed code. This section details of a few different data types in Python.

Python's type() function is used to investigate a variable or object's type. The syntax to use the type() function is below:

>>> type(variable)

Integers

Integers are one of the Python data types. An integer is a whole number, negative, positive or zero. In Python, integer variables are defined by assigning a whole number to a variable. Python's type() function can be used to investage is a variable is assigned an integer.

>>> a = 5
>>> type(a)
<class 'int'>
>>> b = -2
>>> type(b)
<class 'int'>
>>> z = 0
>>> type(z)
<class 'int'>

Floating Point Numbers

Floating point numbers or floats are another Python data type. Floats are decimals, positive, negative and zero. In Python, floats are defined when a number contains a decimal point . Floats can also be defined by typing numbers in scientific notation which contain exponents. Both a lower case e or an upper case E work to define floats in scientific notation.

>>> c = 6.2
>>> type(c)
<class 'float'>
>>> d = -0.03
>>> type(d)
<class 'float'>
>>> Na = 6.02e23
>>> Na
6.02e+23
>>> type(Na)
<class 'float'>

To define a variable is a float instead of an integer, even if it is assigned a whole number, a trailing decimal point . is used. Note the difference when a decimal point . comes after a whole number:

>>> g = 5
>>> type(g)
<class 'int'>
>>> g = 5.
>>> type(g)
<class 'float'>

Strings

Strings are sequences of letters, numbers, symbols, and spaces. In Python, strings can be almost any length and can contain spaces. String variables are assigned in Python using quotation marks ' '. Strings can contain blank spaces. A blank space is a valid string character in Python string.

>>> string = 'z'
>>>> type(string)
<class 'str'>

>>> string = 'Engineers'
>>> type(string)
<class 'str'>

Numbers as Strings

Numbers and decimals can be defined as strings too. If a decimal number is defined using quotes ' ', the number is saved as a string rather than as a float. Integers defined using quotes become strings as well.

>>> num = '5.2'
>>> type(num)
<class 'str'>

>>> num = '2'
>>> type(num)
<class 'str'>

Boolean

The boolean data type is either True or False. In Python, boolean variables are defined by the True and False keywords.

>>> a = True
>>> type(a)
<class 'bool'>

>>> b = False
>>> type(b)
<class 'bool'>

Note that True and False must have an Upper Case first letter. Using a lowercase true returns an error.

>> c = true
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'true' is not defined
d = false
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'false' is not defined