Incrementing and Decrementing in PHP vs Python

On of the most common thing in nearly every language is how to increment and decrement in variables, mostly used for iteration (like for, while, do-while) and recursion, which is a special kind of iteration. PHP and Python are using different approaches for incrementing and decreminting. In PHP it is possible to use the common form of '$var++' to increment and '$var--' to decrement. Another common form is '$var+=1'. In Python the form of '++' doesn't work, but it is possible to '+=' to increment and '-=' to decrement. Python has additionally two functions for incrementing: 'enumerate()' and 'itertools.count()'. The function 'enumerate' works in conjunction with an iterable element, for example an iterator, or a list element. By default 'enumerate' starts with 0, but is possible to use to reset it with an additional parameter, which is called 'start' for example I want to iterate over my list element, which I have called 'myList' and I want to start by '1', code look like this: enumerate(myListe, start=1). The other way is to use the itertools.count method (or a function?). It is possible to set two parameters, one for start and the second for steps. With steps it is possible to decrement. For example starting with '1' and step with '-2'. The code look like this: count(start=1, step=-1) To summarize that topic, Python and PHP have completely different ways to iterate. Python has more modern approaches than PHP.

Comments

nice solution
Gregor Liebenberger - Fri, 11/23/2018 - 11:02 :::
I did not know about this, I actually like the elegance of the solution! I guess at some point I will face a similar problem when using Python so I keep this in mind
Klaus Nigsch - Sun, 11/25/2018 - 13:10 :::
Interesting and well detailed solution. Thank you
Carole Sebah - Mon, 12/10/2018 - 16:56 :::
No answers yet.