What is the most efficient way to toggle between 0 and 1?
0
1
Switching between -1 and +1 can be obtained by inline multiplication; used for calculation of pi the 'Leibniz' way (or similar):
sign = 1 result = 0 for i in range(100000): result += 1 / (2*i + 1) * sign sign *= -1 print("pi (estimate): ", result*4)