## Introduction to Python lesson 3

Are you a kid who wants to learn how to code in python.  Check out my blog posts about introductory concepts in python.  If you haven’t read the first two lessons, start there.

You can find the first lesson here and the second lesson here.

## Comparison Operators:

Let’s talk about 6 comparison operators:

>  means greater than

>=  means greater than or equal to

< means less than

<=   means less than or equal to

==  means equal to

!=  means not equal to

We can use comparison operators if we want to compare things.

### Use TWO Equal signs NOT one when we Want to Compare

Notice that when we did things such as creating a variable, we used ONE equal sign.  When we want to make

a comparison we will use TWO equal signs.

### Comparing Variables that have been defined as Numbers

Let’s create some variables and compare them.

For example, type the following:

a=2

b=2

c=3.

If you type a == b in the CONSOLE section and press ENTER, a value of True will be returned BECAUSE the variable a IS equal to the variable b.

To see this in the Middle editor section, type: print(a == b).

Try checking other conditions.  If you type a > c in the Console section, a value of False will be returned BECAUSE the variable a is NOT greater than the variable c.

Again, if you are in the middle section, type:  print(a > c)

Type b != c

A value of True will print out because b is NOT equal to c.  This condition is True.

### Comparing Strings

We can also compare strings.  For example type “Andre” == “Anna” in the console section.  A value of False will be returned because these two strings are NOT equal.

Now try typing “Andre” != “Anna”

Now a value of True will return BECAUSE the string Andre IS not equal to the string Anna. This is a true statement.

What will be returned if we were to type: “STEM” == “stem”

A value of False will be returned because of the case sensitivity of python.  These two strings are NOT equal to each other.

## Using the If Statement

We can use the if statement to have the computer do something based on a certain condition.

For example, we can do the following:

if name == “Shenek”:

print(“She’s the Alston kids’ mom”)

What happens is:

• First it’s going to print out the words What is your name?  The user can then input his or her name.
• It is then checked to see whether or not what they inputted (defined as the variable name) is equal to Shenek.
• If what they inputted is equal to the string Shenek, so that the condition is true, then the words She’s the Alston kids’ mom will be printed out.

If name is NOT equal to Shenek, NOTHING will be printed out because the condition was not true.

BE CAREFUL WHEN WRITING AN IF STATEMENT:

1. Make sure you use TWO equal signs if you want to compare if two things are equal.
2. Make sure that you put colons at the end of the if statement.
3. The print statement needs to be indented.  If in the middle section of replit, after typing the if statement JUST hit ENTER, and it should indent correctly for you.

### Case sensitivity:

What if we had typed in “shenek” as the answer to the input question.  What would have happened?

NOTHING will be printed out because the statement will NOT be true.  Only if the answer is Shenek will something be printed out.  Remember in python Shenek is NOT the same as shenek.

However, if the user types in shenek, we DO want to tell her that She is the Alston kids’ Mom but the way the program is currently written, it will NOT

### What can we do to fix this??

Let’s explore upper() or lower().

name=name.upper()
print(name)

if name == “SHENEK”:
print(“She’s the Alston kids’ mom”)

So what the name=name.upper() does is it takes whatever the person inputted as her name and changes it to all CAPS.  So then in our comparison in the if statement, we compare name to an ALL CAPS SHENEK.

Similarly, we could have done:

name=name.lower()
print(name)

if name == “shenek”:
print(“She’s the Alston kids’ mom”)

Here the name=name.lower() does takes whatever the person inputted as her name and changes it to all lower case.  So then in our comparison in the if statement, we compare name to a lower case version of shenek.

Exercise:

1. Define a variable age equal to any number you desire.
2. Use the if statement to compare age to the number 16.
3. If the age is less than 16, print out You can not drive.

Try to figure out the code yourself before looking at a possible answer below. 🙂

age=10
if age < 16:
print(“You can not drive.”)

Try running this as it is.  Also, go back and change the age to 16 or a number greater than 16.  For example, if you set age to 20, NOTHING will happen

because the statement age < 16 will NOT be true anymore.

### Challenge:

1. Ask someone their favorite ice cream flavor.

2. If it’s the same as your favorite, print out  the words That’s my favorite flavor too!

Try to write code yourself before looking at a possible answer below.

myfav=”cherry”
yourfav=input(“What’s your favorite ice cream flavor? \n”)
if  yourfav == myfav:
print(“That’s my favorite ice cream flavor too!”)

AGAIN IT’S IMPORTANT THAT YOU DO NOT JUST COPY/PASTE MY CODE ABOVE INTO REPLIT.  You want it to be indented correctly.  So manually type in my code if you want to try it out, and AFTER you type the line with the if statement on it, just hit ENTER, and you will get the correct indentation.

Then you can type in the line with the print function.  So manually type in the code. 🙂

Notice how I defined a variable myfav and then put that in my if statement.  I did NOT have to define a variable.  I could have instead put in my favorite ice cream in the if statement as such:

yourfav=input(“What’s your favorite ice cream flavor? \n”)
if  yourfav == “cherry”:
print(“That’s my favorite ice cream flavor too!”)

NOTICE THE DIFFERENCES IN THE TWO PROGRAMS:

1.  When I defined a variable myfav I did NOT put quotes when I used it in the if statement.  So we don’t need quotes when we use a variable here.
2. When I instead put in cherry in the if statement I needed to surround the string with quotes.

We talked about using the if statement.  We can use the if statement when we want to compare things.  However, when you use the if statement, only if the statement is true does anything “happen.” That can be somewhat frustrating.  What if you want something to print out or “happen” even if the first statement is NOT true.

We can use the if AND the else together.

## Using If and Else

What if we want the computer to print something out EVEN if the first condition is NOT true?  We can use if and else together.

Let’s do the following:

apples = 5

grapes = 8

if apples < grapes:

print(“Apples cost less than grapes”)

else:

print(“Apples cost more than grapes”)

So what happens is that IF the value assigned to apples is less than grapes (the statement is True), then the statement Apples cost less than grapes will print out.

However, if that condition is False, then the statement Apples cost more than grapes will print out.

One of the statements WILL print out, either the first statement or the second statement.

BE CAREFUL:

1. Make sure you put colons at the end of the if line and at after the word else.
2. The two print statements need to be indented.  Again, when you are on the line above those statements, JUST hit enter and it should properly indent it for you in replit if in the middle section.

EXERCISE:

Create a program where you:

1. Define a variable with your age (or someone’s age).
2. If the age is less than 16, print out: You can’t drive.
3. Otherwise print out: You CAN drive.

Try to write code yourself before looking at a possible answer below.

age = 20
if age < 16:
print(“You can’t drive.”)
else:
print(“You can drive.”)

## Homework Assignment 1:

1. You’ve met someone at the airport in the lobby.  Ask them where they are traveling to.
2. If they are traveling to the Grand Canyon, print out that that’s where’s you’re traveling too.

Try the homework before looking at a possible answer below.

visit=input(“Where are you visiting? \n”)
visit=visit.upper()
if visit==”GRAND CANYON”:
print(“I’m going there too!”)

#Remember how to get the correct indentation.  This may not be the exact indentation needed on the print line.

## Homework Assignment 2:

For a homework assignment, please do the following:

1. Create a program where you ask a person: What is the security code?

2. If the user’s security code is NOT equal to 500, then print out a message that tells them that they inputted the incorrect code.

REMEMBER: When we use the input function, the data that user inputs will be a string.  So be careful when trying to do your comparison in the if statement.

Remember that you can turn a string into an integer. 🙂

Try the homework before looking at a possible answer below.

code=input(“What is the security code? \n”)

if int(code) != 500:
print(“You entered the wrong code!”)

## Homework Assignment 3:

1. Create a program where you ask What day is it?
2. If it’s Monday, print Happy Monday.
3. Otherwise print It’s not Monday.

Try the homework before looking at a possible answer below.

day=input(“What day is it? \n”)

if day == “Monday”:
print(“Happy Monday.”)
else:
print(“It’s not Monday.”)

If you were NOT able to successfully program the exercise or challenge WITHOUT looking at the answers, also go back and try them again. 🙂

Recommended Posts