-
Notifications
You must be signed in to change notification settings - Fork 805
/
028_logic.py
85 lines (59 loc) · 2.15 KB
/
028_logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Video alternative: https://vimeo.com/954334163/bf61706e77#t=760
from lib.helpers import check_that_these_are_equal
# As we've mentioned, while numeric operators evaluate to
# numbers, like this:
1 + 2 # evaluates to 3
# Comparison operators evaluate to True or False
1 == 2 # evaluates to False
2 == 2 # evaluates to True
# The `if` keyword looks at whether the conditional
# expression evaluates to True or False to decide whether to
# execute its block, or the else block.
# We can use this information to combine conditions using a
# new kind of operators: logical operators.
# Consider this function:
def starts_with_x_or_y(the_str):
first_letter = the_str[0]
# VV look at this!
if first_letter == "x" or first_letter == "y":
return "It does!"
else:
return "It does not."
# That `or` operator says "evaluate to true if the condition
# on the left, or on the right, or both evaluate to true".
# Operators like `or` are called 'logical' or 'Boolean'
# operators.
# There are others here too.
# @TASK: Research by searching the web for "python
# logical operators" and complete the exercises below.
# I've started it for you.
print("")
print("Function: a_or_b")
def a_or_b(a, b):
return a or b
check_that_these_are_equal(a_or_b(True, True), True)
check_that_these_are_equal(a_or_b(True, False), True)
check_that_these_are_equal(a_or_b(False, True), True)
check_that_these_are_equal(a_or_b(False, False), False)
# == Exercise One ==
print("")
print("Function: a_and_b")
def a_and_b(a, b):
# return a ?? b
pass
check_that_these_are_equal(a_and_b(True, True), True)
check_that_these_are_equal(a_and_b(True, False), False)
check_that_these_are_equal(a_and_b(False, True), False)
check_that_these_are_equal(a_and_b(False, False), False)
# == Exercise Two ==
print("")
print("Function: not_a")
# Note that this operator only takes one value. The operator
# goes first, and the value second.
def not_a(a):
# return ?? a
pass
check_that_these_are_equal(not_a(True), False)
check_that_these_are_equal(not_a(False), True)
# Perhaps you could have guessed those... but now you know
# for sure! When you're done, move on to 029_lists.py