P | Q | P and Q | P or Q |
---|---|---|---|
True | True | True | True |
True | False | False | True |
False | True | False | True |
False | False | False | False |
P | not P |
---|---|
True | False |
False | True |
"x > 5 or x < 8" "x < 5 and x > 10"
Operators | Name |
---|---|
x[index], x[index:index], x(arguments...), x.attribute | Subscription, slicing, call, attribute reference |
** | Exponentiation (right to left associative) |
+x, -x, ~x | Positive, negative, bitwise NOT |
*, /, //, % | Multiplication, division, remainder |
+, - | Addition and subtraction |
in, not in, is, is not, <, <=, >, >=, !=, == | Comparisons, including membership tests and identity tests |
not x | Boolean NOT |
and | Boolean AND |
or | Boolean OR |
if boolean expression: statements1 else: statements2
if x > y: t = x x = y y = t
if a > 5: if b < 10: print ("green") else: print("blue") versus if a > 5: if b < 10: print("green") else: print("blue")
example: if x > 50: grade = "A" else: if x <= 50 and x > 30: #the x <= 50 MUST be true! don't test again! grade = "B"
else: if ...
if condition1: action1 elif condition2: action2 elif condition3: action3 else: default case
if isPrime(num): print("it's good!")
if x > 0: return "positive" else: return "not positive"
if x > 0: result = "positive" else: result = "not positive" return result
if abs(a - 5000) < 0.00001: print("close enough")
Thinking about if's short set of slides about putting if's together