increment

For loop iterate over powers of 2

For loop iterate over powers of 2 Question: I want to write a for loop that will iterate over the powers of 2 for each loop. For example I would want a range like this: 2, 4, 8, 16, … , 1024 How could I do this? Asked By: Minon Weerasinghe || Source Answers: You’ll …

Total answers: 6

Python dictionary increment

Python dictionary increment Question: In Python it’s annoying to have to check whether a key is in the dictionary first before incrementing it: if key in my_dict: my_dict[key] += num else: my_dict[key] = num Is there a shorter substitute for the four lines above? Asked By: Paul S. || Source Answers: What you want is …

Total answers: 7

Python integer incrementing with ++

Python integer incrementing with ++ Question: I’ve always laughed to myself when I’ve looked back at my VB6 days and thought, "What modern language doesn’t allow incrementing with double plus signs?": number++ To my surprise, I can’t find anything about this in the Python docs. Must I really subject myself to number = number + …

Total answers: 7

How can I increment a char?

How can I increment a char? Question: I’m new to Python, coming from Java and C. How can I increment a char? In Java or C, chars and ints are practically interchangeable, and in certain loops, it’s very useful to me to be able to do increment chars, and index arrays by chars. How can …

Total answers: 8

Is the += operator thread-safe in Python?

Is the += operator thread-safe in Python? Question: I want to create a non-thread-safe chunk of code for experimentation, and those are the functions that 2 threads are going to call. c = 0 def increment(): c += 1 def decrement(): c -= 1 Is this code thread safe? If not, may I understand why …

Total answers: 8

Behaviour of increment and decrement operators in Python

Behaviour of increment and decrement operators in Python Question: How do I use pre-increment/decrement operators (++, –), just like in C++? Why does ++count run, but not change the value of the variable? Asked By: Ashwin Nanjappa || Source Answers: ++ is not an operator. It is two + operators. The + operator is the …

Total answers: 11