Skip to content

Commit 6abfb50

Browse files
committed
Add example: Not Knot!
Related to satwikkansal#1
1 parent 39480cc commit 6abfb50

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,35 @@ tuple()
14511451

14521452
---
14531453

1454+
### not knot!
1455+
1456+
Suggested by @MostAwesomeDude in [this](https://github.com/satwikkansal/wtfPython/issues/1) issue.
1457+
1458+
```py
1459+
x = True
1460+
y = False
1461+
```
1462+
1463+
**Output:**
1464+
```py
1465+
>>> not x == y
1466+
True
1467+
>>> x == not y
1468+
File "<input>", line 1
1469+
x == not y
1470+
^
1471+
SyntaxError: invalid syntax
1472+
```
1473+
1474+
#### 💡 Explanation:
1475+
1476+
* Operator precedence affects how an expression is evaluated, and `==` operator has higher precedence than `not` operator in Python.
1477+
* So `not x == y` is equivalent to `not (x == y)` which is equivalent to `not (True == False)` finally evaluating to `True`.
1478+
* But `x == not y` raises a `SyntaxError` because it can be thought of being equivalent to `(x == not) y` and not `x == (not y)` which you might have expected at first sight.
1479+
* The parser expected the `not` token to be a part of the `not in` operator (because both `==` and `not in` operators have same precedence), but after not being able to find a `in` token following the `not` token, it raises a `SyntaxError`.
1480+
1481+
---
1482+
14541483
### Loop variable resilient to changes
14551484

14561485
Suggested by @tukkek in [this](https://github.com/satwikkansal/wtfPython/issues/11) issue.

0 commit comments

Comments
 (0)