Python Forum
Different Indentation Different Result - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Different Indentation Different Result (/thread-24382.html)

Pages: 1 2


Different Indentation Different Result - el_bueno - Feb-11-2020

sump=0
for num in listp:
    if num%2==0:
        sump=sump+num
        print(sump
sump=0
for num in listp:
    if num%2==0:
        sump=sump+num
    print(sump)
sump=0
for num in listp:
    if num%2==0:
        sump=sump+num
print(sump)
Here are technically the same code, but as I play around with the indentation of print(sump) Im having differente results.

Could someone explain to me why is it about the indentation of print that makes the result different every time?


RE: Different Indentation Different Result - buran - Feb-11-2020

(Feb-11-2020, 09:01 PM)el_bueno Wrote: Here are technically the same code
well, technically it's not the same code due to different indentation.
The difference is self evident given the output. what is not clear?


RE: Different Indentation Different Result - michael1789 - Feb-11-2020

(Feb-11-2020, 09:01 PM)el_bueno Wrote: Here are technically the same code
They aren't the same. Each one tells it to print at different times.

The first one is after a for and if statements, the second is only in the for loop, and the last is global. The indentation is changing what needs to be the case in order to print sump.

Indentation is of paramount importance and changes everything. It is a huge factor in when and if a piece of code gets executed.


RE: Different Indentation Different Result - el_bueno - Feb-12-2020

(Feb-11-2020, 09:17 PM)buran Wrote:
(Feb-11-2020, 09:01 PM)el_bueno Wrote: Here are technically the same code
well, technically it's not the same code due to different indentation.
The difference is self evident given the output. what is not clear?

what is not clear is why I get different result with different indentation, I Want to understand the different results.


RE: Different Indentation Different Result - ndc85430 - Feb-12-2020

Do you understand how if and for work and that the indentation determine which statements are part of those blocks?


RE: Different Indentation Different Result - el_bueno - Feb-12-2020

(Feb-12-2020, 07:35 PM)ndc85430 Wrote: Do you understand how if and for work and that the indentation determine which statements are part of those blocks?

I'm a complete beginner so It should help me if you would explain it to me. Pray


RE: Different Indentation Different Result - ndc85430 - Feb-12-2020

Out of interest, how are you learning the language? If you don't have a decent book or material, I'd suggest that you should get one.


RE: Different Indentation Different Result - Clunk_Head - Feb-12-2020

(Feb-12-2020, 08:09 PM)el_bueno Wrote:
(Feb-12-2020, 07:35 PM)ndc85430 Wrote: Do you understand how if and for work and that the indentation determine which statements are part of those blocks?

I'm a complete beginner so It should help me if you would explain it to me. Pray

Read this page about indentation from w3schools


RE: Different Indentation Different Result - el_bueno - Feb-13-2020

(Feb-12-2020, 08:29 PM)ndc85430 Wrote: Out of interest, how are you learning the language? If you don't have a decent book or material, I'd suggest that you should get one.

Im on Udemy with a course Called Complete Python Bootcamp Go from zero to hero in Python 3, I would stick to the course, but I need sometimes to go further and ask myself questions like the one I'm asking here so I can go deeper to fully understand Python.

And I read some articles along the way before I post a question here.

And if you have any thing interesting about indentation I would much appreciate.


RE: Different Indentation Different Result - michael1789 - Feb-13-2020

(Feb-13-2020, 12:19 AM)el_bueno Wrote: And if you have any thing interesting about indentation I would much appreciate.

Indentation is really basic to how it all works. You won't find any special writing on it any more than a collage English book is going to review the order of the alphabet. You have to cover indentation is your learning of the language in general.

watch this about loops:
https://www.youtube.com/watch?v=6iF8Xb7Z3wQ