Google Colab and Your First Program
Python for the Humanities, Episodes 02 and 03
Dear friends of Tomorrow’s Teaching,
Thank you for being back here!
In our series on Python Programming for the Humanities, I would like to send you the next two episodes in this email, so that you have all the videos as they are now already published on YouTube. From next week on, I will only send you one email per week, so don’t worry — I’m not going to fill up your inbox with emails.
The two videos in this email are (a) an introduction to Google Colab as a programming environment for total beginners, and (b) your very first Python program. Both are, as the titles say, very easy to follow. I often think that programming tutorials on YouTube are excessively hard to understand — young developers often want to show off their own proficiency rather than being focused on how easy they make things for their audience. Not so here. I’m an old humanities teacher, writing for old humanities teachers :) so things will go really slowly. If at any point you have questions, just drop them down below and I’ll do my best to answer them. If there is enough interest, we could also have live sessions, either on YouTube or here on Substack, where we explore small coding exercises together. Again, just tell me what you think down below.
And here are the two videos. I will not transcribe the full text, because the instruction relies on showing you in the video how to do things; it would be impractical to try and explain every step in a text-only format. And, after all, this newsletter is called Tomorrow’s Teaching, and tomorrow’s teaching certainly should involve video.
So here we go!
Python for the Humanities 02: Google Colab
Your First Python Program in Google Colab
This first lesson explains how to get access to a Python programming environment using Google Colab. Colab is a free, online tool provided by Google that allows you to write and run Python code directly in your web browser. This is a great starting point because it completely avoids the potential hassle of installing software on your own computer, ensuring that everyone can get started right away.
The first step is to find the tool by searching for "Google Colab" and clicking the link to the "Google Collaboratory." Once there, you will be prompted to sign in with your Google account. After signing in, you can begin by creating a "New Notebook," which is the file where you will write all of your programs for this course.
Once your new notebook is open, you can familiarize yourself with the basic interface. You can rename your notebook at the top of the page to give it a meaningful title, like "Test." The main area consists of cells where you will write your code. For better readability, you can easily make the font size bigger by pressing Shift+Control+Plus on your keyboard.
To write and run your first program, you simply type directly into the first cell. Python can be used like a simple calculator, so you can start by just typing a number. After typing your code, you execute the cell by clicking the small "play" button to the left of the cell. The first time you run a cell, it may take a moment to connect to Google's servers.
Try to enter the following in a code cell:
23
After the code executes, the result is displayed directly below the cell. This immediate feedback confirms that the environment is working correctly. You can try this with any basic arithmetic expression to see how Python calculates and returns the result instantly, e.g.:
23 + 45
When a cell runs, Colab provides useful information about the execution, including how long it took. The free version of Google Colab has limits on computing time, but the small programs we will be writing in this course are very unlikely to ever reach these limits. You can also view the resources your notebook is using, such as RAM and disk space, and will see that the free tier provides more than enough for our purposes.
The lesson concludes with an encouragement for you to follow these steps and set up your own Google Colab environment. This will ensure you are ready to start programming in the next video. If you run into any problems during this setup process, you are welcome to ask for help in the comments section.
Python for the Humanities 03: Your First Program
Content Summary: The print
Statement in Python
This lesson begins by explaining how to build upon previous work in a Google Colab notebook. Instead of deleting or replacing old code, you can keep a running history of your learning process by adding new cells. You can insert "Code cells" for new Python commands and "Text cells" to write notes and explanations for yourself, creating a well-documented and organized record of your progress. It's also good practice to rename your notebook to reflect the current topic, such as "Lesson 1 - Input Output."
The first actual Python statement introduced is print()
, which is used to display output to the screen. In modern Python, the print
command is always followed by parentheses ()
. When you type the opening parenthesis, Google Colab often displays a helpful pop-up box that provides technical details about the function and its parameters. While this help box might seem complex for a beginner, it can be very useful later on for remembering how to use different functions.
The lesson also briefly touches on the concept of an output "stream." This is an abstract programming idea where the output, like a river, can be directed to different places. For now, the stream will always go to our screen, but it's useful to know that the same print
command could be used to send output to a file or another computer.
The most basic use of the print()
function is to display a literal piece of text, known as a string. To do this, you must enclose the text in quotation marks. This tells Python to print the characters exactly as they are, without trying to interpret them as code. The classic first program is a good example of this.
print("hello world")
It is crucial to understand the role of the quotation marks. If you try to print text without them, Python will assume you are referring to a command or a variable that it should know about. Since it doesn't recognize "hello" as a command, it will stop and show an error message. The error message can be helpful, often pointing to the exact character where the problem occurred, which is a good first step in learning how to debug your code.
However, if you give the print()
function something that it can evaluate, like a mathematical expression, you do not need quotation marks. Python will calculate the result of the expression first and then print the answer. This is the key difference: without quotes, Python evaluates; with quotes, it prints literally.
print(23 + 5)
You can combine literal strings and evaluated expressions in a single print()
statement by separating them with a comma. Python will print the string as-is, followed by the result of the calculation. A useful keyboard shortcut for running a cell is Ctrl+Enter
, which saves you from having to click the play button every time.
print("23 + 5 =", 23 + 5)
By default, Python places a single space between each item you ask it to print. In the example above, this results in an unwanted space between the equals sign and the number 28. You can control this behavior using the sep
(separator) parameter. By setting sep=''
(an empty string), you are telling Python to use no separator at all, which makes the output look cleaner.
print("23 + 5 =", 23 + 5, sep='')
This covers the fundamentals of using the print()
statement for basic output. The next step in the course will be to learn about variables, which will allow for creating much more complex and powerful programs.
And with this we are done for today! Thank you again for reading and feel free to reply to this email if you would like to talk to me privately, or just leave a comment below!
Have a nice weekend! — Andy