1
0
mirror of https://github.com/satwikkansal/wtfpython synced 2024-11-22 11:04:25 +01:00

Add example: Loop variable resilient to changes

Closes https://github.com/satwikkansal/wtfPython/issues/11
This commit is contained in:
Satwik Kansal 2017-09-01 14:50:13 +05:30
parent 1a79352cc3
commit ba238cfa82

30
README.md vendored
View File

@ -1407,6 +1407,36 @@ tuple()
---
### Loop variable resilient to changes
Suggested by @tukkek in [this](https://github.com/satwikkansal/wtfPython/issues/11) issue.
```py
for i in range(7):
print(i)
i = 10
```
**Output:**
```
0
1
2
3
4
5
6
```
Did you expect the loop to run just once?
#### 💡 Explanation:
- [Source](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)
- The assignment statement `i = 10` never affects the iterations of the loop because of the way for loops work in Python. Before the beginning of every iteration, the next item provided by the iterator (`range(7)` this case) is unpacked and assigned the target list variables (`i` in this case).
---
### Let's see if you can guess this?
Suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue.