I have this function:
numbers = [3, 4, 6, 7]
for x in numbers:
a = 5 - x
b = 5 + x
c = 5 * x
print(x, a, b, c)
What it exactly does doesn't matter, only the x is relevant.
I want modify x so that:
for x in numbers:
a = 5 - (x + 2)
b = 5 + (x + 2)
c = 5 * (x + 2)
print((x + 2), a, b, c)
But obviously adding + 2 everywhere is annoying, so I just want to have another value for x.
Of course, I could make another variable like this:
for x in numbers:
modifiedX = x + 2
a = 5 - modifiedX
b = 5 + modifiedX
c = 5 * modifiedX
print(modifiedX, a, b, c)
But I'm curious if I could get the same result without adding another line, like:
for x + 2 in numbers:
a = 5 - x
b = 5 + x
c = 5 * x
print(x, a, b, c)
or this:
x + 2 for x in numbers:
a = 5 - x
b = 5 + x
c = 5 * x
print(x, a, b, c)
The last 2 code blocks aren't correct Python syntax, so I'm curious: Is there is a correct method out there to have a modified version of x without adding more lines?
Note: I still want to keep the original numbers list, so I'm not looking for changing the numbers in the list directly.