-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_2.py
More file actions
99 lines (76 loc) · 3 KB
/
Assignment_2.py
File metadata and controls
99 lines (76 loc) · 3 KB
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
1. The two values of the Boolean data type are `True` and `False`. They are written in all capital letters.
2. The three different types of Boolean operators are:
- Logical AND (`and`)
- Logical OR (`or`)
- Logical NOT (`not`)
3. Here are the truth tables for the Boolean operators:
| Operator | Truth Table |
| --- | --- |
| `and` | `True and True = True`<br>`True and False = False`<br>`False and True = False`<br>`False and False = False` |
| `or` | `True or True = True`<br>`True or False = True`<br>`False or True = True`<br>`False or False = False` |
| `not` | `not True = False`<br>`not False = True` |
4. The values of the given expressions are:
- `(5 > 4) and (3 == 5)` evaluates to `False`
- `not (5 > 4)` evaluates to `False`
- `(5 > 4) or (3 == 5)` evaluates to `True`
- `not ((5 > 4) or (3 == 5))` evaluates to `False`
- `(True and True) and (True == False)` evaluates to `False`
- `(not False) or (not True)` evaluates to `True`
5. The six comparison operators are:
- Equal to (`==`)
- Not equal to (`!=`)
- Greater than (`>`)
- Less than (`<`)
- Greater than or equal to (`>=`)
- Less than or equal to (`<=`)
6. The equal to (`==`) operator is used for comparison, while the assignment operator (`=`) is used to assign a value to a variable. For example:
```
x = 5 # Assignment operator, assigns the value 5 to the variable x
if x == 5: # Comparison operator, checks if the value of x is equal to 5
print("x is equal to 5")
```
7. The three blocks in the given code are:
```python
spam = 0
if spam == 10:
print('eggs')
if spam > 5:
print('bacon')
else:
print('ham')
print('spam')
print('spam')
```
8. Here's the code that prints the appropriate message based on the value of `spam`:
```python
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else:
print('Greetings!')
```
9. If your program is stuck in an endless loop, you can press `Ctrl + C` to interrupt the execution and stop the program.
10. The `break` statement is used to exit the loop, while the `continue` statement is used to skip the current iteration and move to the next one.
11. The differences between the three `range()` functions are:
- `range(10)` generates the sequence `0, 1, 2, 3, 4, 5, 6, 7, 8, 9`.
- `range(0, 10)` generates the same sequence as `range(10)`.
- `range(0, 10, 1)` also generates the same sequence as `range(10)` and `range(0, 10)`, as the step size of 1 is the default.
12. Here's the code for the two programs:
Using a `for` loop:
```python
for i in range(1, 11):
print(i)
```
Using a `while` loop:
```python
i = 1
while i <= 10:
print(i)
i += 1
```
13. To call the `bacon()` function from the `spam` module, you would use the following syntax:
```python
import spam
spam.bacon()
```