True
CSCI 1012
eligible_voter.py
discounted_admission.py
greater_number.py
.zip
with all the .py
filesGoal: Run different chunks of code depending on whether or not some condition holds:
==
, !=
, <
, >
, <=
, >=
)and
, or
, not
)if
- elif
- else
StatementsOperator | 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 |
bool
)
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
operator
True
if both expressions being combined are True
; otherwise False
True
False
and
unpackedFalse
True
or
or
True
if at least one of expressions being combined is True
True
True
or
or
True
if at least one of expressions being combined is True
False
not
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 = 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 = 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 = 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:
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
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!
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
discounted_admission
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, 21 Sep 11:55 PM