# Logic Operators

Two more operators you can use are and and or. These are the logic operators. They allow you to narrow down what's inside of your variable. For example, you could check for a range of ages like this:

if int(age) >= 60:

print("Young at heart")

elif int(age) >= 50:

print("Middle aged")

elif int(age) >= 40:

print("Over 40")

elif int(age) >= 30:

print("Not young anymore")

else:

print("Still young")

This code uses the greater than or equal to operators to check a range of ages. It's a little bit awkward, though. Instead, we can use the and operator instead:

if int(age) >= 13 and int(age) <= 21:

Now we're testing for two conditions on the same line (you can test for more than two). We're checking that the age variable is greater than or equal to 13 and we're checking if the age variable is less than or equal to 21. Both of these conditions have to be true for the whole line to be true. If just one of them is false, age is greater than 21, for example, then the line is evaluated as false and Python will move on.

You can keep going and add as many lines as you need:

age = input("How old are you? ")

if int(age) >= 13 and int(age) <= 21:

print("13 to 21")

elif int(age) >= 22 and int(age) <= 30:

print("22 to 30")

We're now checking for values between 22 and 30, as well as ones between 13 and 21.

You can add round brackets to make your long lines more readable (In PyCharm, this should make any underlines go away.):

elif (int(age) >= 22) and (int(age) <= 30):

Here, the round brackets make it clear that you have two tests separated by the and keyword.

## The or keyword

You can use or instead of and. The and logic operator needs each part of your if statement to be true before the whole line evaluates to true. If just one part is false, remember, then the whole line becomes false. With or, the line will be true if just one part is true. If that's a little confusing, try this out:

if (int(age) <= 0) or (int(age) >= 200):

print("Age out of range")

elif (int(age) >= 13) and (int(age) <= 21):

print("13 to 21")

elif (int(age) >= 22) and (int(age) <= 30):

print("22 to 30")

The line to examine is in bold:

if (int(age) <= 0) or (int(age) >= 200):

Here, we're checking two things: Is the age variable less than or equal to zero? Or is the age variable greater than or equal to 200? If just one of those is true (you enter -4, for example) then Python will return a value of true for the whole line and execute the print part. Contrast that with the and logic operator when both parts need to be true before the print statement executes.

## Python Not

You can also test a variable to see if it is not something. You do it with an exclamation point followed by an equals sign ( != ). In the code below, we're checking that the number entered is not zero:

if int(age) != 0:

print("REST OF CODE HERE")

else:

print("BAIL OUT")

If you prefer a more natural language, then you can use is not instead of the != symbols:

if int(age) is not 0:

## Nested if statement

You can have one or more if statement nested under other if statements. For example, you might want an extra message to display if a person enters exactly 13.

if (int(age) >= 13) and (int(age) <= 21):

if int(age) == 13:

print("Congratulations - new teenager!")

else:

print("14 to 21")

The first test is to see if the age variable is greater than or equal to 13 AND less than or equal to 21. If it is, we have a nested if statement:

if int(age) == 13:

print("Congratulations - new teenager!")

else:

print("14 to 21")

This is a brand-new test. Now, we want to know if the age variable is exactly 13. If it is, we print out a message. If it's not, then then the else part kicks in.

Note where all the indenting is in the code above. Each nested If part needs its own indentation. You can't do this:

if (int(age) >= 13) and (int(age) <= 21):

if int(age) == 13:

print("Congratulations - new teenager!")

else:

print("14 to 21")

Python will flag the above code as an error.

Nested If Statements can get quite messy. Just remember that each if line is being tested to see if it's true or false.

In the next lesson, you'll see how to use if statements with message boxes.

If Statements and Messages Boxes >