Do you also want to program in the classroom? What can you start with? This is the first lesson in a series of Python blogs in the classroom. These lessons are given to third grades of secondary education in the context of Physics, subject Force and Movement. For another course you can copy the concepts, but you will have to change the examples. In this blog we will discuss a number of important choices. Which programming language do you choose? How are you going to do that with a class? And where? Then the first programming assignments come. We will cover numbers, strings (text) and variables in this lesson.
We recommend Python for programming in the classroom.
Python is relatively easy to learn as it is one of the most intuitive programming languages. You write the code almost as you would write it on paper. In addition, Python is the standard programming language at many universities (for modeling and data science). There are many “libraries” or tools available to help you create calculations, graphs or 3-D visualizations. Later, students can still do many things with Python. If necessary, they can switch to another programming language, but they have already learned the most important concepts of programming in general.
Introductory classes are about learning important concepts. These concepts are independent of the programming language. So let's start!
Where can you write Python code?
Students can write Python code online in Google Colab. This is an environment where you can write blocks of Python code and run it by clicking Play. This way you can have the students write the code first, and then see if the code works (or if the code unfortunately produces an error message). Small blocks of code can be alternated with blocks of text. As a teacher, you can also prepare a “Notebook” and share it with the students. In this way you can structure the lesson and prepare the exercises.
We recommend that you watch the introductory video. Here is also the link to the Colab Notebook for this lesson. If you want to edit this notebook, please save your own version first. Unfortunately, it's in Dutch.. but the code is English!
And then we are finally going to write the first Python code! We will first work with numbers. This is an important skill, because computations often have to be made in computer programs.
Click on the link to the Python notebook for this lesson if you also want to do this in the Google Colab environment.
You can work with numbers quite intuitively in Python. This is how you can simply write:
5 + 3
If you click on the Play button, you will see that Python shows the result 8.
Subtraction, multiplication and division also work as you would expect:
5 - 3 5 * 3 5 / 3
There is no need to add a space between numbers. This is for readability only.
You can round numbers using the "round" function. In the example below, the number is rounded to two decimal places.
round(5 / 3, 2)
The result is 1.67.
You can also raise a number to a power and take the square root. You write three to the second power as:
The answer is 9.
You write the square root of 9 as follows:
The answer is 3.
Finally, you can use scientific notation:
2.4 * 10**10
The answer is now 24000000000.
How far do you want to go in explaining numbers? You decide. If you think these examples go too far, feel free to stop at adding / subtracting or dividing / multiplying. You may not need powers and roots at all or the students may not have had the theory on this yet. So make your own lesson (and notebook) with the help of this information. You know your students best!
The next topic is text, which is called “strings” in Python. Strings are letters. A string can be a single word or a complete sentence. With strings you can do all kinds of things in Python. For example, you can edit text or search in text with Python code. This is an important skill, as it is important in many computer programs. Think of merging first and last names, or an address. Think of the Google search engine!
An important first step is to have text “printed”. This is not printing the text on paper. But if you want to know what text the computer has stored after a line of code, you can find this out with a print statement. This works as follows:
print('My name is Verbakel')
You will then see that Python displays the text. You can merge text with the plus sign. In the following example, we will add a first name to a last name. However, a space is also added between the first and last name! This is important, otherwise the first and last name will be stuck together.
print('Mark' + ' ' + 'Rutte')
You will now be given the full name Mark Rutte.
Can you also remove text? Sure, but not in the most intuitive way (a minus sign). Unfortunately this does not work! In this code I have put the first line as a comment. The hash does not execute the code on this line. So you can also write comments in the code, in addition to the possibility in the notebook to make text blocks (above and below the code blocks).
#This does not work! print('Mark Rutte' - ' Rutte')
If you run this (Play button) you will get a long error message. The error message states (among other things):
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Try to understand this error message: “unsupported operand… -“, so it says that you cannot use the minus sign for text (string). Being able to read and understand error messages is an important skill. This is very difficult at the beginning, but later you become more and more handy at it. Apparently it works differently than with a minus sign ...
Getting part of a text (string) is called “slicing”. In Python you can get some of the text by specifying the beginning and the end. The first letter of a string has the index 0. The letters after that always have a consecutive number. You can get a single letter from a string like this:
print('Mark Rutte') print('Mark Rutte')
You will get the letter “M” from the first line of code and the lowercase “r” from the second line of code. In other words, the first letter has the index 0 and the third letter the index 2. So the counting of letters starts at 0.
You can also get a longer part of the text by indicating the beginning and the end between the square brackets. Note, the last number (end) will not be part of the result. So if you want to remove the part “Mark” from the name “Mark Rutte”, you write:
Read this as follows: the first letter of the text has the index 0 and this is the beginning (will be included). The end of the text you want is the fourth index. So this is the fifth letter, in this case the space. However, the ending is not included. So what you are left with is the text "Mark".
With the function len () you can determine the length of a string. This is sometimes very useful (for example if you want to calculate the end of the slicing first).
You now get the number 10.
Variables can be used to “store” a string (text) or a number (number). It is very useful to store a text or number in a variable. After that, you can easily reuse the text or number every time, without having to type it all over again. It also makes the code “readable”. See the example below. First, we store the text "Mark" in the variable "first_name". You do this by defining the variable with the "=" sign. You can almost read it as: first name equals Mark. So now the computer "knows" that the variable "first_name" is the string "Mark". We do the same with the surname. Then we add these together and you get the full name “Mark Rutte” again.
first_name = 'Mark' surname = 'Rutte' print(first_name + ' ' + surname)
Example from physics: speed = distance / time or in formula form v = s / t. If we first store the numbers for distance and time in variables, then we can then write this down in the code in a very readable way.
#distance in meters (m) s = 20 #time is seconds (s) t = 5 #calculation of velocity in m/s v = s / t print(v) print('The velocity is ' + str(v) + ' m/s')
In Python you don't have to define what type the variable is. So you don't have to write in the definition that it is a number (number) or text (string). This is necessary in many other programming languages. This is also one reason why Python is a bit easier to learn than other programming languages. Python does this behind the scenes for you. Python recognizes the type during definition (by analyzing what is after the equal sign). Programming languages where you do have to indicate the type when defining variables are called “strongly typed”.
You can complete the theory of this lesson with assignments. See here the commands with numbers and strings.
Hopefully until the next lesson!