Indentation levels and scope (Python)

Discussion in 'Mac Programming' started by unbelly, Mar 27, 2012.

  1. macrumors newbie

    Joined:
    Mar 1, 2012
    #1
    Hey there. I'm going through a tutorial on python and programming for beginners, have been following up until now but am having some trouble understanding what exactly they mean by 'indentation level' here:


    I just want to double check that I properly understand what they mean by 'indentation level'.
    Are they talking about the line spaces between the lines of code (as in a space between two lines) or about how far inside a line the code is indented? Or both?
     
  2. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #2
    Code:
    Z = 5
    
    def f(x):
    return x + Z
    
    print(f(3))
    
    I would check the tutorial you are doing because what you posted above should probably look like the code below.

    Code:
    Z = 5
    
    def f(x):
        return x + Z
    
    print(f(3))
    
    When writing functions in languages like Java or C++, the statements belonging to a function are grouped by { }.

    Code:
    void myFunction()
    { 
        //do something
    }
    
    However, Python uses indention instead of the {} to group statements belonging to a function so the function would be

    Code:
    void myFunction():
        //do something
     

Share This Page