Skip to ContentGo to accessibility page

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

Assigning and using variables; ch 1, video 5

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.
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.