An interactive introduction to programming in Python, for human beings and whoever else
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 = 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
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.
60 * 24 * 7
minutes = 60 * 24 * 7
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.
dog_years = 6 * 7and 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
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'
print(1, 2, "some words", "blah", 3)
foodwithout quotation marks to mean the variable instead of the string
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?
"___ degrees F is ___ degrees C"(with the blanks filled in)
f_degrees = 50to start things off
Celsiuson the right-hand side of the equation! To save something into a variable, you’ll need to do
c_degrees = blah blah
(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
print(f_degrees, "degrees F is", c_degrees, "degrees C")
We learned about variables, which is a way to save and name our data. We also practiced using
There were a lot of dumb math problems, too.