So far, we've seen conditional statements
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)
conditional statements with two alternatives
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.")
and in the lab, we saw nested conditional statements
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")
Type the following program into Thonny, and run it several times. What does it do?
name1 = input("Enter a name: ")
name2 = input("Enter another name: ")
if name1 < name2:
print(name1,"is first")
else:
print(name2,"is first")
Is string comparison in Python case-sensitive? E.g., is "Eric" the same as "eric"? How could we test it?
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
|
|
|
You can use the built-in ord() function to look up the code for any given character. Use chr() to do the opposite.
ord("A")
chr(65)
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.
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.")
Note:
else part is still optional but gives you the opportunity for "default" behaviorif-elif-else statement will runtemp = 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.")
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 are operators that take Booleans as operands: and, or, not
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.")
a = 2
b = 4
c = 6
See if you can work out the values below before the answer is revealed
a == 4 or b > 2
6 <= c and a > 3
1 != b and c != 3
a >= -1 or a <= b
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.
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