Python is more than just a programming language. It includes additional components which all work together as a holistic system. This course provides an introduction to some of these different components. Including the programming language itself. With the goal of introducing you to the Python programming language as a means of creating and controlling objects.
Upon completing this course you’ll be familiar with the basic concepts of language syntax, the python interpreter, and code blocks.
This course was designed for first-time developers wanting to learn Python. Existing developers may want to skip.
This is an introductory course and doesn’t require any prior programming knowledge. However, conceptual knowledge of Python objects is recommended.
Oftentimes we require multiple lines of code to be run together as one block. This concept will be our topic for this lesson.
A code block consists of lines of related code. A code block is conceptually similar to a paragraph. Which consists of a collection of sentences related to the same thought.
The purpose of a code block is to represent multiple related lines of code. It’s not important at this stage to understand the code involved in a code block. But it is important to understand the concept of a code block and how they’re defined.
So, a code block represents multiple related lines of code. Different programming languages include the concept of a block of code. And they have different ways to define a code block.
More than a few of the most common programming languages use curly brackets to define a block of code.
An opening curly bracket indicates the start of a block and a closing bracket indicates the end of the block. And all the code between the two is part of the block.
Python language syntax took a different approach for defining a code block. Python uses the concept of whitespace based indentation to create a block of code. Let’s visualize this concept to better understand how it works.
The Python interpreter determines which lines of code belong to which block based on the indentation at the beginning of each line of code. This indentation is defined by counting the number of whitespace characters. Whitespace characters consist of characters such as spaces and tabs.
So, let’s ask this question: if so many other languages use curly brackets to define a block of code, why does Python use whitespace characters?
When using whitespace characters, the only visual indicator for these characters is their effect on the characters around them.
For example, if you type 1000 spaces into a text editor, it basically looks no different than if you typed nothing. However, if you typed 10 spaces in front of some text then you’ll know those spaces exist because they move the text.
So, why does this matter? By using whitespace characters -- which are basically invisible -- Python attempts to make the language easier to read by reducing the visual clutter of repeatedly used symbols.
In my opinion, the use of whitespace for Python’s language syntax seems like a good choice. The language is designed to be visually minimalist with an almost written language feel, at times. And the use of whitespace helps towards that minimalism.
Let’s take a trip down a rabbit hole to explore whitespace based indentation.
Imagine that you want to create a mechanism that defines multiple lines of text as a single block. Using an opening and closing symbol makes for an easy solution.
Let’s borrow from other programming languages and use curly brackets. An open bracket indicates the start of a block and the closing bracket indicates the end of the block. All the lines in between are a part of the same block.
Using these symbols we could even create blocks inside of other blocks. Which gives us the ability to create nested blocks. These visible symbols make it reasonably easy to determine to which block a line belongs.
Let’s see how we’d define this with whitespace based indentation.
With whitespace based indentation we determine which lines are part of a block by the number of whitespace characters at the beginning of the line.
No whitespace characters indicate the first block. Then four whitespace characters indicate the next block. And each new level of indentation creates a nested block.
The intent of whitespace based indentation is that the lines which are in the same block are all aligned.
However, because there are multiple whitespace characters we end up with some potential problems.
First off, it’s fairly easy to accidentally include an extra space. Which can break our code. This sort of thing happens often when first learning Python’s syntax.
Second, we can’t mix and match the characters. Both spaces and tabs are white space characters. However, they’re technically two different characters.
A tab is a special character which can be configured to visually represent multiple spaces - without having to type multiple spaces.
For example you can specify that a tab represents 4 spaces. Then when you use a tab it will be one character but it will display on screen as 4 spaces. And while tabs might visually represent spaces, they are different characters.
Imagine if we want to define a block and one line starts with four spaces and the other is a tab which visually consumes four spaces. These two lines look the same to us because we can’t see which character is actually used. But they’re different to the interpreter.
This causes problems for many new Python developers. Because visually our code might be indented to the same level. However, it’s done with different whitespace characters.
The Python interpreter is particular about indention. However, it also allows us to define which character and how many of them to use for a single indentation. And while we can specify the indentation character and width, the Python community long ago settled on the use of 4 spaces to represent a single level of indentation.
I’ll be referring to this as Python’s standard indentation in all future content. Make a mental note that Python’s standard indentation is to use 4 spaces for each new level of indentation.
Okay, so how does the interpreter know which indentation character and width to use?
The interpreter reads through code one line at a time. The first line that it encounters with whitespace at the beginning of the line determines the character and width.
The interpreter counts how many of the given characters are specified before it encounters some code. And it uses that to determine the expected indentation in the rest of the code. If the interpreter encounters code that doesn’t adhere to the specified indentation level it raises an error.
Python’s whitespace based indentation can be the cause of a lot of headaches for new Python developers. Especially when copying code from the internet. If you’re seeing indentation errors when working with Python code, compare the first indentation of the file with the line mentioned in the error.
It’s common that blocks will be created inside of other blocks. And it’s worth understanding how these nested blocks relate to each other.
The interpreter considers consecutive lines of code that use the same indentation level to be a block. However, it ends the block as soon as it encounters a line of code which uses the indentation level of the outer block.
I know that all of this is a bit abstract. However, if you understand these concepts at a high level, it’ll serve as context when you begin learning Python’s syntax.
Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
- The purpose of a code block is to represent multiple consecutive related lines of code.
- A code block is defined by using whitespace based indentation.
- Python’s standard indentation defines a single level of indentation as 4 spaces.
- The runtime determines the indentation pattern based on the first line of code which begins with whitespace.
Alright, that's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.