From the course: Hands-On Introduction: Python

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Scope and indentation

Scope and indentation

- In Python, indentation has meaning. Now, in many languages, we use indentation. In Python, however, indentation is part of the language's syntax. In Python, we use indentation to denote a code block or a unit of code that should be executed together. And a very straightforward, intuitive way of looking at this is this very naive if/else statement. You see two code blocks, both of which contain to print statements, and in the beginning, you see if True and then column. And when you have a column and in the next line, you indent with four spaces, then you're in a new code block. And then the else statement is also a code block. So we have else with a colon and then two lines to print statements that are a code block. Now, code blocks are present in many places in the language, not just if else statements. Very commonly, you'll see them in functions and methods. For example, here we have a print statement outside of the function, and then we use the def keyword to define a function…

Contents