Skip to content

Commit 5467a67

Browse files
committed
Add minor example
Adds an example to illustrate absence of Increment and Decrement operators in Python. Related to satwikkansal#30
1 parent 8304eb7 commit 5467a67

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,22 @@ a, b = a[b] = {}, 5
16331633
+ `'a'[0][0][0][0][0]` is also a semantically correct statement as strings are iterable in Python.
16341634
+ `3 --0-- 5 == 8` and `--5 == 5` are both semantically correct statements and evaluate to `True`.
16351635

1636+
* Given that `a` is a number, `++a` and `--a` are both valid Python statements, but don't behave the same way as compared with similar statements in languages like C, C++ or Java.
1637+
```py
1638+
>>> a = 5
1639+
>>> a
1640+
5
1641+
>>> ++a
1642+
5
1643+
>>> --a
1644+
5
1645+
```
1646+
1647+
**💡 Explanation:**
1648+
+ There is no `++` operator in Python grammar. It is actually two `+` operators.
1649+
+ `++a` parses as `+(+a)` which translates to `a`. Similarly, the output of the statement `--a` can be justified.
1650+
+ This StackOverflow [thread](https://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python) discusses the rationale behind the absence of increment and decrement operators in Python.
1651+
16361652
* Python uses 2 bytes for local variable storage in functions. In theory, this means that only 65536 variables can be defined in a function. However, python has a handy solution built in that can be used to store more than 2^16 variable names. The following code demonstrates what happens in the stack when more than 65536 local variables are defined (Warning: This code prints around 2^18 lines of text, so be prepared!):
16371653
```py
16381654
import dis

0 commit comments

Comments
 (0)