Python Data Types

Jakob Jenkov
Last update: 2024-04-08

The values referenced by Python variables all have a data type. The variable itself may not be locked to a specific data type, but the value it points to will have a data type. Python comes with a set of built-in core Python data types. These data types are:

  • Boolean
  • Numeric
  • String
  • List

Python data types allow you to perform certain data type specific operations on each data type. Thus, it is good to know the built-in core Python data types.

Each of these Python data types are covered in more detail below, and some of them possibly even in more detail in their own tutorials.

Python Boolean

The boolean (bool) Python data type represents a value that is either True or False. We know this from a bit which can be either 1 or 0, where 1 corresponds to True and 0 corresponds to False.

Here are two examples of setting a Python variable to a boolean value:

mybool_1 = True

mybool_2 = False

Python Numeric

The numeric Python data type represents a numeric value. A numeric value in Python can be either an integer, a floating point or a complex number.

Here are two examples setting a Python variable to an integer value and a floating point value:

myint = 123

myfloat = 123.45

Python String

The string Python data type represents a textual value. A string in Python can contain Unicode characters.

Here is an example of setting a Python variable to a string value:

mystr = "This is a text in Unicode"

Python also supports multi-line strings. Here is an example of setting a Python variable to a multi-line string value:

my_ml_str = '''This is
               a multi-line
               string in Python
            '''

Python List

The list Python data type represents a list of elements, such as a list of values or objects. The elements stored in a Python list are stored in sequential order, and can be accessed in that same sequential order. Each element in a Python list has an index which is the element's position in the sequence of elements in the list. The list element indexing starts from 0. You can access an element in a Python list directly via its index.

Here is an example of creating a list in Python, and accessing its elements via their indexes:

mylist = ["Joe", "Mehdi", "Miranda"]

joe = mylist[0]
mehdi = mylist[1]
miranda = mylist[2]

Python Tuple

The tuple Python data type represents an immutable list of values or objects. A Python tuple is thus very similar to a Python list - with the exception that a tuple cannot add, replace or remove elements.

Here is an example of creating a Python tuple:

mytuple = (1,3,4, "text")

Notice the parentheses around the values in the tuple declaration. This is different from list declarations which use square brackets.

You index into a Python tuple in the same way you index into a Python list. Here is an example of accessing elements in a Python tuple via their indexes:

mytuple = ("Joe", "Mehdi", "Miranda")

joe = mytuple[0]
mehdi = mytuple[1]
miranda = mytuple[2]

Python Set

The set Python data type represents a set of unique elements, meaning each element only occurs once in the set. In a list of tuple, the same element could potentially occur more than once.

Here are some examples of creating a Python set:

myset1 = set()

myset2 = set("one element")

myset3 = set(["one", "two", "three"])

myset4 = set([1, 2, "three", "four"])

Notice the call to the standard Python method set() in order to create a Python set.

The order of the elements in the set is not guaranteed (undefined), so you cannot index into the set like you can with a tuple or list.

Python Dictionary

The dictionary Python data type represents a set of (key,value) pairs. You use the key to both store and retrieve the corresponding value in the dictionary.

Here is an example of creating an Python dictionary and inserting values into it:

mydict1 = {}

mydict2 = { "key1" : "value1", "key2" : 123 }

You can access the elements of a Python dictionary like this:

mydict2 = { "key1" : "value1", "key2" : 123 }

myvalue = mydict2["key2"]

Data Types in Other Languages

For the curious reader, here are links to tutorials about data types in all the programming languages covered here at Jenkov.com:

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next