Skip to content

Commit 753d0b6

Browse files
committed
Add example: For what?
* Adds a new example * Merges an existing example "Loop variable resilient to changes" in the explanation of this example Closes satwikkansal#23
1 parent 7e93e0c commit 753d0b6

File tree

1 file changed

+57
-32
lines changed

1 file changed

+57
-32
lines changed

README.md

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ So, here ya go...
8585
- [💡 Explanation:](#-explanation-19)
8686
- [Needle in a Haystack](#needle-in-a-haystack)
8787
- [💡 Explanation:](#-explanation-20)
88-
- [not knot!](#not-knot)
88+
- [For what?](#for-what)
8989
- [💡 Explanation:](#-explanation-21)
90-
- [Loop variable resilient to changes](#loop-variable-resilient-to-changes)
90+
- [not knot!](#not-knot)
9191
- [💡 Explanation:](#-explanation-22)
9292
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
9393
- [💡 Explanation:](#-explanation-23)
@@ -1481,6 +1481,61 @@ tuple()
14811481

14821482
---
14831483

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] in enumerate(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 in range(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+
14841539
### not knot!
14851540

14861541
Suggested by @MostAwesomeDude in [this](https://github.com/satwikkansal/wtfPython/issues/1) issue.
@@ -1510,36 +1565,6 @@ SyntaxError: invalid syntax
15101565

15111566
---
15121567

1513-
### Loop variable resilient to changes
1514-
1515-
Suggested by @tukkek in [this](https://github.com/satwikkansal/wtfPython/issues/11) issue.
1516-
1517-
```py
1518-
for i in range(7):
1519-
print(i)
1520-
i = 10
1521-
```
1522-
1523-
**Output:**
1524-
```
1525-
0
1526-
1
1527-
2
1528-
3
1529-
4
1530-
5
1531-
6
1532-
```
1533-
1534-
Did you expect the loop to run just once?
1535-
1536-
#### 💡 Explanation:
1537-
1538-
- [Source](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)
1539-
- 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-
15431568
### Let's see if you can guess this?
15441569

15451570
Suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue.

0 commit comments

Comments
 (0)