Variables, Strings and Assignment

In Python and other programming languages, variables are the way information is stored by the computer.   You make a variable by giving it a name and assigning it a value.  Say you need a variable to hold the name of a food.

here the variable is named “food” and it has the value “pickles”.  We can later use the variable in several ways.  Here, it is the argument of the print function.

If you give food a different value, for instance you write on the next line:

the first value is gone.  If you want to store two values at the same time, you’ll need two variables (or a more complex data structure, like a list – we’ll learn about those later.)

Variable names should start with a letter, and they cannot contain spaces.   Variable names may start with an underscore, but no other characters.  Variable names are case sensitive!   If you want a variable names to be more than one word long, which is often a great idea, use an underscore in the name, like this.

Some words are reserved by Python.  For instance, you cannot use the word “print” or the word “True” as a variable name.  “print” is the name of a function, and “True” and “False” are boolean values.

The = sign in variable assignment is different from the = you use in math class.  When you use = to assign a value to a variable, the variable name goes to the left, and the value to the right.  If you try this:

You get an error.

Strings

Strings are one kind of variable that you can use in Python.  In the next unit, we’ll learn to use numbers in Python as well.   Later, you’ll learn to make more complex objects like lists and sequences, and assign those to variables as well.

Strings are lists of characters.  You can learn more about Python strings here.