1.3: Variables
- Page ID
- 117515
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
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.
Which line of code correctly retrieves the value of the variable, city, after the following assignment?
city = "Chicago"
print("In which city do you live?")city = "London"print("The city where you live is", city)
- Answer
-
c. This would produce the output string,
"The city where you live is Chicago".
Which program stores and retrieves a variable correctly?
-
print("Total =", total) total = 6 -
total = 6 print("Total =", total) -
print("Total =", total) total = input()
- Answer
-
b. These two lines of code would produce the output string,
"Total = 6".
Which is the assignment operator?
:===
- Answer
-
c. The single
=character is the assignment operator.
Which is a valid assignment?
temperature = 98.598.5 = temperaturetemperature - 23.2
- Answer
-
a. An assignment statement has the variable name on the left side and the value on the right side of the assignment statement.
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
|
Which can be used as a variable name?
- median
- class
- import
- Answer
-
a.
medianis a descriptive name for a variable that stores amedianvalue.
Why is the name, 2nd_input, not a valid variable name?
- contains an underscore
- starts with a digit
- is a keyword
- Answer
-
b. A variable name can contain, but not start with, a digit.
Which would be a good name for a variable storing a zip code?
zvar_2zip_code
- Answer
-
c. The name clearly indicates the value being stored is a zip code.
Given the variable name, DogBreed, which improvement conforms to Python's style guide?
dog_breeddogBreeddog-breed
- Answer
-
a. Snake case separates each word with an underscore and uses only lowercase letters.
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 value4. - Creates a variable,
score2, assigned with the value3. - Prints
team1,"versus", andteam2as a single line of output. - Prints
"Final score: ",score1,"to",score2as a single line of output.
Interactive Code
- Answer
-
team1 = "Liverpool"
score1 = 4
team2 = "Chelsea"
score2 = 3print(team1, "versus", team2)
print("Final score:", score1, "to", score2)


