An interactive introduction to programming in Python, for human beings and whoever else
Chapter 15
All right, let’s talk about presidents! We’ve had, I don’t know, maybe 40-something presidents in the history of the US, but I’m only interested in the first few.
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]
This time I’m putting them in a variable immediately so that we don’t have to mess around with typing that incredibly long line again and again and again.
How many presidents do we have here? We could count with our eyes, but I’d also hope Python can help us. Can you guess the name of the function that finds the length of a list? Use it to calculate the length of the list below.
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]
len
with the variable presidents
.len(presidents)
and press enter.Sometimes you want to deal with the presidents one at a time. No problem! You just do a weird thing with square brackets. Let’s get the first one.
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"] print(presidents[0])
Oh, weird, presidents[0]
gives us the first president? Then what does president[1]
do?
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"] print(presidents[0]) print(presidents[1]) print(presidents[2])
Yup, that seems super weird. presidents[0]
gives us the first president, presidents[1]
gives us the second president, and presidents[2]
gives us the third president. What??? That’s not how counting works!
Actually, it is how counting works: when you’re programming, counting starts from zero, not one!
Why? Because programming is strange, don’t think too hard about it. This isn’t even specific to Python - almost every programming language starts counting from zero instead of one!
Your training mission: Copy what we did above and print out the first president’s name.
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]
Your actual mission: Print out the fourth president’s name.
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]
presidents[4]
0
, 0
is the first, 1
is the second, 2
is the third, and 3
is the fourth.presidents[0]
gets us the first one, what does presidents[3]
give us?An important question might be, why do we use [0]
? Why not {0}
or (0)
? Well, because once upon a time someone invented Python and they decided that’s how it should be. Now we’re stuck with it!
Most things in programming are pretty arbitrary, sorry if you wanted a really deep explanation! As a compromise, I’ll tell you this: punctuation in programming is called syntax. Sometimes you’ll get a SyntaxError
, which usually means you did something wrong with a comma, parenthesis or bracket.
So far we’ve done some stuff with parentheses ()
and some stuff with brackets []
- the difference between [0]
and (0)
is very very important! Python is very picky about punctuation (a.k.a. syntax). If you were writing an essay, sometimes you might accidentally use the wrong punctuation mark. Most teachers wouldn’t care very much, no big deal, everyone survives. If you use the wrong punctuation mark in programming, through, Python goes CRAZY and refuses to do ANYTHING except print an error message.
Like this, for example. We’ll try to use presidents(0)
to get the first president instead of presidents[0]
.
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"] print(presidents(0))
Hint: If it’s an error with syntax (punctuation), why does it say TypeError
instead of SyntaxError
? Because even though you typed punctuation wrong, Python actually has a million other kinds of errors, too!
Hint: To do this right, you’ll want to type print(presidents[0])
Ouch! What did I do wrong? Type a new line up above to print out the first president correctly.
[]
vs ()
aren’t the end of it, either. As you learn to program, you’re going to become best friends with a thousand kinds of brackets, along with weird marks like ~
and |
that you probably never use in everyday life. Get excited!
Let’s do some real work, now! I stole this list of presidents in order of height from Wikipedia. The tallest president is first, and the shortest president is last. It’s so many presidents!!! I’ve put some missions for you down below the prompt.
presidents = ["Abraham Lincoln", "Lyndon B. Johnson", "Donald Trump", "Thomas Jefferson", "George Washington", "Chester A. Arthur", "Franklin D. Roosevelt", "George H. W. Bush", "Bill Clinton", "Andrew Jackson", "John F. Kennedy", "Ronald Reagan", "Barack Obama", "James Monroe", "John Tyler", "James Buchanan", "James A. Garfield", "Warren G. Harding", "Gerald Ford", "William Howard Taft", "Herbert Hoover", "Richard Nixon", "George W. Bush", "Grover Cleveland", "Woodrow Wilson", "Dwight D. Eisenhower", "Franklin Pierce", "Andrew Johnson", "Theodore Roosevelt", "Calvin Coolidge", "Jimmy Carter", "Millard Fillmore", "Harry S. Truman", "Rutherford B. Hayes", "William Henry Harrison", "James K. Polk", "Zachary Taylor", "Ulysses S. Grant", "John Quincy Adams", "John Adams", "William McKinley", "Martin Van Buren", "Benjamin Harrison", "James Madison"]
You won’t get confetti for getting these right yet, sorry! I haven’t set it up yet, but the last hint for each is the answer.
len
gives us the length of both strings and listslen
to allow it to hug up on the presidents
variablelen(presidents)
and press enterprint(presidents[0])
president[5]
, because with lists we start counting from 0
. Does that mean it’s going to be 4
or 6
? Try to count it out.print(presidents[4])
and press enter-1
to count from the end of the list, e.g. presidents[-1]
will give me the last one, presidents[-2]
will give me the second-to-last, etc.print(presidents[-1])
and press enter-1
, not -0
, so counting from the end is a little different than counting forward.print(presidents[-5])
and press enterWe learned more about lists, like how to see how long they are and access specific items inside.
The important thing is list_name[0]
gets you the FIRST element, so you always have to subtract in your mind. You can also use negative numbers to count from the end of the list, like list_name[-2]
gets you the second-to-last item.