In some other programming languages, indentation is generally used to improve readability but by itself is not required. Semi-colons or curly braces are used to signal end of statements and for code blocks. Even if indentation is not used, the code is still valid as long as it adheres to the syntax of the language.
In Python, indentation is important and is not optional.
Consider the following variations in indentation and how they affect the program.
def same_values(lst1, lst2):
new_list = []
for num in range(len(lst1)):
if lst1[num] == lst2[num]:
new_list.append(num)
print(num)
return new_list
Here, the return
statement has the same indentation level as the if
statement. Both the if
statement and the return
statement are part of the for
loop’s body. The for
loop will be executed and the first iteration will begin. If the condition is true, the statements in the body of the if
statement will be executed. If not, the statements in the body of the if
statement will be skipped. Then, the return
statement will be executed. You won’t get to the second iteration of the for
loop. Only one iteration of the for
loop will be executed and as soon as the return
statement is encountered, you will exit the function.
def same_values(lst1, lst2):
new_list = []
for num in range(len(lst1)):
if lst1[num] == lst2[num]:
new_list.append(num)
print(num)
return new_list
Here the return
statement has the same indentation as the other statements in the body of the if
statement. So, this makes the return
statement a part of the if
statement’s body. The for
loop will be executed and the first iteration will begin. If the condition is false, then nothing will happen and you will move on to the next iteration of the for
loop. If the condition is true, the statements in the body of the if
statement will be executed. After appending and printing num
, you will again return
immediately causing you to exit the function.
def same_values(lst1, lst2):
new_list = []
for num in range(len(lst1)):
if lst1[num] == lst2[num]:
new_list.append(num)
print(num)
return new_list
Here, the return
statement has the same indentation as the new_list = []
statement and the for
loop. This means that the return
statement is part of the same_values
function’s body. Now, new_list
will be initialized as the empty list, the for
loop will run to completion. After the for
loop has finished, new_list
will be returned.
Consider the following example:
#
# Version A:
def func():
for i in [3, 5, 4, 7, 8]:
print(i)
if (i % 2 == 0):
print(f"{i} is Even!")
return "Done"
print(func())
# Output:
3
Done
# Version B
def func():
for i in [3, 5, 4, 7, 8]:
print(i)
if (i % 2 == 0):
print(f"{i} is Even!")
return "Done"
print(func())
# Output:
3
5
4
4 is Even!
Done
# Version C
def func():
for i in [3, 5, 4, 7, 8]:
print(i)
if (i % 2 == 0):
print(f"{i} is Even!")
return "Done"
print(func())
# Output:
3
5
4
4 is Even!
7
8
8 is Even!
Done
Play with a few examples of your own to better understand how indentation affects the results.