Is list comprehension in python an imperative program or a declarative program.
For example:
vals = [1,2,3,4,5,6,7,8,9,10]
list2 = [x+2 for x in vals] # Imperative or Declarative?
print(list2)
Is list comprehension in python an imperative program or a declarative program.
For example:
vals = [1,2,3,4,5,6,7,8,9,10]
list2 = [x+2 for x in vals] # Imperative or Declarative?
print(list2)
It is a declarative construct borrowed from Haskell, a purely functional programming language.
Python, itself, sticks mostly to an imperative paradigm, although it borrows from functional programming on a case by case basis.
Another way of looking at it, a list-comprehension expresses map/filtering operations (you can of course, abuse it for side effects, but let's stick to its primary use-case, because Python rarely forces you to do things a particular way). Those are both declarative constructs.
If you look the equivalent of this list comprehension
list(map(lambda x: x+2, range(10)))
one ca see that it is basically a declarative statement. However, this is debatable (please see also the comments). You will find arguments for both perspectives.