---
# System prepended metadata

title: Programming Practice (Slides) Answer Key

---

# Programming Practice (Slides) Answer Key

## Question 1:

![Screenshot 2025-03-14 at 10.41.26 AM](https://hackmd.io/_uploads/HkW8qJGhkx.png)

Answer:

| x1 | x2 | x1 and x2| x1 or x2 |
| -------- | -------- | -------- | --------|
| true     | true     | true     | true |
| true     | false     | false     |  true  |
| false     | true     | false     | true   |
| false     | false     | false     |  false  |


## Question 2:

![Screenshot 2025-03-14 at 10.45.03 AM](https://hackmd.io/_uploads/ryc7syfh1e.png)

Answer:
```python!=
x = 0
while x < 4:
    print(x)
    x += 1
```


## Question 3:
![Screenshot 2025-03-14 at 10.47.56 AM](https://hackmd.io/_uploads/rkU0syGn1l.png)

Answer:
```python!=
for my_var in range(100,201,2):
    print(my_var)
```
or
```python!=
for my_var in range(100, 201):
    if my_var % 2 == 0:
        print(my_var)
        
# the og loop increases by 2 every iteration
# meaning it prints only even numbers
# e.g. 100, 102, 104, ..., 200
```

Notes:
1. My bad, these aren't my prompts and I didn't look at these thoroughly apparently. I provided two versions because I didn't go over how to increment range() by more than one. 
    a. if we want to count by an increment (aka step) that is not 1 (e.g. count every two `0,2,4,5`, count every 3 `0,3,6`, etc.), we use a third argument.
    
2. I named the temp variable my_var just to keep it similar to the original while loop


## Question 4:
(Oops again)

![Screenshot 2025-03-14 at 11.28.18 AM](https://hackmd.io/_uploads/ryarBxf2ke.png)

Answer:
```python!=
i = 10

while i >5:
    print(i)
    x -= 1
```

Note: a negative "step" (i.e. the third argument in the `range()` function) causes the `range()` to count down/backwards.


## Question 5:

![Screenshot 2025-03-14 at 11.29.53 AM](https://hackmd.io/_uploads/HJ3iBlMhkg.png)

Answer (w/ explanation steps):
```
Iteration 1: x = 1
    1 % 3 == 0 is False
    output: 1
    
Iteration 2: x = 2
    2 % 3 == 0 is False
    output: 2 
    
Iteration 3: x = 3
    3 % 3 == 0 is True
    break 
    

Final Output:
1
2

```

## Question 6:

![Screenshot 2025-03-14 at 11.32.02 AM](https://hackmd.io/_uploads/HknmIlzh1l.png)

Answer (w/ explanation steps):
```
Iteration 1: x = 1
    1 % 3 == 0 is False
    output: 1
    
Iteration 2: x = 2
    2 % 3 == 0 is False
    output: 2
    
Iteration 3: x = 3
    3 % 3 == 0 is True
    continue
    
Iteration 4: x = 4
    4 % 3 == 0 is False
    output: 4
    
Iteration 5: x = 5
    5 % 3 == 0 is False
    output: 5
    
Iteration 6: x = 6
    6 % 3 == 0 is True 
    continue
    
Iteration 7: x = 7
    7 % 3 == 0 is False
    output: 7
    
Iteration 8: x = 8
    8 % 3 == 0 is False
    output: 8
    
Iteration 9: x = 9
    9 % 3 == 0 is True
    continue
    
Loop ends (because finished iterating over range)

Final output:
1
2
4
5
7
8

```


## Question 7:

![Screenshot 2025-03-14 at 11.34.51 AM](https://hackmd.io/_uploads/HyHR8ez21e.png)

Answer (w/ explanation steps):
```
Iteration 1: y = 0
    0 == 15 is False
    output: 0

Iteration 2: y = 5
    5 == 15 is False
    output: 5 

Iteration 3: y = 10
    10 == 15 is False
    output: 10
    
Iteration 4: y = 15
    15 == 15 is True
    continue
    
Iteration 5: y = 20
    20 == 15 is False
    output: 20
    
Iteration 6: y == 25
    25 == 15 is False
    output: 25
    
Loop ends (because next iteration would be y = 30 but we stop iterating at 29)

Final output:
0
5
10
20
25

```


## Question 8:

![Screenshot 2025-03-14 at 11.39.19 AM](https://hackmd.io/_uploads/B1zy_xM31g.png)

Answer:
```python!=
sum = 0
# I'm including 14 in the calculation
for x in range(8, 15):
    sum += x
    
print(sum)
```

## Question 9:
![Screenshot 2025-03-14 at 11.40.22 AM](https://hackmd.io/_uploads/H1ZQdgGhkg.png)

```python!=
import math

user_num = int(input("Enter an integer greater than 2: "))

if user_num <= 2:
    print("I said greater than 2!")
else:
    iteration_count = 0
    while x > 2:
        iteration_count += 1
        x = math.sqrt(x)   # modifies loop control variable
        print(f"{iteration_count}: {x}")   
```