Understanding Python Data Types, With Examples

python oops

Understanding Python Data Types, With Examples

Key Points

  • Python has many tools, libraries, and frameworks available for a variety of use cases that make it quite powerful.
  • Python has several data types available to use, including text, numeric, sequence, mapping, boolean, and None.
  • Text data types in Python allow you to communicate with the end user utilizing your software or web application.
  • Numeric data types in Python include integers, floating-point numbers, and complex numbers.
  • Sequence data types in Python, such as lists and tuples, allow you to store multiple pieces of information.

What are Python data types, and why should you care about them? When learning back-end web and software development, many new programmers gravitate toward Python for many reasons. 

One of the most important of these reasons is that Python has many tools, libraries, and frameworks available for a variety of use cases that make it quite powerful. 

These utilities range from graphical user interfaces to web development frameworks, and there are many Python libraries out there that deal with the following tasks a programmer might need to accomplish:

  • Displaying web pages
  • User authentication
  • API development
  • Machine learning
  • Artificial intelligence
  • HTTP requests
  • Mathematical operations
  • . . . and so much more!

But, before you learn how to use these powerful tools, you must first understand how to work with the most basic elements that exist in any programming language — the data types that allow you to perform these more advanced tasks.

Python has several data types available to use, and if you’re looking to set a good foundation for getting started with them, you’ve come to the right place. First, let’s dive into the most commonly used data types to get started.

Text Data in Python

The first on our list of Python data types is also the first you will likely encounter in your programming journey.

As a developer, you will most likely use text data in your programs with every software or application you develop. The main reason for this is that it gives you a way to communicate with the end user utilizing your software or web application.

For this, you will use the data type string. Here is a basic example of how you’d set a string variable and display it in the console:

python data types string

So, let’s dive into what you’re seeing on the screen:

The first line of the code declares the variable that is of string data type. In Python, you don’t need to declare what type of variable this is because it automatically detects it.

However, Python is not a typeless language. It is actually considered to be both strongly typed and dynamically typed.

This means it doesn’t require you to tell it what it is, but what it will matter when it comes to performing certain tasks. For instance, you can’t do division on this variable we created.

There are many operations you can perform on this string, though. Check out the table below for a sampling of what you can do with this text variable:

String OperationDescription
upper()Converts text to all uppercase.
lower()Converts text to all lowercase.
find()Searches text for a specific character and returns the index of where it starts.
count()Returns the number of times a character is repeated in a text variable.
split()Cuts the text off at a certain point and turns it into a list.

Numeric Data Types in Python

Number data types are something you’ll work with in almost any program or application you code. Python has three types of numeric data types available to use:

  • int — positive and negative whole numbers
  • float — positive and negative decimal numbers
  • complex — a combination of positive and negative real and imaginary numbers 

This is how these three types of numbers look in a simple Python program:

Python data types numeric

Like the string in the above example, we didn’t need to explicitly define the type of number we planned to store in the variable. It automatically knows by looking at how we assign its value. Other than that, the first two examples are pretty straightforward, but let’s unpack how complex numbers work in Python.

As you can see in this example, the result we get from the complex number function looks like an algebraic function. Python represents it this way because that’s exactly what it is—it has to have a way to account for the unknown (or imaginary) number.

Like other numerical data types, you can perform a variety of operations on this number. So, if instead of simply printing it, we multiplied it by two, we would get this result:


This type of data is useful when your use case involves scientific or mathematical data, but it is not commonly used in everyday web or software applications. 

Sequence Data Types in Python

Text and numeric variables are easy to work with; however, they have their limitations. Using a sequence type for your data can make it much more versatile because they allow you to store multiple pieces of information.

The first example we’ll look at is a list:

python data types sequence

Our numberList in the above example is created by putting a list of numbers surrounded by brackets. This list has an index that starts at 0 and ends at the last item in the list.

So, outside of this loop we created, if you wanted to access a specific element, you would use this notation:

numberList[2] = 11

Our second example of sequence data types in Python is a tuple, which is similar to a list, but they have a few distinctions:

  • While lists are mutable (able to be changed), items in a tuple are immutable.
  • They are created with parenthesis instead of brackets.
  • Tuples are ideal for larger collections because their performance is a bit better than lists.

Here is a simple example of a tuple in Python:

example of tuple in python

Keeping in mind that tuples are immutable, the following would give us an error when we tried to run the code:

myTuple[2] = “Johnny”

So, this also makes them ideal to work with when you want to protect the data inside them.

Mapping Data in Python

However, let’s say the sequence you want to create is a bit more complicated.

Python has another option for you called a dictionary that allows you to map your data rather than simply create a sequence of items. First, let’s look at how to set a simple one up in Python:

python data types mapping data

First, we set up our tuple by opening and closing the data set with curly braces. This signals Python that we’re setting up a dictionary.

Next, each item in our dictionary is a pair of two items—the first one is called the key, and the second one is called the value. (These are referred to as “key-value pairs.”)

This pair of data is separated by a colon. If you’ve ever worked with APIs and JSON data, then the dictionary will look very familiar to you.

However, if you just print the entire dictionary, it will not be formatted. If we did that, we would see this on the console:

{10001: “John Smith”, 20002: “Karen Morgan”, 30003: “Jack Rabbit”, 40004: “Jessica Crow”}

Since this is unformatted in its raw form, we’ve used Employees.keys() and Employees.values() to access each particular item in the dictionary. This improves readability on the front end.

Boolean Data in Python

Booleans are another data type in Python you will likely use in most of your applications because they give us a simple way to control the flow of execution. We can see how useful this can be in the following code:

python data types boolean example

The first variable we initialized in our code is a boolean variable, which should either be set to True or False. This boolean is useful in determining when we can exit out of the while loop. Without it being accurately initialized and changed within the loop, the code would run infinitely because it wouldn’t know when to stop.

As we go through our while loop, we first print a message to the user to let them know the yourNumber variable is still a positive number. After it prints, the loop checks to see if decrementing changed it to be a negative number.

If it’s still positive, we run through the loop again. However, if it’s negative, the boolean is switched to False, a message is printed, and we successfully exit the loop.

This data type can be interpreted as either off or on, similar to a light switch. Whenever its value changes, we tell the code to do something new that changes the flow of the program. This is also used widely in if statements since if statements always return a true or false value when they’re executed.

None Data Type in Python

You might have occasion to see “None” printed out to the console if you’ve been coding with Python for any amount of time. None is another term for null. A null value does not mean it’s zero, and it does not mean it’s False.

This is often not done on purpose, but the None data type can be used purposefully. Using None as a data type can be useful when you only need to check if something is “falsy,” meaning you don’t need it to be strictly False, but you don’t want it to be completely True either.

Here is an easy example of using None in your code on purpose:

python data types example of none data

Checking for None can also be helpful when you’re testing for unexpected results from your code. For instance, if a function expects a dictionary and you gave it a list, you might receive None back because it didn’t know how to manipulate your data in that format.

In the above code example, we defined a variable with “None” to indicate that we needed to get input from the user, which we implemented with a simple if statement.

Wrapping Up: Python Data Types

Whenever you code with the Python programming language, you will need to use data types to represent it in the proper format.

Whether it’s through strings, numbers, lists, dictionaries, booleans, etc., it’s important to build the foundational knowledge that teaches you how to access and manipulate your data.

Because they each have their own rules and modes of operation, this short overview of data types in Python gets you started in building at least basic programs that will allow you to build your coding skills.

Now, let’s answer some frequently asked questions to help further that knowledge.

Summary Table

Data TypeDescription
TextUsed for communicating with end users through strings.
NumericIncludes integers, floating-point numbers, and complex numbers.
SequenceIncludes lists and tuples for storing multiple pieces of information.
MappingUses dictionaries to map key-value pairs of data.
BooleanControls the flow of execution with True or False values.
NoneRepresents a null value, neither zero nor False.

Frequently Asked Questions

What are the 5 data types Python uses?

The five types of data in Python can be sorted into: Dictionary, Tuple, List, String, and Numbers.

Why isn’t there an array data type in Python?

Because Python’s data types are dynamic, it doesn’t allow for an array to be defined because arrays get defined as specific data types. For instance, in Java, you’d define an array like this:

int[] numArray = {1, 2, 3, 4, 5};

But, since Python doesn’t type its variables that way, it doesn’t have a primitive array data type. However, that doesn’t mean you can’t create an array in Python. This can be accomplished through a variety of libraries available in Python.

Is it possible to convert an integer to a float and a float to an integer?

Python allows for casting number variables into different types, but you should take great care when you do this. Because a float allows for numbers with decimals, it will give you more accuracy if you need more precision. As such, you can lose some data by converting it to an integer.

What’s one reason we should use a list over a tuple when we need to store a sequence of data?

If you expect the data inside your list will need to change during the execution of your code, a list will give you the flexibility you need to accomplish your task.

How do you handle a string with quotes inside it?

You might have a reason once in a while to display some text to the user that has quotation marks around it. There are a couple different ways you can solve this challenge. If your string won’t have any apostrophes, you can use an apostrophe to enclose the whole string. Alternatively, you can use an escape sequence if your string is enclosed in double quotes. The escape sequence for a double quote is \”. A third option would be to use triple double quotes at the beginning and end of the string, like we’ve used at the end of the above code as a multi-line comment.

To top