An interactive introduction to programming in Python, for human beings and whoever else
Chapter 10
A lot of things in life depend on answers to yes or no questions.
You can ask the same kinds of questions when programming.
if 4 < 10: print("You're not dreaming!")
It would be a crazy world if 4 was not less than 10, but we just had to check! If the thing on the first line is true, it prints out the second line (notice how it’s indented!).
This is called an if statement or a conditional. It needs four things:
if
at the beginning of the line:
at the end of the first lineThe indented code only runs if the comparison - the conditional, in this case 4 < 10
- is correct. For example, if we change it to ask if 10 is less than 4…
if 10 < 4: print("You're dreaming!")
Since ten is not less than four, nothing ever gets printed!
Try to write your own if statement below. Check to see if 100
if greater than 33
. If it is, print out "Correct!"
.
Remember to put a :
at the end of your if statement. And after your indented part, you’ll need to hit enter twice for your code to run.
All three of those were both pretty boring if statements, let’s be honest. 4
is always less than 10
, and 100
is always going to be more than 33
. Let’s try something more fun — instead of comparing two numbers, let’s compare a number and a variable.
In the US, the voting age is eighteen. I’ve saved a variable with a number as age
. Let’s write an if statement that looks at age
and prints “You can vote!” if we’re 18 or older.
Remember to hit enter twice to make it run! And if you get an error feel free to just Try again, no big deal.
age = 22
age
just like we used 4
or 10
in the first exampleage > 17
, “if age is more than 17”if
at the beginning of your if statement and the :
at the endprint
line. It can either be two or four spaces.if age > 17:
print("You can vote!")
Instead of using age > 17
, can also make the comparison as age >= 18
, which means “greater than or equal to.”
You can also ask if two values are exactly the same. This is super useful for comparing strings! To see if two things are exactly the same, you’ll use two equals signs, like ==
. Why two equals signs? I don’t know, programming is weird!
sound = 'woof woof' if sound == 'woof woof': print("It's a dog!")
Notice for "It's a dog"
I used double quotation marks. That’s because if I tried to use single quotation marks, it would look like this: 'It's a dog'
, and Python would get confused about which '
meant we were done talking.
Now below, I want you to reproduce the code about dogs, but make it about cats.
sound
"meow meow"
sound
to "meow meow"
- are they the same?"It's a cat!"
Remember to use two equals signs, and to press return twice to make it run.
sound = "meow meow"
==
, two equals signs:
at the end of your if
lineif sound == 'meow meow':
print("It's a cat!")
Sigh, sorry, this isn’t any fun to type. The indenting is weird and we have to hit enter twice and we can’t go back and edit and it just looks messy! Let’s change techniques and use the editor instead.
We can use the editor to illustrate how important identation is. Feel free to play around with the sound
in the next example and see what is and is not printed out.
sound = 'meow meow' if sound == 'meow meow': print("It's a cat!") print("She's too cute!") print("Can we keep her?") print("Take that animal back outside!")
This teaches us an important lessons about indentation:
Indentation is super important in Python (unlike some other languages), and people have personal opinions about whether you should use two spaces or four spaces. We like to use two spaces, but if four looks nicer to you that’s fine, too! Both work as fine as long as you always use the same number.
It’s pretty boring talking about the same animal sounds again and again, so hopefully we’ll be able to mix things up with sound
soon!
We learned about conditionals and if statements, which run code (or don’t run code) based on a yes/no question. If statements always end with a :
, and the special “yes” code is always indented by 2 or 4 spaces.
We also learned to ask for input from the user by using input
!