Re-introduction to Python - part 1. The power of labeling pieces of data.
In programming we work with data. Computers have specific ways of storing and representing different kinds of data in the computer memory. For example, numbers are stored in a certain way, text is stored in a different way, dates and times are stored in yet another way. These different kinds of data are called "data types" and while it is true they are all just 1s and 0s underneath, it turns out to be very helpful to treat different types of data differently, because the type contains valuable information about what should and shouldn't be done with certain data.
Programming languages usually have built-in support for certain fundamental data types, as well as ways for combining these into more complex structures of data. In this way, basic data types serve as building blocks for more sophisticated software.
Most of what we do in programming comes down to transforming data in various ways by applying series of operations on it. Each data type defines not only how that kind of data is stored, but also the operations which can be performed on elements of that data type. For example, numbers can be added together, subtracted, multiplied. Sequences of text (called "strings") can be connected together ("concatenated"), split into the individual characters that make them up, transformed from lower case to UPPER CASE, and so on.
Moreover, there are operations for turning one type of data into another. A date, for instance, can be turned into a string by formatting it in a specific convention, e.g. "19 April 2021" or "04/19/2021". A numeric value can be extracted ("parsed") out of a text string, and so on.
You can think of a computer programme as a series of transformations. The data we have at the beginning is called "input". The data we get at the end is called "output".
To make it easier to handle the various pieces of data in a programme, we can label them with names. Like in mathematics, we can use the name "x" for example to label a numeric value. We call these "variables", because their values can change. In Python we assign a value to a variable like this:
x = 25
Here, x
is the name of the variable and its value is whatever number we decide to assign to it - in this case 25. If we change our mind later, or if we wish to re-use the name "x" for a different variable, we can simply do another similar assignment and the old value will be forgotten. In a typical programme, we would use many different variables and would assign different pieces of data to them, depending on the task at hand. The names themselves are meaningless to the computer, but it is good practice to use names which hint at the reason we needed to use a variable in the first place. For example, if we were describing someone's age, it would be better to call our variable age
rather than x
.
Because the value of our variable is an integer number, we say that "the type of x is integer" (or, in Python, just int
). Now we can apply to x
any of the operations which can be applied to integers - regardless of its actual value. E.g. we can add 10 to it.
We call this technique "abstraction". It gives us the ability to talk about an "abstract" integer "x", without caring about its specific value. This is one of the most powerful tools for reasoning (in mathematics) and for manipulating data (in computer programming).
Variables don't have to contain numbers - they can contain values of any data type supported by our programming language. We can even combine them together into composite data types. In Python, we can have a "list" of items, like [1, 2, 3, 4, 5]
or [x, y, z]
. Each item in the list can be of any data type, and we can mix and match to our heart's content. Because the list is its own (composite) data type, we can apply certain operations to it. E.g., we can ask for the "length" of the list (how many items are in it?), or we can join two lists together into one. We can, of course, also give our list a name, by assigning it to a variable:
my_numbers = [10, 15, x]
If we didn't change x
and its value is still 25, the list above would be equivalent to the list [10, 15, 25]
.
For the sake of the example, I've limited these lists to just a few items, but a list can just as well contain 300 or 30 million items. You can see how it's very handy to be able to give our list a short name like my_numbers
, especially if we intend to apply further transformations to it.
Here are some questions and topics for you to explore and discuss with your peers today:
- What are the rules for naming variables in Python?
- What are some other basic data types in Python?
- The "list" data type is an example of a collection. What other collection data types does Python support?
- What is a "boolean" data type (
bool
in Python) and why is it special? What operations can be performed on data of typebool
? - EXTRA CREDIT: What is the difference between
date
anddatetime
in Python? How are timezones handled in Python? How can you convert adatetime
from one timezone to another? What are some ways to convert a Pythondate
ordatetime
to a string? What is "ISO formatting" for dates and times? What are some ways to convert a string to adate
ordatetime
? - EXERCISE: Think of (or make up) a character. It could be a character from a movie, or a book, or an actual person. Write a Python programme in which you use a series of variable assignments to describe your character. Use appropriate data types for each aspect or property of your character. For example, use a "string" (
str
in Python) for the character's name. Use anint
for their age. (But what data type would you use if your character is 6-and-a-half?) What data type would you use to store their date of birth? Think of different aspects of your character and see how many different data types you can use. What data type would you use to represent the "net worth" of your character? Compare your programme with your peers.
In the next lesson we will continue with some more sophisticated data types, we'll talk about boolean logic (something you'll need to get good at for just about any programme you'll ever need to write), and we'll see how we can interact with the "user" of our programme - even if we are the only users of our programmes so far.