True
CSCI 1012
.zip
with all the .py
files
Goal: Run different chunks of code depending on whether or not some condition holds:
==
, !=
, <
, >
, <=
, >=
)and
, or
, not
)Operator | Function | Syntax |
---|---|---|
== |
Equal? | 3 == 5 |
!= |
Not equal? | 3 != 5 |
< |
Less than? | 3 < 5 |
> |
Greater than? | 3 > 5 |
<= |
Less than or equal to | 3 <= 5 |
>= |
Greater than or equal to? | 3 >= 5 |
True
and False
True
!= "True"
Operator | Function | Example |
---|---|---|
== |
Equal? | 3 == 5 returns False |
!= |
Not equal? | 3 != 5 returns True |
< |
Less than? | 3 < 5 returns True |
> |
Greater than? | 3 > 5 returns False |
<= |
Less than or equal to | 5 <= 5 returns True |
>= |
Greater than or equal to? | 3 >= 5 returns False |
True
True
False
False
True
False
=
vs. ==
!They are not interchangeable:
=
is used for variable assignment
==
is used to check for equality between 2 values
4
False
and
and
: True
if all expressions being combined are True
; otherwise False
True
False
False
or
or
: True if at least one of expressions being combined is TrueTrue
True
False
not
False
True
The condition was True, so here we are
(nothing is printed)
x is greater than 1
x is greater than 1
done running code
x is greater than 1
done running code
x is greater than 1
done running code
x is greater than 1
done running code
done running code
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 0
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is less than or equal to 1
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 0
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is less than or equal to 1
0
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 0
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is less than or equal to 1
0
done running code
x = 4
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is greater than 1
done running code
x = 0
if x > 1:
print("x is greater than 1")
else:
print("x is less than or equal to 1")
print(x)
print("done running code")
x is less than or equal to 1
0
done running code
x = 0
if x > 1:
print("x is greater than 1")
else:
if (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
elif
statementsFalse
if (CONDITION1):
# Indented code that executes if CONDITION1 is True
elif (CONDITION 2):
# Indented code that executes if CONDITION1 is False and CONDITION2 is True
elif (CONDITION 3):
# Indented code that executes if CONDITION1 and CONDITION2 are False and CONDITION 3 is True
else:
# Indented code that executes if CONDITION1, CONDITION2, CONDITION3 are all False
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = -4
if x > 1:
print("x is greater than 1")
elif x >= 0:
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is less than 0
x = 0
if x > 1:
print("x is greater than 1")
elif (x >= 0):
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1
x = -4
if x > 1:
print("x is greater than 1")
elif x >= 0:
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is less than 0
def describe_number(x):
if x > 1:
return "x is greater than 1"
elif x >= 0:
return "x is greater than or equal to 0, but less than or equal to 1"
else:
return "x is less than 0"
first = describe_number(2)
print(first)
second = describe_number(-4)
print(second)
x is greater than 1
x is less than 0
x = 4
if x > 1:
print("x is greater than 1")
if x >= 0:
print("x is greater than or equal to 0, but less than or equal to 1")
else:
print("x is less than 0")
x is greater than 1
x is greater than or equal to 0, but less than or equal to 1
eligible_voter
True
(the boolean) if they are eligible to vote (i.e., at least 18)False
(the boolean) if they are not eligible to vote based on their ageeligible_voter.py
Submit on the submit server!
discounted_admission
discounted_admission.py
Submit on the submit server!
greater_number
max
function even if you are familiar with itgreater_number.py
Submit on the submit server!
==
, !=
, <
, >
, <=
, >=
)and
, or
, not
)Reading: Think Python Chapter 7
Homework 4 due Sunday, 9 Feb 11:55 PM