result = 0
for i in range(1,101):
if i%2 == 0:
result = result - i
else:
result = result + i
print(result)
1-2+3-4+5 .... When you say that you are going to do it up to 100, except for even values and add odd values
It's a very basic code, but I'm asking you a question because my head is not working.
python
You've already made the code work. The shortest weave code is as follows:
result = -sum(range(0,101,2))+sum(range(1,101,2))
print(result)
result = sum([-i if i%2==0 else i for i in range(1,101)])
print(result)
If you want to shorten the code, you can do it like this...
result = 0
for i in range(1,101):
result = (result - i, result + i)[i%2 == 0]
print(result)
556 Who developed the "avformat-59.dll" that comes with FFmpeg?
572 GDB gets version error when attempting to debug with the Presense SDK (IDE)
564 Uncaught (inpromise) Error on Electron: An object could not be cloned
803 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.