String Comparison, Chained Conditionals, and Logical Operators

CS 65: Introduction to Computer Science I

Conditionals Review

So far, we've seen conditional statements

In [1]:
total_bill = 35.63
age = float(input("What is your age? "))

if age > 65:
    print("Applying 10% senior discount")
    total_bill = total_bill * 0.9
    #any indented code is only runs
    #when the condition is True
    
#un-indented code runs no matter what
print("Your total bill is", total_bill)
What is your age? 67
Applying 10% senior discount
Your total bill is 32.067

conditional statements with two alternatives

In [2]:
height = int(input("Enter your height in inches: "))

if height < 60:
    print("You are not tall enough for this ride.")
else:
    print("You are tall enough for this ride.")
Enter your height in inches: 61
You are tall enough for this ride.

and in the lab, we saw nested conditional statements

In [3]:
num = int(input("Enter a number: "))

#this code checks if a number is divisible by both 2 and 3
if num % 2 == 0:
    print(num,"is even")
else:
    if num % 3 == 0:
        print(num,"is odd and divisble by 3")
    else:
        print(num,"is odd and not divisible by 3")
Enter a number: 9
9 is odd and divisble by 3

Group Exercise: Comparing Strings

Type the following program into Thonny, and run it several times. What does it do?

In [10]:
name1 = input("Enter a name: ")
name2 = input("Enter another name: ")

if name1 < name2:
    print(name1,"is first")
else:
    print(name2,"is first")
Enter a name: Eric
Enter another name: Manley
Eric is first

Is string comparison in Python case-sensitive? E.g., is "Eric" the same as "eric"? How could we test it?

In [ ]:
 

Character representation

Every text character is represented by a numerical code in your computer's memory.

They came up with in the 1960's as part of the ASCII standard (https://en.wikipedia.org/wiki/ASCII)

Here's some examples

code character
33 A
34 B
35 C
36 D
37 E
code character
65 A
66 B
67 C
68 D
69 E
code character
97 a
98 b
99 c
100 d
101 e

You can use the built-in ord() function to look up the code for any given character. Use chr() to do the opposite.

In [11]:
ord("A")
Out[11]:
65
In [14]:
chr(65)
Out[14]:
'A'

Other conditional statement options

There are also chained conditional statements when you have more than two choices

elif a new keyword meaning "else if" which allows you to add as many more options to a conditional statement as you want.

In [4]:
temp = float(input("What's the temperature? "))

if temp >= 70:
    print("Let's go hiking.")
elif temp >= 30:
    print("Let's put on jackets and go hiking.")
elif temp >= 10:
    print("Let's go skiing.")
else:
    print("It's too cold. Let's stay in and code.")
What's the temperature? 40
Let's put on jackets and go hiking.

Note:

  • else part is still optional but gives you the opportunity for "default" behavior
  • at most one block in a if-elif-else statement will run

a common chained-conditional logic error

In [5]:
temp = float(input("What's the temperature? "))

if temp >= 10:
    print("Let's go skiing.")
elif temp >= 30:
    print("Let's put on jackets and go hiking.")
elif temp >= 70:
    print("Let's go hiking.")
else:
    print("It's too cold. Let's stay in and code.")
What's the temperature? 80
Let's go skiing.

Make sure to put the conditions in the order you want to check them - the first one to evalute to True will be the one that runs.

Logical Operators

Logical operators are operators that take Booleans as operands: and, or, not

In [6]:
temp = float(input("What's the temperature? "))
raining = input("Is it raining? ")

if temp < 50 or raining == 'yes':
    print("Grab your coat.")

if not (raining == 'yes') and temp > 70:
    print("Grab your mitt.")
What's the temperature? 60
Is it raining? yes
Grab your coat.
In [ ]:
a = 2
b = 4
c = 6

See if you can work out the values below before the answer is revealed

In [ ]:
a == 4 or b > 2
In [ ]:
6 <= c and a > 3
In [ ]:
1 != b and c != 3
In [ ]:
a >= -1 or a <= b
In [ ]:
not a > 2

Logical operators are pretty intuitive because we use them in our natural language, but they can sometimes be tricky when you get complex expressions.

In [ ]:
a = 2
b = 4
c = 6
result = a == 2 or b != 2 and c != 6 or a == b + c
print(result)

Rule of thumb: Always use parentheses when you have more than one logical operator