What Are the Different Python Data Types?

Eric Kleppen, Contributor to the CareerFoundry blog

Python remains one of the most popular programming languages in the world since it is easy to learn, flexible, powerful, and has a fantastic community. It is the go-to programming language for people learning about analytics and data science because of its core analytics capabilities and the thousands of community-driven libraries. Before getting into the complexities of data analysis, visuzaliations and machine learning, it’s best to develop a foundational knowledge of the programming language and understand the Python data types. 

In this article we’re going to cover the different Python data types. No matter your goal, understanding Python data types will act as the first layer of foundational knowledge you need to master Python programming. We’ll define the following Python data types and look at examples using Python code. 

Use the clickable menu to skip ahead to any section: 

  1. What is a Python data type?
  2. What are the different Python data types?
  3. How to learn more about Python
  4. Summary

All examples are done using Python 3.6 or higher. If you’re new to Python, I recommend checking out the Anaconda installation, which will get you set up with many useful packages like Jupyter notebook. Now, let’s get started!

1. What is a Python data type?

Python data types are built into the programming language. At the most basic level, a Python data type is metadata, or a categorization, about a variable that defines what operations can be performed on the variable. Under the hood, data types are Python classes, and variables are the objects of the classes. We can use the type() function in Python to see a variable’s data type. 

While it’s beyond the scope of this article, and not required to understand Python data types, if you’re not familiar with classes and objects, I recommend reading this article on Object Oriented Programming to get you started with the concepts.

2. What are the different Python data types?

Now, let’s get into the different Python data types. For this article, we’ll look at the boolean,  numeric, sequence, set, and dictionary data types. 

Python data types: Boolean

The boolean data type in Python is based on boolean logic and is used to evaluate whether something is true or false. In Python, we must use capital T for True and capital F for False when utilizing the boolean data type.  

#Examples of Boolean data type. Boolean is either True or False.
print(type(True))
print(type(False))

#Boolean data type can return when evaluating an expression too.
var_bool = 4==4
print(type(var_bool))

Whether or not 4 equals 4 is True or False, since our code is evaluating one variable with another in a way that can only produce an answer of True or False, the resulting data type of the variable assigned to the expression is a bool. 

The output for each example will be <class ‘bool’> indicating the data is an object of the bool class. 

Boolean Python data types takeaways

  • The boolean data type represents True or False values
  • Expressions that result in a variable being true or false will be assigned a bool data type.

Python data types: Numeric 

There are three numeric data types in Python that are used to categorize variables that have a numeric value. Numeric data types include int, float, or complex

Int

The Python data type int represents integers, which are positive or negative whole numbers that don’t include decimals. Let’s look at a few different int values using Python. 

#Examples of the Integer Data type.
print(type(2))

#int values can be very long.
print(type(648165234234234234363466184))

#int values can be assigned to variables
var = 5489
print(type(var))

The output for each example will be <class ‘int’> indicating the data is an object of the integer class.

Float

The Python data type float represents a real number with a floating point (decimal place). The float class can make use of the character ‘e’ to represent scientific notation. 

#Examples of the Float Data type.
print(type(2.58))

#float values can use e for scientific notation.
var_float = 578.34e10
print(type(var_float))

The output for each example will be <class ‘float’>  indicating the data is an object of the float class.

Complex

The Python data type complex is used to represent imaginary numbers and is returned by assigning ‘j’ to an integer or float value. 

#Examples of the complex Data type.
print(type(4j))

#float values can also be used in complex numeric data types
var_complex = 45 + 5.5j
print(type(var_complex))

The output for each example will be <class ‘complex’>  indicating the data is an object of the complex class.

Numeric Python data types takeaways:

  • The three numeric data types are int, float, and complex.
  • Floats can utilize scientific notation by assigning e to the float value.
  • Complex numbers are represented by assigning j to the int or float value.

Python data types: Sequence

The Python data types that represent sequences are str, list, and tuple. These data types are sequences because they represent an indexed, ordered collection of various data types. When you start getting into more advanced Python content, you’ll quickly see how important it is to understand how to manipulate sequences of data.

Str

The Python data type str represents strings, which are arrays of unicode characters. In Python, strings are one or more characters within single quotes, double quotes, or triple quotes. 

#Example of string (str) data types. 
print(type('single quotes'))
print(type("double quotes"))
print(type('''triple quotes'''))

The output for each example will be <class ‘str’>  indicating the data is an object of the string class.

Strings have an index, meaning it’s possible to call a particular position within the string. In Python, indexes always start with 0, not 1. To select a position, use brackets [] and the index number. For example, we’d find the second character of the string using [1]. Let’s look at some code examples to illustrate this concept.

We can access a particular position in the string by calling its index value.

string = 'CareerFoundry.com rocks!'

#this will return the 3rd character in a string.
print(string[2])

#A range of values can be returned. This is called a substring.
print(string[0:6])

#Negative numbers can be used to return the end of the string first. 
#-1 would return the last character in the string
print(string[-1])

#You can even print a string in reverse!
print(string[::-1])

The output for each example is r, Career, !, and !skcor moc.yrdnuoFreeraC. Understanding how to manipulate strings is very important for data analysis and data science because so much data is text.

List

The Python data type list is represented by a sequence of data inside brackets [] and is mutable. You can also use the list() function to transform objects into a list. The data in the list do not need to be the same data types. Lists are used heavily in Python and can be used efficiently, transforming an operation that would sometimes require multiple lines of code into only 1 line using a list comprehension

#Example of Python data type list
print(type([1,3,'g', 5, 'z']))

#you can have lists within lists!
print(type(['a', 'b', 3, [123, 9, 2], 'y']))

The output for each example will be <class ‘list’>  indicating the data is an object of the list class.

Just like strings, lists have indexes. When a list contains another iterable object, a second set of brackets with the index number can be used to access the sub-item. 

#lists have indexes
var_list = ['a', 'b', 3, [123, 9, 2], 'y']
print(var_list[0])

#The sublist index can be accessed with an additional []
print(var_list[3][1])

The output of each example will be a and 9. In the second example, we get 9 because we’re accessing index value 3, which is a list within a list. Then we’re accessing the item at index value 1, which is 9. Remember that indexes always start with 0 in Python.  

Tuple

The Python data type tuple is similar to a list in that it is an ordered sequence of objects. The differences are a tuple is represented using parenthesis () and it is immutable. That means tuples cannot be changed after they are created. You can also use the tuble() function to transform objects into a tuple. 

#Example of Python data type tuple
print(type((1,3,'g', 5, 'z')))

#You can have tuples within tuples!
print(type(('a', 'b', 3, (123, 9, 2), 'y')))

The output for each example will be <class ‘tuple’>  indicating the data is an object of the tuple class. Just like you can do with strings and lists, items within the tuple can be selected by referring to the index number. 

Sequence Python data types takeaways:

  • The Python data types that represent sequences are str, list, and tuple.
  • Sequences are ordered collections.
  • Strings (str) are sequences of characters inside singel, double, or triple quotes.
  • Lists are sequences of objects inside brackets [] and are mutable.
  • Tuples are sequences of objects inside parenthesis () and are immutable.

Python data types: Set

Sets in Python represent a distinct, unordered collection of objects. That means every item in a set is a unique value. Since sets are unordered, they have no index. To create a set Python data type, use the set() function, putting an iterable like a list inside the function. 

#Example of Python data type set
var_set_list = [1,1,2,3,4,4,5,'a','a','b','c']
var_set = set(var_set_list)

print(type(var_set))

The output for the example will be <class ‘set’>   indicating the data is an object of the set class.

Notice we assign a list with several duplicate values to the variable var_set_list, and then transform the variable into a set using the set() function. If we print the set, you might notice something unusual.

print(var_set)

The output might look something like this {1, 2, 3, 4, 5, ‘c’, ‘b’, ‘a’}. Notice the values aren’t in the same order as they were in the list. That’s because sets do not guarantee the data remain in any particular order. 

Set Python data type takeaways

  • Sets are collections of distinct objects and are unordered. 
  • Create a set by using the set() function, wrapping an iterable collection like a list. 
  • Sets have no index.

Python data types: Dictionary

As you get into more advanced programming, you’ll see the dict Python data type is used a lot. A dictionary holds data in key:value pairs, and is considered an unordered collection. That means it doesn’t have an index. A dictionary can be created by placing the key:value pair inside curly brackets {} or by using the dict() function. Remember to use a colon to separate the key from the value, and use commas to separate each pair from one another. 

#Example of Python data type dictionary
var_dictionary = {'person1':'Eric', 'person2':'Kristen'}

print(type(var_dictionary))

The output for the example will be <class ‘dict’>   indicating the data is an object of the dictionary class.

There are several ways to access items in a dictionary. We can use the keys() function to output all of the keys.

print(var_dictionary.keys())

We can use the values() function to output a list of values. 

print(var_dictionary.values())

Since the dictionary has no index, you can access a specific value by passing the key to the dictionary variable similar to how you’d use an index number to look up an item in a list. 

print(var_dictionary['person1'])

print(var_dictionary['person2'])

Dictionary Python data type takeaways:

  • The dict data type stores a collection of objects as key:value pairs. 
  • Dictionaries are unordered and don’t have indexes. 
  • Return a list of keys using the keys() function.
  • Return a list of values using the values() function.
  • Return a specific value by calling a key from the dictionary like you would an item’s index in a list. 

3. How to learn more about Python

Python is a fantastic and versatile programming language, and understanding the Python data types is only the beginning. If you want a book that can introduce you to basic concepts while showing you how to code some useful projects, I recommend checking out the book Automating the Boring Stuff with Python

Otherwise, if you’re looking to quickly advance your Python programming skills and want to get into data analytics and learn Python under the guidance of a mentor and tutor, a data analytics bootcamp with Python modules would be a good place to start.

4. Summary

There are several different Python data types you’ll need to master as you begin your journey into Python programming. 

  • Booleans allow you to evaluate whether an expression is true or false. 
  • The numeric data types represent things like whole numbers, decimals, and even complex numbers. 
  • The sequence data types are used to represent things like strings, lists and tuples. 
  • Sets are a useful data type when you need a distinct collection of values, and 
  • Dictionaries are useful when you need to map values in a key:value pair. 

Remember that learning Python is a marathon, not a sprint, and that laying a solid foundation is necessary before jumping into complex and fun topics like machine learning. 

Want to learn more about Python and its data types? Why not try out our free, 5-day data analytics course? You may also be interested in the following articles:

What You Should Do Now

  1. Get a hands-on introduction to data analytics and carry out your first analysis with our free, self-paced Data Analytics Short Course.

  2. Take part in one of our FREE live online data analytics events with industry experts.

  3. Talk to a program advisor to discuss career change and find out what it takes to become a qualified data analyst in just 4-7 months—complete with a job guarantee.

  4. This month, we’re offering the first 100 students reduced tuition—worth up to $1,120 off our full Data Analytics Program. To secure your spot, book an advisor call today.

What is CareerFoundry?

CareerFoundry is an online school for people looking to switch to a rewarding career in tech. Select a program, get paired with an expert mentor and tutor, and become a job-ready designer, developer, or analyst from scratch, or your money back.

Learn more about our programs