During a Python beginners course I usually briefly explain what an expression is.
It is not essential to know the term ’expression’, but I find it very helpful when explaining some essential concepts, such as what happens when you create a new variable.
Of course not everyone wants to know in detail how things work. Some people only care about the rules, the syntax. So I keep it quite short.
I usually cover expressions something like this:
If you’re new to Python or to programming, you don’t really need to know what an ’expression’ is. But I will be using the term a few times during the course, so let’s quickly go over it.
An expression is anything which returns a value. You can save this value for later use using a variable. Or you can use right it now, for instance by printing it.
An expression can be a simple variable
age = 10
print(age) # Expression: age
Or a calculation
age = 10
print(age + 1) # Expression: age + 1
Or the result of calling a function
name = 'Amara'
print(len(name)) # Expresion: len(name)
Or a combination of any of these
name = 'Amara'
print(len(name) + 10) # Expresion: len(name) + 10
Often, when the object already exists, that object is returned.
# Create a new object - a float with value 1.53
# and 'bind' it to the name 'amount'
amount = 1.53
# the expression '= age' does not create a new object
# but returns a reference to the existing one,
# created in the previous step
amount_copy = amount
# Both age and age_copy now refer to the same object
Whilst other expressions create a new object.
total = 5 * 1.73
name = 'Jonathan'
number_of_characters = len(name)
All of these create a new object, and ‘bind’ it to a new name (total, name and number_of_characters)