# Variables and Assignment

## Overview

Teaching: 10 min
Exercises: 10 min
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.

## 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'

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

## 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.

• If a variable doesn’t exist yet, or if the name has been mis-spelled, Python reports an error.
• Unlike some languages, which “guess” a default value.
print(last_name)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-c1fbb4e96102> in <module>()
----> 1 print(last_name)

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 can be used in calculations.

• We can use variables in calculations just as if they were values.
• Remember, we assigned 42 to age a few lines ago.
age = age + 3
print('Age in three years:', age)

Age in three years: 45


## Use an index to get a single character from a string.

• The characters (individual letters, numbers, and so on) in a string are ordered. For example, the string ‘AB’ is not the same as ‘BA’. Because of this ordering, we can treat the string as a list of characters.
• Each position in the string (first, second, etc.) is given a number. This number is called an index or sometimes a subscript.
• Indices are numbered from 0.
• Use the position’s index in square brackets to get the character at that position.
atom_name = 'helium'
print(atom_name[0])

h


## Use a slice to get a substring.

• A part of a string is called a substring. A substring can be as short as a single character.
• An item in a list is called an element. Whenever we treat a string as if it were a list, the string’s elements are its individual characters.
• A slice is a part of a string (or, more generally, any list-like thing).
• We take a slice by using [start:stop], where start is replaced with the index of the first element we want and stop is replaced with the index of the element just after the last element we want.
• Mathematically, you might say that a slice selects [start:stop).
• The difference between stop and start is the slice’s length.
• Taking a slice does not change the contents of the original string. Instead, the slice is a copy of part of the original string.
atom_name = 'sodium'
print(atom_name[0:3])

sod


## Use the built-in function len to find the length of a string.

print(len('helium'))

6

• Nested functions are evaluated from the inside out, just like in mathematics.

## 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.

## Use meaningful variable names.

• Python doesn’t care what you call variables as long as they obey the rules (alphanumeric characters and the underscore).
flabadab = 42
ewr_422_yY = 'Ahmed'

• Use meaningful variable names to help other people understand what the program does.
• The most important “other person” is your future self.

## Predicting Values

What is the final value of position in the program below? (Try to predict the value without running the program, then check your prediction.)

initial = 'left'
position = initial
initial = 'right'


## Solution

'left'


The initial variable is assigned the value ‘left’. In the second line, the position variable also receives the string value ‘left’. In third line, the initial variable is given the value ‘right’, but the position variable retains its string value of ‘left’.

## Challenge

If you assign a = 123, what happens if you try to get the second digit of a via a[1]?

## Solution

Numbers are not stored in the written representation, so they can’t be treated like strings.

a = 123
print(a[1])

TypeError: 'int' object is not subscriptable


## Choosing a Name

Which is a better variable name, m, min, or minutes? Why? Hint: think about which code you would rather inherit from someone who is leaving the lab:

1. ts = m * 60 + s
2. tot_sec = min * 60 + sec
3. total_seconds = minutes * 60 + seconds

## Solution

minutes is better because min might mean something like “minimum” (and actually does in Python, but we haven’t seen that yet).

## Slicing

What does the following program print?

atom_name = 'carbon'
print('atom_name[1:3] is:', atom_name[1:3])

atom_name[1:3] is: ar

1. What does thing[low:high] do?
2. What does thing[low:] (without a value after the colon) do?
3. What does thing[:high] (without a value before the colon) do?
4. What does thing[:] (just a colon) do?
5. What does thing[number:negative-number] do?
6. What happens when you choose a high value which is out of range? (i.e., try atom_name[0:15])

## Key Points

• Use variables to store values.

• Use print to display values.

• Variables persist between cells.

• Variables must be created before they are used.

• Variables can be used in calculations.

• Use an index to get a single character from a string.

• Use a slice to get a substring.

• Use the built-in function len to find the length of a string.

• Python is case-sensitive.

• Use meaningful variable names.