


Not only does Python require this indentation so that it can recognize the code blocks, but the consistent indentation makes it easy for people to recognize the if/elif-conditions and their corresponding code blocks.
Python code blocks free#
So first it checks if age is less than 2, and if so, it indicates that the flying is free and jumps out of the elif-condition. The following program determines how much a passenger should pay: # airfare.pyĪfter Python gets age from the user, it enters the if/elif-statement and checks each condition one after the other in the order they are given. For example, suppose an airline has the following “child” ticket rates: Kids 2 years old or younger fly for free, kids older than 2 but younger than 13 pay a discounted child fare, and anyone 13 years or older pays a regular adult fare. Statements within the same block of code need to be indented at the same level.Īn if/elif-statement is a generalized if-statement with more than one condition.

But in Python, it is required for indicating what block of code a statement belongs to. In most other programming languages, indentation is used only to help make the code look pretty. The two blocks of code in our example if-statement are both indented four spaces, which is a typical amount of indentation for Python.

To indicate a block of code in Python, you must indent each line of the block by the same amount. These ones happen to be only a single line long, but Python lets you write code blocks consisting of any number of statements. ') and print('Incorrect password.') are two separate code blocks. Consider the if-statement from our simple password-checking program: if pwd = 'apple': One of the most distinctive features of Python is its use of indentation to mark blocks of code. Learn More Buy Code Blocks and Indentation P圜harm is one of the most popular Python editors.Python: Visual QuickStart Guide, 2nd Edition Let’s dive into the most popular editors one-by-one. Table: Shortcuts to block-comment and -uncomment a given text selection for different Python editors. Have a look at the following table that showcases the shortcut to toggle block comments for a given selection of text: Editor
Python code blocks how to#
So, how to comment out a block of Python code? You can see both examples here: # Block Comment Method 1 However, Python takes those triple-quote strings as docstrings.
