Question on white spaces


#1

From exercise 6, may i know what is indention (ie the 4 spaces in the line after a colon on the previous line) used for? From another thread, i understand that it determines what is nested in a function. Can anyone explain to me in layman terms what does "determining what is nested in a function" means? And in simple terms, i would like to understand under what circumstances is an indention not required after a colon.


#2

after a colon, the next line always needs indention. Where many language use brackets (JS example):

if (x == 5){
     console.log("indention doesn't matter now")
console.log("given you use curly brackets")
     console.log("what is part of the if clause")
}

where on the hand in python:

if x == 5:
     print "indention does matter"
     print "to determine what inside the if clause"
     print "given there are no curly brackets"

uses indention


#3

I understand that the reason why there must be an indention after a colon is so that what is nested in a function can be determined. May i know what that means in layman terms?


#4

well, functions aren't covered yet. We can have function:

def example():
    print "hello world"

in the code above, the indent ensures the print statements are part of if clause. Just like the indent here ensures the print statement is part of function content.


#5

I see i see! Thank you! :slight_smile:


#6

I still don't understand the exact use of whitespaces.Could you please explain them in a little more detail? Thanks!


#7

How i am suppose to explain this in more detail? Python uses indention to determine what is nested in a if, elif and else clause, for loop, function and so on.

Not sure which details to add


#8

Thank you stetim94, I could understand it now.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.