6.6. Data types

6.6.1. Overview

Behind the scenes, computers work in binary. Everything is stored as either a 0 or a 1. Many of the details of this are not important for this course, you’ll go into detail on binary in your digital electronics courses. However, we always need to remember this, as it shapes how and why computers and programs work the way they do.

For our programs, we need a way of going between the 0’s and a 1’s that a computer works with, and the data a human works with. We’re used to dealing with numbers (e.g. 0, 8, 22.3), or text (e.g. “hello”), or dates (05.11.1955), and many other formats.

To do this, there are two things built in to our programming. Firstly, there is an agreed on standard. If we all agree that 01001101 represents the letter M, then we can work with that. There are well established standards for how computers represent common types of data (e.g. IEEE/ISO/IEC 60559 for storing numbers that have decimal points). In general, we won’t worry about these in this course. The computer knows the standard for us, and we only need to go into a little depth on the following pages.

Secondly, each thing we store in our code, whether via a variable or in an object, is given a data type. The program is given some additional information to help it know what kind of information is being kept. For example, in Python, if you have

name = "alex"

the double quotes " help indicate that this is storing text.

6.6.2. Common data types

There are a number of common data types, and we’ll introduce the properties of some important ones on the following pages. It’s not necessary to know all of the details of these, but it is important to have some understanding of how our data is being used by the computer under-the-hood. Having some familiarity will help you debug when you have issues with your data.

6.6.3. Dynamically typed vs. statically typed

In general, Python is dynamically typed. That means, it looks at our code for us, and automatically works out which data type to use and this can change on-the-fly. In contrast, Rust is statically typed. We have to explicitly say what data type to use for a piece of data. For our purposes, you may see weakly typed and strongly typed being used in place of dynamic and static.

Being statically typed makes it more work for the programmer, as you have to enter this information each time you define a new variable (and think about what you want the data type to be!). The resulting program can then run faster, as the computer doesn’t have to figure out the data type for you. It can also help avoid mistakes. If you try and put, say, text in a variable expecting a number, this is easy for the computer to automatically spot that there’a bug in the code.