Introduction to If Statements and Variables
Welcome back to the third week of Intro to Code. We are looking forward to cover if statements and boolean variables. At the end of class you will build a Speed Dater.
The agenda for today is:
- Recap of Function and For Loops
- Exploring If Statements
- Speed Dater
- Extended Learning Opportunities
Below are the slides to reference for today’s class:
Recap of Functions and For Loops
For this week we are going to be covering an entirely new fundamental of code: if statements. Along with if statements we will be cover boolean variables and the input() parameter. For us, for loops and functions this week may not seem as relevant but we remember you can always refer to them when working on the Speed Dater.
Refer back to week 1 for functions. Refer back to week 2 for a review of for loops.
Exploring If Statements
if statements allow us to control when a function executes once a certain condition is True. We as humans do this all the time. Like for example, let’s take the example of eating food. We can say that we will eat when we are hungry. Otherwise we will continue to practice our coding. Translating that to code looks like this:
if hungry:
eat()
else:
practice_code()
Note that the above code reads as follows: “If I am hungry, then I will eat. Otherwise, I will continue to practice coding”. The “hungry” in this context is a conditional. A conditional holds either a True or False value. Meaning that if “hungry” is True, we eat(), otherwise we practice_code().
Common conditional checks we will use for our if statements are as follow:
1. if name == "Armando": #this checks to see if name is equal to Armando
2. if number != 0: # checks to see if the number is not zero
3. if number >= 0: # checks to see if a number is greater than or equal to zero
4. if number <= 0: # checks to see if a number is less than or equal to zero
There is also an if elif else statement. It gives you more options to select from. So let’s do another example. Let’s say you are a teacher and you are welcoming everyone into the classroom. You want to make sure you say “Hi” and then the student’s name immediately afterwards. To do that, in code it would like this:
if name == "Armando":
print("Hi Armando!")
elif name == "Alexa":
print("Hi Alexa!")
elif name == "Roberto":
print("Hi Roberto")
else:
print("Hi I don't know you!")
This is saying, if the name is Armando, say “Hi Armando”. And if the name is Alexa, say “Hi Alexa”. And if the name is Roberto say “Hi Roberto”. However if the name is not: “Armando, “Alexa”, or “Roberto”, just say: “Hi I don’t know you!”. The cool thing about if elif else statements is you can add as many elif statements as you want. In this case we just have 2 but you can add as many as you’d like.
In the above example, note that name here is just a variable. A variable in python can take on a number or string value (among other things). For number variables just write the name you want to give your variable0, an equal sign and a number afterwards as follows:
age = 12
For string variables, it just holds text. For that we do the same thing but on the other end of the equal sign, instead of placing a number, we but text surrounded in double quotes
name = "Armando"
One “=” in Python gives a variable its value. So here we made the name variable with the value of “Armando”. Two “==” of these in Python checks if a variable is equal to a certain value. Just to reiterate
name = "Armando" #make my name variable equal Armando
name == "Armando" # does our name variable equal Armando?
Last but not least, in your Speed Dater assignment you will make use of input(). input() is a way for the computer to wait for you to type stuff and then assigns what you typed to a variable. So for instance:
name = input("What is your name?")
This will assign the name variable with whatever you typed into the input. You can explore this within the introduction to if statements repl.it.
Here is the link that is a Repl.it playground for if statements.
Speed Dater
In this Speed Dater assignment, you will explore writing if elif else statements, writing conditionals, and using the input() technique in Python. My example of Speed Dater is below.
Here is the Repl.it link to Speed Dater.
Extended Learning Opportunities:
Like Week 2 and Week 1, we will share with you additional information from previous iterations of Intro to Code to solidify your understanding of if statements!