Questions:

  • How can I store data in programs?

Objectives:

  • Write programs that assign scalar values to variables and perform calculations with those values.
  • Correctly trace value changes in programs that use scalar assignment.

Keypoints:

  • Use variables to store values.
  • Use print to display values.
  • Variables must be created before they are used.
  • Variables persist between cells.
  • Variables can be used in calculations.
  • Python is case-sensitive.
  • Use valid and meaningful variable names.

Use variables to store values.

  • Variables are names for values.
  • In Python the = symbol assigns the value on the right to the name on the left.
  • The variable is created when a value is assigned to it.
  • Here, Python assigns an age to a variable age and a name in quotes to a variable first_name.
age = 42
first_name = 'Ahmed'

Use print to display values.

  • Python has a built-in function called print that prints things as text.
  • Call the function (i.e., tell Python to run it) by using its name.
  • Provide values to the function (i.e., the things to print) in parentheses.
  • To add a string to the printout, wrap the string in single or double quotes.
  • The values passed to the function are called 'arguments'
print(first_name, 'is', age, 'years old')
Ahmed is 42 years old
  • print automatically puts a single space between items to separate them.
  • And wraps around to a new line at the end.

Variables must be created before they are used.

  • Unlike some languages, which "guess" a default value, if a variable doesn't exist yet, or if the name has been mis-spelled, Python reports an error.
print(last_name)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/var/folders/5q/mny3pg2n7h5g21h3v32rfj9wpykqrf/T/ipykernel_792/2256896230.py in <module>
----> 1 print(last_name)
      2 

NameError: name 'last_name' is not defined
  • The last line of an error message is usually the most informative.
  • We will look at error messages in detail later.

Variables Persist Between Cells

Be aware that it is the order of execution of cells that is important in a Jupyter notebook, not the order in which they appear. Python will remember all the code that was run previously, including any variables you have defined, irrespective of the order in the notebook. Therefore if you define variables lower down the notebook and then (re)run cells further up, those defined further down will still be present. As an example, we can create 2 cells with the following content, in this order:

print(myval)
myval = 1

If you execute this in order, the first cell will give an error. However, if you run the first cell after the second cell it will print out ‘1’. To prevent confusion, it can be helpful to use the Kernel -> Restart & Run All option which clears the interpreter and runs everything from a clean slate going top to bottom.

Variables can be used in calculations.

  • We can use variables in calculations just as if they were values.
age = 42
age = age + 3
print('Age in three years:', age)
Age in three years: 45

Python is case-sensitive.

  • Python thinks that upper- and lower-case letters are different, so Name and name are different variables.
  • There are conventions for using upper-case letters at the start of variable names so we will use lower-case letters for now.

Tip: In programming an eye for detail is important. If you include an extra full-stop, or forget a space, then you may get an error message or unexpected behaviour.

Use valid and meaningful variable names.

  • Python doesn't care what you call variables as long as they obey the following rules:

    • can only contain letters, digits, and underscore _ (typically used to separate words in long variable names)
    • cannot start with a digit
  • Variable names that start with underscores like __bobbins_real_age have a special meaning so we won't do that until we understand the convention.

flabadab = 42
ewr_422_yY = 'Ahmed'
print(ewr_422_yY, 'is', flabadab, 'years old')
  • Use meaningful variable names to help other people understand what the program does.
  • The most important "other person" is your future self.