I'm coming from C++.
Intuitively, in C++, here is what I'd do:
int someArray[n];
for (int i = 1 ; i < n ; i++){
someArray[i] = !(i%2);
}
where i%2 == 0
when i
is even and i%2 == 1
when i
is odd.
The !
then makes that evens are evaluated as 1
and odds as 0
, so someArray
would end up with 0
in its odd indices and 1
in its even indices.
This is an example to illustrate what I'd like to do.
Is there a way to do this in Python without the usage of control flow, simply within the mathematical expression itself? not
evaluates a boolean, and not a 1
or a 0
.
I ask because I am setting the attribute of something +-1
based on whether a certain count
is even or odd, and it is nested inside multiple other specifiers.