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