filmov
tv
Python Quiz #37: Nested FOR Loop Quiz | How many times is the print statement executed? #yasirbhutta

Показать описание
@yasirbhutta A video explains a nested loop example in Python, demonstrating how the inner loop iterates for each iteration of the outer loop.
To determine how many times the print statement will be executed, we need to consider the structure of the nested loops:
```python
for i in range(3):
for j in range(2):
print(f"i = {i}, j = {j}")
```
**Explanation:**
1. **Outer Loop (`for i in range(3):`)**:
- This loop iterates over the values `i = 0, 1, 2`. So, it runs 3 times in total.
2. **Inner Loop (`for j in range(2):`)**:
- For each iteration of the outer loop, the inner loop iterates over the values `j = 0, 1`. This means it runs 2 times for each value of `i`.
**Total Iterations:**
- For `i = 0`: The inner loop runs 2 times (`j = 0` and `j = 1`).
- For `i = 1`: The inner loop runs 2 times (`j = 0` and `j = 1`).
- For `i = 2`: The inner loop runs 2 times (`j = 0` and `j = 1`).
Since the inner loop runs 2 times for each of the 3 iterations of the outer loop, the total number of times the print statement is executed is:
3 (iterations of `i`) * 2 (iterations of `j`) = **6 times**.
**Correct Answer: C) 6 times**
#pythontips #python #python3 #pythonlearning #programming #coding #technology #machinelearning #pythonprogramming #datascience #tech #codinglife #development
To determine how many times the print statement will be executed, we need to consider the structure of the nested loops:
```python
for i in range(3):
for j in range(2):
print(f"i = {i}, j = {j}")
```
**Explanation:**
1. **Outer Loop (`for i in range(3):`)**:
- This loop iterates over the values `i = 0, 1, 2`. So, it runs 3 times in total.
2. **Inner Loop (`for j in range(2):`)**:
- For each iteration of the outer loop, the inner loop iterates over the values `j = 0, 1`. This means it runs 2 times for each value of `i`.
**Total Iterations:**
- For `i = 0`: The inner loop runs 2 times (`j = 0` and `j = 1`).
- For `i = 1`: The inner loop runs 2 times (`j = 0` and `j = 1`).
- For `i = 2`: The inner loop runs 2 times (`j = 0` and `j = 1`).
Since the inner loop runs 2 times for each of the 3 iterations of the outer loop, the total number of times the print statement is executed is:
3 (iterations of `i`) * 2 (iterations of `j`) = **6 times**.
**Correct Answer: C) 6 times**
#pythontips #python #python3 #pythonlearning #programming #coding #technology #machinelearning #pythonprogramming #datascience #tech #codinglife #development