Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Chapter 8

Variables

The number one rule of programming is that programmers are lazy! There are probably more rules, but I’m too lazy to remember them right now. We spend a lot of time typing, so if there’s any way to not type the same code again and again and again, we love it.

When we’re programming, we can give our data names. Think of it like names for people. If we know some big guy with a big beard who wears red clothes and brings us presents, we don’t call him “some big guy with a big beard who wears red clothes and brings us presents.” We just give him a name: Santa! Easy.

We can do the same thing with our data. Remember when we calculated the number of seconds in a day, with 60 * 60 * 24? We’d get real bored typing that again and again and again, so instead we’re just going to do this magic, and name it seconds:

seconds = 60 * 60 * 24

It looks like nothing happened, but now every time I type seconds it’s just another name for 60 * 60 * 24.

seconds = 60 * 60 * 24
print(seconds)

We don’t have to use print here, but we’ll need to use it soon so let’s get some practice in with that, too.

How many seconds are in 3 days?

seconds = 60 * 60 * 24
print(seconds * 3)

In a year?

seconds = 60 * 60 * 24
print(seconds * 365)

These names are called variables. They’re super helpful, and we’re going to use them every second of the rest of our programming lives.

Make a variable called minutes that represents the number of minutes in a week, then print it out.


  • Hint: A week is 7 days, of 24 hours per day, 60 minutes per hour
  • Hint: Your math is going to be 60 * 24 * 7
  • Hint: Save your calculation as a variable called minutes by using minutes = 60 * 24 * 7
  • Hint: Print your new variable with print(minutes)

I think descriptive variable names are good - what does minutes really mean? I think minutes in a year is a little more useful! We should make that the variable name so people know what we’re talking about.

minutes in a year = 60 * 60 * 24

Oops, that’s an error! It turns out you can’t put spaces in a variable name. If you want to use spaces, you typically use underscores, like minutes_in_a_year. Some other languages might use minutesInAYear instead, but Python programmers typically use underscores.

minutes_in_a_year = 60 * 60 * 24
print(minutes_in_a_year)

Exercise: For every one “normal” year, a dog gets 7 “dog years” older. For example, a 5-year-old dog is 35 dog years old, and a 10-year-old dog is 70. If I have a dog that is six years old, how old is he in dog years? Save this in a variable named after “dog years,” then print the variable out.


  • Hint: You can’t use spaces in your variable name! Try underscores _ instead
  • Hint: Your calculation is 6 years times 7 dog years.
  • Hint: Name your variable dog_years
  • Hint: Type dog_years = 6 * 7 and press enter, then print(dog_years) and press enter

Yes, that was a pretty dumb exercise, and yes, I’m sorry.

You can use variables with any kind of data (integers! strings! lists! floats! etc!). For example, maybe I’m saying hello to one of my friends:

name = 'Lakshmi'
print('Hello', name)

WAIT A SECOND. This is confusing. I thought we couldn’t type words without quotation marks?????? Well, kind of. It depends on whether we’re using

  • No quotation marks? Variable name.
  • Quotation marks? String.

If we type "seconds", we mean the string seconds, the actual text of the word. If we type seconds, we mean the variable seconds.

seconds = 60 * 60 * 24
print(seconds, "is a variable")
print("seconds", "is a string")

It usually takes a while to remember the difference between strings and variables. If you keep getting mixed up, don’t worry, you’ll get it with practice.

Mission: I created a variable called food. Please print out "I love chocolate", but use the variable, not just print("I love chocolate"). That’d be cheating!

food = 'chocolate'
  • Hint: To print out multiple things, you separate them with commas, like print(1, 2, "some words", "blah", 3)
  • Hint: Use food without quotation marks to mean the variable instead of the string "food"
  • Hint: Type print("i love", food) and press enter

Are you ready for a hard one??? Feel free to skip this one if you’d like.

The formula to convert from Fahrenheit to Celsius is (Fahrenheit - 32) * 5 / 9 = Celsius.

If it’s 50 degrees Fahrenheit, what’s the temperature in Celsius?

  • Create a variable called f_degrees and save 50 into it
  • Calculate the number of degrees in celsius, and save it in a variable called c_degrees
  • Print out the sentence "___ degrees F is ___ degrees C" (with the blanks filled in)

  • Hint: You’ll use f_degrees = 50 to start things off
  • Hint: I tried to trick you by putting Celsius on the right-hand side of the equation! To save something into a variable, you’ll need to do c_degrees = blah blah
  • Hint: Your calculation should be (f_degrees - 32) * 5 / 9. You could also use (50 - 32) * 5 / 9, but that’s not as fun. You’ll want to save that into the variable c_degrees
  • Hint: The print statement is tough! It’s print(f_degrees, "degrees F is", c_degrees, "degrees C")

Chapter summary

We learned about variables, which is a way to save and name our data. We also practiced using print to print out multiple things (variables, text, numbers) on the same line.

There were a lot of dumb math problems, too.