I am a newbie for python and trying to understand the below piece.
[i**+1 for i in range(3)]
I know that i += 1
means i = i + 1
but what does **+
mean?
I know **
means exponent but the output of the above list comprehension puzzling me.
I am a newbie for python and trying to understand the below piece.
[i**+1 for i in range(3)]
I know that i += 1
means i = i + 1
but what does **+
mean?
I know **
means exponent but the output of the above list comprehension puzzling me.
This confusion is a result of not surrounding operators with whitespaces as PEP8 suggests.
[i**+1 for i in range(3)]
is the same as [i ** +1 for i in range(3)]
which is the same as [i ** 1 for i in range(3)]
.
The output of [i**+1 for i in range(3)]
is [0, 1, 2]
which is the expected output.
**+1
is simply ** +1
which is the 1st positive power. The +
is redundant here.
This is similar to: i to the power of 1 i.e +1
[i**+1 for i in range(3)]
NOTE: +1 is not an addition here. it is the power of i
i.e.
o to the power of +1 = 0;
1 to the power of +1 = 1;
2 to the power of +1 = 2;
so the answer is: [0, 1, 2]