Code Indentation
Python defines code blocks using indentation - spaces that 'push' the code in from the left side of the code editor.
For example, look at this code:
if weather == "sunny":
print("It will be sunny today")
print("Wear sunscreen!")
print("Time to go to the beach...")
Some of the commands are 'pushed in' or indented with spaces (usually four) at the start of the line:
if weather == "sunny":
print("It will be sunny today")
print("Wear sunscreen!")
print("Time to go to the beach...")
The commands on lines 2 and 3 form a block of code which belongs to the if
command on line 1. The indented commands will only be run when the if
command is true (when the weather is sunny).
Note that the command on line 5 is not indented:
def report():
while True:
weather = input("Weather today: ")
if weather == "sunny":
print("It will be sunny today")
print("Wear sunscreen!")
if time == "now":
print("Ding!")
else:
print("Dong!")
print("Time to go to the beach...")
It does not belong to the if
command and will always run, whatever the weather.
How to Indent and Unindent Code
You can use the spacebar to indent code, but there is a better way...
- Press the Tab key to indent the code (push it right by four spaces)
- Press Shift + Tab to unindent the code (pull it left, removing four spaces)