Photo by Chris Ried on Unsplash

Anyone can code Learn the Basics: Data Types and Data Structures


Max Lloyd

Writes in mostly formal languages but occasionally likes to dabble with the informal


Welcome to part three of this tutorial series, I would recommend going back and reading the previous 2 articles if you haven’t already. This is intended to be more of a standalone tutorial and as such could be revisited to remind you if you need it later.

Programmers use a wide variety of data types and data structures all the time, we use the former because we have to (well not technically but if you really want to try and interpret raw ascii binary be my guest) and we use the latter to solve more complex problems. I have already introduced you to a couple of basic data types already, you may remember the integer (int) or the string (str) being the computers way of distinguishing between a whole number and a word/sentence. Today I intend to introduce you to all the basic data types that Python has to offer so strap in and lets go.


Data Types

In Python we can check the data type of a variable using the type() function, this means that if you put anything inside the brackets of type() Python will tell you what type it is. Try this out in IPython with the examples below. Note: this is not a complete list, there are more data types than I will show you however they are beyond the scope of this guide.


Integer

An integer (int) is simply a whole number, any whole number. So for example 1 or 2 would be an integer whereas 1.2 would not be. In Python you can make an integer of any length, this means that the number 1020228486040293759791037576 would be accepted by Python as an integer and no errors will come your way, this is not necessarily the case for all languages but for us right now that doesn’t matter.


Float

A float is short for floating-point number, this is essentially a number with a decimal point in it. The way that computers store this data is really interesting and I would definitely recommend this Wiki article if maths is your thing. Try typing type(1.7) into IPython and see what happens. It should return float, if not check that you actually put a decimal point in the number.

Floats are obviously very useful for working with decimals but they also have standard form support (for the uninitiated standard form is a scientific number notation, it’s a way of writing really big or really small numbers). You can use standard form by using an e in place of ×10. For example instead of writing 1.989×1030 you would write 1.989e30


String

As I keep saying, a string (str) is fancy programmer talk for word/sentence. In fact you aren’t only restricted to those, you can even mix in a few numbers into a string, or even make a string comprised of only numbers. The defining feature of a string is if it surrounded by quotes, be they single: ‘’ or double: “” it doesn’t matter (not the case for all languages but is the case for Python). So "this would be a string" and so would this → "123456" and this → 'this is a 1234 string'. This may seem annoying or confusing but it is for good reason. User input is usually collected as a string and as such it’s important to be able to store any type of user inputtable data as a string (be that numbers, floats or words), we can then use some simple functions to convert the input to a different data type.


Character

A character (chr) is what it says on the tin, a single character. In Python, no distinction is made between a string and a character. That is to say that a character is just a string with a length of 1. The reason for me bringing this up however is due to it being quite common in other languages and as such it’s useful to know.


Boolean

A boolean (often shortened to bool) is simply a value that is either True or False. I have covered this operator in depth in this article so have a look if you want a deeper dive into logic. The gist of it is, you can use booleans with conditional statements (an if else statement for example - again go have a look if you need reminding) and they can be combined with logical operators to form more complex logic.


Data Structures

A data structure is a collection of data organised in such a way to make it easier to access or to manage. You will be familiar with some of these through real-world usage. One such example would be a list, this would be a collection of data under one name - “shopping list” for example. In programming we have an analogue of this, also called a list, it is a data structure where we can store multiple data items under a single name. There are many more complicated data structures such as graphs and trees but I will not cover these as this is intended as a beginners guide, feel free to have a read if interested however.


List

A list is a collection of data under a single name, you can think of it exactly like you would with a list in the real world. This is probably the most common data structure in programming, used to collect and store data. In Python it is defined like any other variable (click here to remind yourself about those if needed). The key difference being you use square brackets to indicate you are working with a list, the square brackets surround the list items and each item is seperated by a comma. This would look like: nameoflist = ["listItem1", "listitem2", "listitem3"]. In Python we can put any type of data inside a list, we can even put different types in the same list, for example you can have a single list containing ints, floats and strings at the same time, this is not the case for all languages however.

In order to access a certain list item you use square brackets notation, this is where you write square brackets containing the index of the list item you want to access immediately following the name of the list. Maybe an example would make this clearer, remember the list called “nameoflist” that I defined above? Well if I wanted to access the first item in that list I would write nameoflist[0]. You may be wondering why I have asked for the 0th element in the list, this is because programmers count from 0 - so the 0th element of a list is actually the 1st element of the list. In order to access the second element in the list you would then ask for the 1th element (I say oneth to distinguish between 1th and 1st as they aren’t the same thing) so therefore you would write nameoflist[1].

You can add data to a list that already exists by using the append() function, you use this by writing the name of the list followed by .append() with the item you want added to the list inside of the brackets. For example, to add “listitem4” to the list “nameoflist” above you would write nameoflist.append("listitem4"). The resulting list would then look like: ["listItem1", "listitem2", "listitem3", "listitem4"]

Note: you may hear of another list esque data structure called an array, the key difference between an array and a list is that a list can have any number of data items added to it, an array however is of a fixed length and as such can only have a finite amount of data items added to it. Lists are sometimes called dynamic arrays for this reason. Python only has lists so no need to worry about this if you’re only going to be learning Python.


Tuple

A Tuple is similar to a list with the only difference being it is immutable, this means that it cannot be changed (a list is mutable and therefore can be changed - mutable for mutation). These may not seem useful at first but they’re often used for creating pairs of data that you do not want changed at all during the programs execution. The way you define a tuple is by using normal brackets, an example would be: nameoftuple = ("item1", "item2", "item3"). Accessing data from it is exactly the same as a list so to get the first item from this example you would write nameoftuple[0].


Dictionary

A dictionary, also known as a “hash map”, is a collection of key value pairs, this means if you know a certain key you can access its respective data. This is useful for emulating a database in a program without actually needing a database. A good use case of this would be storing test scores of students, you would create a dictionary where the keys are the students’ IDs’ or names and the values are the scores of the respective students. The way you define a dictionary is with curly brackets, you use any immutable datatype as the key followed by a colon and then the data that relates to said key, each key value pair is separated by a comma. This would look like: nameofdictionary = {"key1":"value1", "key2":"value2", "key3":"value3"}. Note: you can use any data type or data structure as the value, you could even nest (nest is programmer terminology for put inside - you can nest lists also) a dictionary inside of another dictionary by making a value of a key another dictionary.

Accessing data inside of a dictionary uses the same notation as a list, square brackets. So, using the example of “nameofdictionary” I defined previously, you would access “value1” by writing nameofdictionary["key1"]. A more concrete example would be student test scores so let’s define another dictionary: testScores = {"Bob":62, "Val":67, "Jeb":77}. If I wanted to find Bob’s test score I would write testScores["Bob"] and Python would return 62. Try this out in IPython by defining the dictionary and fetching data from it.

To add a new key-value pair to a dictionary you write the name of the dictionary followed by square brackets containing the new key that you want to add, followed by an equals sign, followed by the new value you want to assign to that key. For example to add a new key and value to the dictionary “nameofdictionary” I defined above you would write nameofdictionary["key4"] = "value4". The resulting dictionary would then look like: {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4"}



I may cover some of the more complex data structures in a later guide but for now this should be enough for you to get started. Apologies if this was more of a theory based article but that’s just the nature of this topic, the next guide should get you writing some more code. Regardless, the information here is all useful for programming so make sure you at least remember the data types and the list data structure, they will be the most important for later posts. Feedback on these can be sent to the email in the footer, any and all of it is appreciated :)

Comments