You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+57-32Lines changed: 57 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -85,9 +85,9 @@ So, here ya go...
85
85
-[💡 Explanation:](#-explanation-19)
86
86
-[Needle in a Haystack](#needle-in-a-haystack)
87
87
-[💡 Explanation:](#-explanation-20)
88
-
-[not knot!](#not-knot)
88
+
-[For what?](#for-what)
89
89
-[💡 Explanation:](#-explanation-21)
90
-
-[Loop variable resilient to changes](#loop-variable-resilient-to-changes)
90
+
-[not knot!](#not-knot)
91
91
-[💡 Explanation:](#-explanation-22)
92
92
-[Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
93
93
-[💡 Explanation:](#-explanation-23)
@@ -1481,6 +1481,61 @@ tuple()
1481
1481
1482
1482
---
1483
1483
1484
+
### For what?
1485
+
1486
+
Suggested by @MittalAshok in [this](https://github.com/satwikkansal/wtfpython/issues/23) issue.
1487
+
1488
+
```py
1489
+
some_string="wtf"
1490
+
some_dict= {}
1491
+
for i, some_dict[i] inenumerate(some_string):
1492
+
pass
1493
+
```
1494
+
1495
+
**Outuput:**
1496
+
```py
1497
+
>>> some_dict # An indexed dict is created.
1498
+
{0: 'w', 1: 'f', 2: 'f'}
1499
+
```
1500
+
1501
+
#### 💡 Explanation:
1502
+
1503
+
* A `for` statement is defined in the [Python grammar](https://docs.python.org/3/reference/grammar.html) as:
1504
+
```
1505
+
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else'':' suite]
1506
+
```
1507
+
Where `exprlist`is the assignment target. This means that the equivalent of `{exprlist} = {next_value}`is**executed for each item**in the iterable.
1508
+
An interesting example suggested by @tukkek in [this](https://github.com/satwikkansal/wtfPython/issues/11) issue illustrates this:
1509
+
```py
1510
+
for i inrange(4):
1511
+
print(i)
1512
+
i=10
1513
+
```
1514
+
1515
+
**Output:**
1516
+
```
1517
+
0
1518
+
1
1519
+
2
1520
+
3
1521
+
```
1522
+
1523
+
Did you expect the loop to run just once?
1524
+
1525
+
**💡 Explanation:**
1526
+
1527
+
- 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(4)` this case) is unpacked and assigned the target list variables (`i`in this case).
1528
+
1529
+
* The `enumerate(some_string)` function yields a new value `i` (A counter going up) and a character from the `some_string`in each iteration. It then sets the (just assigned) `i` key of the dictionary `some_dict` to that character. The unrolling of the loop can be simplified as:
1530
+
```py
1531
+
>>> i, some_dict[i] = (0, 'w')
1532
+
>>> i, some_dict[i] = (1, 't')
1533
+
>>> i, some_dict[i] = (2, 'f')
1534
+
>>> some_dict
1535
+
```
1536
+
1537
+
---
1538
+
1484
1539
### not knot!
1485
1540
1486
1541
Suggested by @MostAwesomeDude in [this](https://github.com/satwikkansal/wtfPython/issues/1) issue.
- 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).
1540
-
1541
-
---
1542
-
1543
1568
### Let's see if you can guess this?
1544
1569
1545
1570
Suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue.
0 commit comments