Sitemap

Learn Python Data Types

6 min readFeb 5, 2023

--

One of the first steps in learning to code is learning data types. Programming languages have different data types to store different kinds and structures of data. Variables “store” this data — this is in quotes because variables actually point to the location where data is stored but this isn’t important to understand when you’re getting started (or honestly ever if you just don’t care to know).

All of these different data types may seem like a lot of information to take in, but after going through this tutorial, hopefully you’ll feel more confident in navigating these on your own. Enough with the subpar intro, let’s get into it.

Press enter or click to view image in full size
a table listing each data type (string, integer, float, complex, list, tuple, set, dictionary) and corresponding python keywords and descriptions

Strings (str)

The string data type holds sequences of characters (letters, numbers, special characters, spaces).

screenshot of code with three variables set to string values

Here we have three variables all with a string value. Each character has a designated index denoting its placement in the string. Indices start at zero. For example, the zeroth index of my_string is “h” and can be accessed by typing my_string[0].

Integers (int), Floats (float), and Complex Numbers (complex)

The integer data type holds positive or negative whole numbers.

Three variables with integers assigned to them

The float type holds positive or negative numeric values with decimal points.

Three variables with numeric values containing decimals

The complex type holds numeric values that contain a real part and an imaginary part (I’ve personally never used these as work involving imaginary numbers is not common) like -1 + 2j.

Get Sam Marsman’s stories in your inbox

Join Medium for free to get updates from this writer.

int(), float(), and complex() each convert the argument that’s passed in to the data type associated with the method.

conversion of data to types int, float, and complex in the terminal

In the first statement, the string is converted into an integer. In the second, the rational number is converted into a float. In the last, the first argument is converted to the real part of the complex number and the second is converted into the imaginary part. Again, don’t worry about complex numbers right now — I forgot they exist until I started writing this.

Boolean (bool)

The boolean type can be one of two values — True or False. True and False are built-in Python values. bool() returns True or False depending on the value of the given argument. Non-empty strings, non-zero numerics, and other non-empty data types equate to True. You can also pass in equivalence statements as in the first two examples below.

The result of three boolean operations.

Lists (list)

Lists are similar to strings in that they are sequential data types. They contain indices and use the same syntax to access the data at specified indices.

Press enter or click to view image in full size
Three variables each with a list assigned to them.

Lists can contain any data type (yes, even another list!) and are not limited to one data type in a list . The data is ordered — meaning that an index is associated with an element of the list. We can access an item like this: my_list_1[1] which will return ate. The indexing can be confusing because it starts at 0 instead of 1.

Press enter or click to view image in full size
A list with the indices noted

Indices can be positive or negative. The beginning of a list is always index 0 and the end of a list is always index -1.

The list() method accepts one argument and converts the data to a list.

Tuples (tuple)

Tuples, tuples, tuples. What can I say about tuples? Well, tuples are an ordered collection of data that can contain multiple data types — like lists — but unlike lists, they cannot be changed (also known as being immutable) once they’re created.

Two variables both with a tuple assigned to them

Tuples are contained in parenthesis. If you’re creating a single-element tuple, there must be a comma after the element.

An example of a list being cast as a tuple and a string being cast as a tuple

Tuples are indexed in the same way as strings and lists.

Sets (set)

A set is a collection of elements like lists and tuples. However, the data is unordered, meaning elements can’t be accessed by an index, and it contains no duplicates.

Press enter or click to view image in full size
Two variables both with a set as the value

Sets are unique for several reasons. They can contain most data types, however, they cannot contain lists, dictionaries, or other sets. There’s a reason for this and it allows for sets to be easily and quickly searchable, but it’s not important for this tutorial. Also, if you try to create a set with two identical elements, only one will be in the set.

Press enter or click to view image in full size
conversion of two lists into sets in the terminal

The second set is a great example of how sets are not ordered. You can see in the list [“a”, 4.0, “eleven”, “1 + 2”] that “eleven” is at the second index, but in the set “eleven” is listed first. The set may return the elements in a different order any time it’s called.

Dictionaries (dict)

We’ve made it to our final data type. Congratulations! I’m so proud of what you’ve accomplished so far. Dictionaries are also unordered collections of data like sets. Their format is a bit different from what we’ve seen so far and resemble, well, dictionaries. Python dictionaries have a key and a value. If we’re going with the English dictionary analogy, the key is like the word and the value is like the definition.

Press enter or click to view image in full size
Three variables each with a dictionary assigned to them

The keys come before the colon, and the values are after. These are called key-value pairs. In my_dict, the key-value pairs are ("one", 1), ("two", 2), and ("three", 3). In the same way you look up a word in the English dictionary, you look up a key in a Python dictionary.

Press enter or click to view image in full size
The initialization of a dictionary followed by the accessing of one of the keys in the terminal

You can see on the second line d["dog"] looks similar to how we call an index in a list, string, or tuple. Instead of an index, we enter a key and it returns the associated value. You can also create dictionaries by passing a list of tuples in dict() where the tuples are key-value pairs.

Press enter or click to view image in full size

The End

Holy shirt, you made it through all of the Python data types! That’s huge! This is a lot of information and might seem a bit overwhelming, but the good news is it’s not going anywhere. There are also other tutorials and exercises you can follow if you just search in your favorite search engine.

You did a lot today! If you didn’t already, put this to practice even if it’s making lists and dictionaries and such just for the sake of it. That’s how we learn! Anyway, I’ll stop bugging you now. Bye bye!

--

--

Sam Marsman
Sam Marsman

Written by Sam Marsman

Data engineer. Stand-up comedian. Interested in comedy, equality in tech, and caramel corn.