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?
(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?
(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.
(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.
Do you understand how if
and for
work and that the indentation determine which statements are part of those blocks?
(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.
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.
(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.
(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