Home

Search IconIcon to open search

Python quirks

# Modulo

Modulo and integer division works differently from C! In C it’s:

1
2
3
4
7 % 3 = 1
-7 % 3 = -1
7 % -3 = 1
-7 % -3 = -1

But in Python:

1
2
3
4
5
6
7
8
>>> 7 % 3
1
>>> -7 % 3
2
>>> 7 % -3
-2
>>> -7 % -3
-1

Aaand for Python’s Decimal it’s different. So yeah, more ways to shoot yourself in the foot.

# Tuples

This is not a tuple:

1
a = (123) # int

This is a tuple:

1
b = (123,) # tuple

# Call stack limit

By default, call stack is limited to 1000 frames. Weird, but OK.