Python Indentation

Cai Gengyang gengyangcai at gmail.com
Sat Aug 13 06:52:19 EDT 2016


Indentation matters. Each line under the if statement that is indented will only be executed if the statement is true:

if a == 1:
    print("If a is one, this will print.")
    print("So will this.")
    print("And this.")
 
print("This will always print because it is not indented.")

Indentation must be the same. This code doesn't work.

if a == 1:
  print("Indented two spaces.")
     print("Indented four. This will generate an error.")
    print("The computer will want you to make up your mind.")

Why does the first example work and the 2nd example doesn't work ? Can someone explain in layman's terms what indentation is and why the first one works and the 2nd one doesn't ? Thanks alot

Gengyang



More information about the Python-list mailing list