-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch_2_practice.txt
120 lines (86 loc) · 2.78 KB
/
ch_2_practice.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
## 1. What are the two values of the Boolean data type? How do you write them?
True
False
## 2. What are the three Boolean operators?
not
and
or
## 3. Write out the truth tables of each Boolean operator (that is, every
## possible combination of Boolean values for the operator and what they
## evaluate to).
Statement Evaluates To
not True False
not False True
True and True True
True and False False
False and False False
True or True True
True or False True
False or True True
False or False False
## 4. What do the following expressions evaluate to?
Statement Evaulates To
(5 > 4) and (3 == 5) False
not (5 > 4) False
(5 > 4) or (3 == 5) True
not ((5 > 4) or (3 == 5)) False
(True and True) and (True == False) False
(not False) or (not True) True
## 5. What are the six comparison operators?
==
!=
<
>
<=
>=
## 6. What is the difference between the equal to operator and the
# assignment operator?
The equal to operator (==) compares whether two values are the same,
and evaluates to a boolean.
The assignment operator (=) assigns a value to a variable.
## 7. Explain what a condition is and where you would use one.
A condition is an expression that evaluates to a Boolean.
Conditions are used in control blocks.
## 8. Identify the three blocks in this code:
spam = 0
if spam == 10:
print('eggs') ## start block 1
if spam > 5:
print('bacon') ## start block 2
else:
print('ham') ## start block 3
print('spam')
print('spam')
## 9. Write code that prints Hello if 1 is stored in spam, prints Howdy
## if 2 is stored in spam, and prints Greetings! if anything else is stored
## in spam.
spam = 1
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else:
print('Greetings!')
## 10. What can you press if your program is stuck in an infinite loop?
CTRL-C
## 11. What is the difference between break and continue?
break causes Python to break out of a while loop or a for loop and
move to the point just after the loop.
continue causes it to skip the rest of the block and go to the
next iteration of the loop.
## 12. What is the difference between range(10), range(0, 10), and
## range(0, 10, 1) in a for loop?
There is no difference. range(10), range(0,10), and range(0,10,1)
produce the same thing.
## 13. Write a short program that prints the numbers 1 to 10 using a
## for loop. Then write an equivalent program that prints the numbers
## 1 to 10 using a while loop.
for i in range(1,11):
print(i)
j = 1
while j <= 10:
print(j)
j += 1
## 14. If you had a function named bacon() inside a module named spam,
## how would you call it after importing spam?
spam.bacon()