Classes, functions, loops, and if statements all start with headers, then show the body they contain using indentation. Many other languages use indentation for readability, but in Python, the indentation is required to show what is and isn't within the body under a header.
Most IDEs will indent automatically after any header. If it didn't, make sure you remembered your colon!
To indent in Python, you can hit the tab key, or type 4 spaces. (Most IDEs will put 4 spaces when you hit the tab key.) To unindent, delete the 4 spaces, or press SHIFT+Tab.
If you need to indent/unindent a large chunk of code, you can highlight all of the code and either hit Tab to indent, or SHIFT + Tab to unindent.
Example 1:
if is_happy:
#indented code will happen
#only if is_happy is True
hands.clap()
feet.stomp()
#unidented code is not a part
#of if block and will always run
print("Bye!")
Example 2:
if isAlive:
#happens if alive
if is_happy:
#happens if alive and happy
person.clap()
person.stomp()
else:
#happens if alive and not happy
person.cry()
#happens if alive
else:
#happens if dead
zombie.groan()
#happens whether alive or dead
If you have a header with a body that doesn't yet have text, you must put a placeholder. The pass keyword can be used as a placeholder so your code doesn't error.
if(word == "something"):
#do something
elif(word == "otherthing"):
pass
else:
#do another thing