Video Coming Soon...
4: Variables and Names
Now you can print things with print
and you can do math. The next step is to learn about variables. In programming, a variable is nothing more than a name for something, similar to how my name "Zed" is a name for, "The human who wrote this book." Programmers use these variable names to make their code read more like English and because they have lousy memories. If they didn't use good names for things in their software, they'd get lost when they tried to read their code again.
If you get stuck with this exercise, remember the tricks you have been taught so far of finding differences and focusing on details:
- Write a comment above each line explaining to yourself what it does in English.
- Read your Python code backward.
- Read your Python code out loud, saying even the characters.
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
print("There are", cars, "cars available.")
print("There are only", drivers, "drivers available.")
print("There will be", cars_not_driven, "empty cars today.")
print("We can transport", carpool_capacity, "people today.")
print("We have", passengers, "to carpool today.")
print("We need to put about", average_passengers_per_car,
"in each car.")
_
in space_in_a_car
is called an underscore character
. Find out how to type it if you do not already know. We use this character a lot to put an imaginary space between words in variable names.What You Should See
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car.
Study Drills
When I wrote this program the first time I had a mistake, and Python told me about it like this:
Traceback (most recent call last):
Cell In[1], line 8, in <module>
average_passengers_per_car = car_pool_capacity / passenger
NameError: name 'car_pool_capacity' is not defined
Explain this error in your own words. Make sure you use line numbers and explain why.
Here are more study drills:
- I used 4.0 for
space_in_a_car
, but is that necessary? What happens if it's just 4? - Remember that 4.0 is a
floating point
number. It's just a number with a decimal point, and you need 4.0 instead of just 4 so that it is floating point. - Write comments above each of the variable assignments.
- Make sure you know what
=
is called (equals) and that its purpose is to give data (numbers, strings, etc.) names (cars_driven
,passengers
). - Remember that
_
is an underscore character. - Try running
python3
from the Terminal as a calculator like you did before, and use variable names to do your calculations. Popular variable names are alsoi
,x
, andj
.
Common Student Questions
What is the difference between
=
(single-equal) and==
(double-equal)? The=
(single-equal) assigns the value on the right to a variable on the left. The==
(double-equal) tests whether two things have the same value. You'll learn about this later.Can we write
x=100
instead ofx = 100
? You can, but it's bad form. You should add space around operators like this so that it's easier to read.What do you mean by "read the file (code) backward"? Very simple. Imagine you have a file with 16 lines of code in it. Start at line 16, and compare it to my code at line 16. Then do it again for 15, and so on until you've read the all of the code backward.
Why did you use
4.0
forspace_in_a_car
? It is mostly so you can then find out what a floating point number is and ask this question. See the Study Drills.
Register for Learn Python the Hard Way, 5th Edition (2023-2024)
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.