Dear friends of Tomorrow’s Teaching,
I’m happy to be back with another episode in our Python programming course for the Humanities! Today, we are going to talk about variables, what they do, and how to use them.
If you have not seen the previous episodes, it would be good to begin with the first one, here:
Later lessons build upon previous ones, so it is best to take the course in order, beginning from the first episode. If you have already watched until this point, then you will enjoy today’s episode too.
I am settling into a rhythm, I think, where I will try to bring you a new episode every Wednesday at around 15:00 GMT/UTC, whatever that is at your place (for me, it’s Wednesday night). We will see how this works out. I will also bring you more varied posts on other aspects of teaching and AI in future posts, but right now I feel that it would be better to not interrupt the flow of the programming course, so I’ll keep this up for the coming few weeks (the initial run, covering all of basic Python, is planned to be eight episodes).
So let’s go! As before, the full information is in the video and a summary in text form is appended below. If you have any questions or comments, please feel free to post them here and I will try to answer them.
Content Summary: Python Variables
This lesson begins with a brief recap of the previous session, reviewing how Python can directly calculate and print the result of mathematical operations. It also revisits the print()
statement, emphasizing the difference between parts in quotation marks, which are printed literally, and other parts like variables or expressions, which are evaluated first. The sep
parameter is also quickly demonstrated as a way to control the separator between printed items.
The main topic is variables, which are the very essence of computer programming. While you can perform simple, on-the-spot calculations without them, variables are what allow a program to remember information and store it in memory for later use. A variable is a name that represents a value. Unlike in mathematics where this is usually a number, a variable in programming can hold many types of data, including numbers, text (strings), files, or even complex data records.
To create a variable in Python, you use the assignment operator (=
) to give a name to a value. This action itself does not produce any visible output; the value is simply stored internally in the memory of the Python instance. For example, the line a = 23
creates a variable named a
and assigns it the value 23.
a = 23
To see that the variable was created and holds the correct value, you can use the print()
function. When you print(a)
, Python looks up the value associated with the name a
and displays it. If you try to print a variable that has not yet been defined, such as print(b)
, Python will raise a NameError
because it cannot find any variable with that name.
A variable can be assigned the result of a complex mathematical operation. Python automatically handles different types of numbers. For instance, if a calculation results in a number with a decimal part, Python treats it as a "floating-point" number (e.g., 30.5
) to distinguish it from an integer, or whole number.
a = (23 + 5 + 2 + 1) / 2
print(a)
Once a variable is defined, it can be used in subsequent calculations to define other variables. For example, after defining a
, you can create a new variable b
whose value is based on a
. This allows for building up complex operations step-by-step.
b = 2 * a
print(b)
There are a few rules for naming variables. A variable name must start with a letter, but it can contain digits later on (e.g., Peter3
). While Python allows you to mix uppercase and lowercase letters, it is considered good practice to be consistent, as many other programming languages are case-sensitive and treat Peter
and peter
as different variables.
Peter3 = 42
print(Peter3)
Modern programming environments like Google Colab offer a helpful auto-complete feature. When you start typing the name of a variable you have already defined, the editor will suggest a list of possible completions, which can save time and prevent typos, especially with long variable names.
Variables are not limited to numbers; they can also hold text, which in programming is called a string. To assign a string to a variable, you must enclose the text in quotation marks. This tells Python to treat the text as a literal value and not to try to interpret it as code.
greeting = "hello world"
print( greeting )
It is critical to remember the role of quotation marks when printing. If you write print(greeting)
, Python will print the value stored in the greeting
variable, which is "hello world". However, if you write print("greeting")
, it will print the literal word "greeting", because the quotation marks tell Python to treat it as a string, not as a variable name to be evaluated.
This concludes the basic introduction to using variables in Python. With this foundation, you can now begin to write more complex and dynamic programs.
See you in the next one! — Andy