-
Notifications
You must be signed in to change notification settings - Fork 0
/
BooleanPractice.txt
73 lines (51 loc) · 2.33 KB
/
BooleanPractice.txt
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
The logic combinations you learned from the last exercise are called “boolean” logic expressions.
Boolean logic is used everywhere in programming. They are essential fundamental parts
of computation, and knowing them very well is akin to knowing your scales in music.
In this exercise, you will take the logic exercises you memorized and start trying them out in
Python. Take each of these logic problems, and write out what you think the answer will be. In
each case, it will be either True or False. Once you have the answers written down, you will start
Python in your Terminal and type them in to confi rm your answers.
Ture and True
False and True
1==1 and 2==1
"test"=="test"
1==1 or 2!=1
True and 1==1
False and 0!=0
True or 1==1
"test"=="testing"
1!=0 and 2==1
"test"!=="testing"
"test"==1
not (True and False)
not(1==1 and 0!=1)
not(10==1 or 1000==1000)
not(1!=10 or 3==4)
not("testing"=="testing" and "zed"=="cool Guy")
1==1 and not ("testing"==1 or 1==0)
"chunky"=="bacon" and not (3==4 or 3==3)
3==3 and not ("testing"=="testing" or "Python"=="Fun")
I will also give you a trick to help you fi gure out the more complicated ones toward the end.
Whenever you see these boolean logic statements, you can solve them easily by this simple process:
1. Find an equality test (== or !=) and replace it with its truth.
2. Find each and/or inside parentheses and solve those fi rst.
3. Find each not and invert it.
4. Find any remaining and/or and solve it.
5. When you are done, you should have True or False.
I will demonstrate with a variation on #20:
3 != 4 and not ("testing" != "test" or "Python" == "Python")
Here’s me going through each of the steps and showing you the translation until I’ve boiled it
down to a single result:
1. Solve each equality test:
a. 3 != 4 is True: True and not ("testing" != "test" or "Python" ==
"Python")
b. "testing" != "test" is True: True and not (True or "Python" == "Python")
c. "Python" == "Python": True and not (True or True)
2. Find each and/or in parentheses ():
a. (True or True) is True: True and not (True)
3. Find each not and invert it:
a. not (True) is False: True and False
4. Find any remaining and/or and solve them:
a. True and False is False
With that, we’re done and know the result is False.
WARNING!