Instructions
Programs make decisions by asking questions that have a True or False answer. Python has three groups of operators for asking these questions. Every expression below produces a Boolean value: True or False.
1. Relational (comparison) operators
Relational operators compare two values. In the examples below, x = 7.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal to | x == 7 | True |
!= | not equal to | x != 7 | False |
> | greater than | x > 10 | False |
< | less than | x < 10 | True |
>= | greater than or equal to | x >= 7 | True |
<= | less than or equal to | x <= 6 | False |
=and==are different. One=stores a value in a variable. Two==compare two values.- Watch the boundary. "18 or older" includes 18, so it is
>= 18. "Over 18" does not include 18, so it is> 18. - The
=always comes second: write>=and<=, never=>or=<. - Text comparisons are case-sensitive.
"Maya" == "maya"isFalse. - Ints and floats compare by value.
5 == 5.0isTrue.
2. Logical operators: and, or, not
Logical operators combine or flip True/False values. They are lowercase English words in Python (not &&, || or ! like some other languages).
| A | B | A and B | A or B |
|---|---|---|---|
True | True | True | True |
True | False | False | True |
False | True | False | True |
False | False | False | False |
andis True only when both sides are True.oris True when at least one side is True.notflips one value:not TrueisFalseandnot FalseisTrue.
age = 15 print(age >= 13 and age <= 19) # True
3. Identity operators: is, is not
== asks "do these have the same value?" is asks "are these the very same object in memory?" is not asks the opposite.
a = [1, 2] b = a # b points to the SAME list as a c = [1, 2] # c is a NEW list with the same values print(a == c) # True same values print(a is c) # False different objects print(a is b) # True same object
- The most common use of
isis checking forNone. The official Python style guide (PEP 8) says to writex is Noneorx is not None, notx == None. - Do not use
isto compare numbers or text. Use==for those. Python may even show aSyntaxWarningif you write something likex is 5. is notis two words with a space between them.
4. Order: which part does Python do first?
When operators are mixed, Python works in this order: (1) comparisons such as ==, <, is, then (2) not, then (3) and, then (4) or. Parentheses always go first, so use them to make your meaning clear.
print(True or False and False) # True and goes first: True or (False and False) print((True or False) and False) # False parentheses change the order
How this works
- Enter your first name, last name initial, and class period, then click Start Assignment. Your start time is recorded.
- The assignment opens in a Python editor with three files (tabs) and 30 problems. Complete every orange dashed box and drop down.
- Click ▶ Run on any part to see what Python would print in the terminal. Run does not grade you, so check your work carefully.
- When you finish all three tabs, click Submit for Score. Your end time is recorded and your score appears. Answers cannot be changed after submitting.
| Part | Task | Problems | Points |
|---|---|---|---|
| 1 | Predict whether each line prints True or False | 12 | 6 (½ point each) |
| 2 | Choose the operator that completes each line | 10 | 5 (½ point each) |
| 3 | Type the missing operator | 8 | 4 (½ point each) |
| Total | 30 | 15 |
Student Information
print line, choose what Python would show in the terminal: True or False.>=, and or is not. Python keywords are lowercase.