Data Science and Computing with Python for Pilots and Flight Test Engineers
Code Blocks and Indentation
Introduction to Code Blocks
A code block is a piece of code that belongs together and is executed as a unit. We will encounter many different code blocks later, such as functions, class definitions, the body of a for loop, etc. At in this lesson, we shall only concern ourselves with the syntax, how to tell Python that something is a code block. If you are interested to learn more about code blocks, let us refer you to the corresponding section of the official Python documentation.
In C/C++ such semantic units of code are enclosed in curly braces. In Python, this works a bit differently. Indentation of the corresponding lines of code is used instead. This forces the programmer to “style” the code properly and can make it visually sometimes easier, but because no actual visible characters are used to mark the beginning and the end of a code block, this can also become quite messy and can pose a challenge, when you have to modify a code.
We will therefore want to learn in this lesson, how to do code block indentation properly in Python and what tools we can use to simplify the typesetting process. The code below illustrates a code block; in this example it is the body of a function “plusone”, which takes the variable \(x\) as an argument and returns the variable \(x\) again as output, after modifying it. We will learn about functions later. For now just notice how the lines are indented, which make up the body of the function until the return statement.
def plusone(x):
""" Function which adds one to a variable and returns the new value. """
x = x + 1
return x
Python Indentation Philosophy
In principle, you can mark code blocks with indentation by as many as spaces you like. Even just one space, if you like (though this becomes hard to see). Every line with the same number of spaces at the beginning is perceived as belonging to this code block (until the block ends). By convention, however, you are supposed to use four spaces for each indentation level in Python. Do not use tabs (Tab key) for this, as you would in a shell script. Tabs should in fact be avoided in Python for indentation.
At first using four spaces for each indentation level seems like an easy thing to remember and do. But imagine that you have 50 lines of already written code, which you now all need to be indented by 4 spaces, because you want to put them into a new code block. Going into each line of the 50 lines of code and typing four spaces at the beginning is not only very tedious, but also prone to error (if you miss a space or a line somewhere, the code will stop working, or worse, will work incorrectly and you will spend time finding out, what went wrong).
For this reason, various text editors have tools to assist with indentation like this. In the section below, we will look how indentation is handled conveniently in the Python code cells of the Jupyter notebooks, which is what we work with in this introductory Python course.
Practical Indentation Tools in Jupyter Notebooks
In Jupyter notebooks, contrary to what we just stated above, you should actually use the Tab key to make code block indentations! This is because the Tab key in a Jupyter notebook cell is automatically realized by the notebook as four spaces. You press Tab, and four true spaces are created instead.
The Tab key has one other very important property. You can use it to indent and unindent many lines of code at once! Normally, if you would highlight several lines of code and press the space key (four times), the lines would all disappear and be replaced by one (or four) spaces. This is obviously not what we want for indentation.
In a Jupyter notebook code cell, if you highlight several lines of code with the cursor first and then press the Tab key, the text of all lines will remain present, yet all lines will be indented by four spaces. This is super convenient. What if you need to remove the indentation later, because an underlying code block disappeared? No problem. Press Shift + Tab key together, and four spaces will be removed. This works for multiple lines of code as well, if you select them prior with the cursor.
Do practice this, e.g. with the piece of code featured above, to internalize how it works.
Note though that in a regular text editor, a tab remains a tab (and does not become four spaces). If text is copy and pasted from such a text editor into a Jupyter notebook code cell, the tabs are not automatically replaced by four spaces, but rather represented as thin right arrows, to visually distinguish them.