Strings, variables and expressions

Strings, variables and expressions#

You remember that the following line of code is an expression - a recipe that returns a value:

10.50
10.5

We can see that this is an expression, because Jupyter displayed a text representation of the value that results: 10.5.

In fact the expression 10.50 is called a float literal.

Now consider this expression:

"Magneto"
'Magneto'

It is a recipe that returns a value. In this case the value is Python’s internal representation of the fragment of text “Magneto”.

We can store the result of the expression in a variable, using an assignment statement.

An assignment statement has a variable name on the left hand side, then an equals sign =, followed by an expression on the right hand side.

villain = "Magneto"
# Show the value
villain
'Magneto'

Notice the distinction between a variable name (no quotes) and the string literal (surrounded by quotes). A variable name is a name that refers to a value. It has to follow the rules of variable names. A string literal is an expression, that evaluates to Python’s representation of text.

Recall that an assignment is a name on the left hand side, an =, and an expression on the right hand side. So, what is wrong with this assignment statement (in fact, there are two things wrong)? Think how you would explain what is wrong. Then make a new cell (Cell menu item, Add Cell Below), copy the code into the cell, and see what Python says. Is that what you expected?

"goodie" = Professor X

Now fix the cell above so the variable goodie gets the value of the text fragment “Professor X”.