Variables

1.3 Variables

Learning objectives

By the end of this section you should be able to

  • Assign variables and print variables.
  • Explain rules for naming variables.

Assignment statement

Variables allow programs to refer to values using names rather than memory locations. Ex: age refers to a person's age, and birth refers to a person's date of birth.

A statement can set a variable to a value using the assignment operator (=). Note that this is different from the equal sign of mathematics. Ex: age = 6 or birth = "May 15". The left side of the assignment statement is a variable, and the right side is the value the variable is assigned.

Checkpoint

Assigning and using variables

Concepts in Practice

Assigning and using variables

1.
Which line of code correctly retrieves the value of the variable, city, after the following assignment?
city = "Chicago"
  1. print("In which city do you live?")
  2. city = "London"
  3. print("The city where you live is", city)
2.
Which program stores and retrieves a variable correctly?
  1. print("Total =", total)
    total = 6
    
  2. total = 6
    print("Total =", total)
    
  3. print("Total =", total)
    total = input()
    
3.
Which is the assignment operator?
  1. :
  2. ==
  3. =
4.
Which is a valid assignment?
  1. temperature = 98.5
  2. 98.5 = temperature
  3. temperature - 23.2

Variable naming rules

A variable name can consist of letters, digits, and underscores and be of any length. The name cannot start with a digit. Ex: 101class is invalid. Also, letter case matters. Ex: Total is different from total. Python's style guide recommends writing variable names in snake case, which is all lowercase with underscores in between each word, such as first_name or total_price.

A name should be short and descriptive, so words are preferred over single characters in programs for readability. Ex: A variable named count indicates the variable's purpose better than a variable named c.

Python has reserved words, known as keywords, which have special functions and cannot be used as names for variables (or other objects).

False
await
else
import
pass
None
break
except
in
raise
True
class
finally
is
return
and
continue
for
lambda
try
as
def
from
nonlocal
while
assert
del
global
not
with
asynch
elif
if
or
yield
Table 1.2 Keywords

Concepts in Practice

Valid variable names

5.
Which can be used as a variable name?
  1. median
  2. class
  3. import
6.
Why is the name, 2nd_input, not a valid variable name?
  1. contains an underscore
  2. starts with a digit
  3. is a keyword
7.
Which would be a good name for a variable storing a zip code?
  1. z
  2. var_2
  3. zip_code
8.
Given the variable name, DogBreed, which improvement conforms to Python's style guide?
  1. dog_breed
  2. dogBreed
  3. dog-breed

Try It

Final score

Write a Python computer program that:

  • Creates a variable, team1, assigned with the value "Liverpool".
  • Creates a variable, team2, assigned with the value "Chelsea".
  • Creates a variable score1, assigned with the value 4.
  • Creates a variable, score2, assigned with the value 3.
  • Prints team1, "versus", and team2 as a single line of output.
  • Prints "Final score: ", score1, "to", score2 as a single line of output.