[Week 3]: Booleans, If-Elif-Else, For¶
We reviewed how to use strings and input. We specifically covered indexing and slicing. Make sure you remember these!
We then covered boolean variables and how to combine them. You can use boolean variables in if-elif-else statements, which can be used to create conditional code.
As bonus material, we covered turtles and for loops. I will cover both more in depth next week.
- Do one of the turtle design options:
- Draw a face
- Draw your initials
- Draw something creative
- Use inputs to make a menu!
- a basic menu with a joke
- a two-level menu with two jokes!
- Break things!
- Write down what happened and what the error was!
Booleans are variables that can have a value of
You can set Boolean variables in code with something like
x = True, or you can use comparison operators.
These are the comparison operators we discussed:
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
- == equal to (remember, in Python, “equal to” uses two equals signs, because one equals sign is just used for making a variable)
- != not equal to
Comparison operators compare the values of two different variables, and will evaluate to either True or False.
5 > 3 will evaluate to
10 == 9 will evaluate to
You can use these to make Boolean variables as well.
Booleans can also be combined using the
y are Booleans, the expression
x and y will only be
True if both
x or y will only be
True if at least one of them is
And of course,
not x will just be the opposite of
We practiced evaluating Booleans using cards and complex conditions (suite == hearts and not number <= 5).
if statements are comprised of two ingredients: a condition (which must evaluate or be a boolean), and some code.
Python checks if the condition is True; if it is, the code will be executed.
But if the condition is False, Python will just ignore the code and move on.
If statements kind of resemble a paragraph - the condition goes at the top, and the accompanying code is all indented by 4 spaces.
if <condition>: do some code do some more code back to normal code
The computer knows when the
if statement paragraph ends because the indentation stops.
That’s the only way it will know!
More complex types of
It helps to think of the three of them like this:
ifstatement gives the computer one option: if
<condition>is True, then do something. That’s all.
if-elsestatement gives the computer two options: if
<condition>is True, then do something. If
<condition>is False, do some other thing!
if-elif-elsestatement gives the computer several options, where you can say “Check all of these conditions until you find one that’s True.”
Each kind of statement is indented in the same way - with 4 spaces. Here’s an example of each:
if x == 5: print("x is 5!")
if x == "Penny": print("Your name is Penny!") else: print("Looks like your name isn't Penny!")
if age == 50: print("You're really old!") elif age == 20: print("You're kind of young!") elif age == 10: print("You're a kid!") else: print("I wonder how old you are?")
You can put in however many “elif” portions you want. The computer will just go through each of the conditions, one after another, until it finds one that’s True. Then, it will skip the rest of the paragraph. And if none of the conditions are True, it will do whatever is written under the “else” section.
The last thing we learned about is the
for loops are great - they use indented lines to form a ‘paragraph’ (kind of like If statements!) and let you run the code in that paragraph over and over again, as many times as you want!
Say you wanted to print someone’s name 10 times (kind of a ridiculous example). The loop would look like this:
for i in range(10): print("Cinder")
That’s it! If you execute this code in Python (easier to type it into PyCharm than the shell), it will print out “Cinder” ten times in a row.
Breaking it down:
foris a special keyword - when Python sees it, it knows we’ll be repeating some code
iis just a variable, just like
range(10)is the list of all numbers from 0 to 9
In the above For loop, Python will repeated the indented code 10 times, and each time,
i will take a new value.
- First time through:
- Second time through:
- Third time through:
So you can also do something like this:
for i in range(5): print(i)
This will print 0, 1, 2, 3, and 4, because the code will execute 5 times, and each time,
i has a different value!
For loops can be tricky to wrap your head around. The best thing to do is to use the above two examples, copy them into PyCharm, and verify that they work. Then try changing the number in range(), and also change around what happens in the indented text. The best way to practice new coding techniques is to try it yourself
- Turtle Loops 1
- Turtle Loops 2
- Turtle Circles
- Turtle Triangle Trick!
- Two Turtles and Triangle Stamps
- Turtle Star!
Extra Turtle Challenge: Specific Coordinates¶
Turtles are awesome because we can make them do many things. Let’s create the turtle first:
1 2 3
import turtle bob = turtle.Turtle() bob.speed('fastest')
Now, in the following, we can make the turtle go to very specific coordinates:
Bob is now at x=100 and y=0.
In general, the syntax is
We can use this to make interesting things. For example, if I want to make bob do a triangle without a for loop:
1 2 3 4
bob.setpos(-100, 0) bob.setpos(0,100) bob.setpos(100,0) bob.setpos(-100, 0)
What’s even cooler is that we can use variables to make this scalable:
1 2 3 4 5
tri_size = 30 bob.setpos(-1*tri_size, 0) bob.setpos(0, 1*tri_size) bob.setpos(1*tri_size, 0) bob.setpos(-1*tri_size, 0)
But this is a lot of code for something simple. What if we could store all of the coordinates ahead of time and then use a for loop to loop over the coordinates?
1 2 3 4 5 6
tri_size = 130 coords = [[-1, 0], [0, 1], [1, 0], [-1, 0]] for coord in coords: x = coord y = coord bob.setpos(x*tri_size, y*tri_size)
This triangle looks a little funny. What if we wanted to have each side be the same length AND use the coords list? What numbers would we have to change?
Use a coordinate list like the one above to make your initials (first and last).