What Does Indentation Mean in Python?: Optimize Your App Performance

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

What does indentation mean in Python?

Ad by DatadogHQ.com

Optimize your app performance.

Geoffrey Wiseman, Moderate Python Exp.

Indentation in a writing context typically describes the space between the page border (or page
margin) and the start of the text. In a programming context, it describes the space between the
edge of the editor and the start of the code.

Python is unusual (if not unique) among programming languages by having what is sometimes
called "significant whitespace", where the amount of space before the code starts affects the
structure of the program.

Many programming languages allow you to group statements together into blocks and run those
blocks in a loop or conditionally. Usually those blocks are delimited by some kind of identifier that
helps the program easily tell where the block begins and ends. In many C-style languages, these
delimiters are curly braces:

1. if ( condition ) {
2. do something;
3. do something else;
4. }

In languages with delimiters it is often still common and recommended practice to indent the
statements within the block so that it is easier for a human to see where the block begins and ends
without having to scan for the braces.

In python, the interpreter can tell where the block begins and ends based on whitespace alone and
the delimiters are unnecessary:

1. if condition:
2. do something
3. do something else

This does mean that if you accidentally fail to indent a line that is meant to be in a block, it won't be
in that block. However, it ensures that the meaning and the whitespace are always consistent.

There are arguments to be made for and against significant whitespace, but most programming
languages have chosen not to go this route.

Read the text again and answer the folllowing questions:

1- What is indentation? Explain in your own words.


2- What does “block” refer to? How is a block determided?
HOW TO INDENT AND DEDENT YOUR
PYTHON CODE
By Brendan Scott

You’re going to have to change the number of spaces in front of one or more lines of code. It’s

common in programming like Python. Moving them in is indenting. Moving them out

is dedenting (or deindenting).

For example, if you want to move a print statement from the main part of the program into the

code block of a loop, you need to indent it. To move it out of the code block of a loop, you

need to deindent it. IDLE has tools to indent and dedent code blocks.

Try those -denting tools:

1. Start with some code.


Here’s some:
""This is just a test file""
DEBUG = True
print('Hello World! from the editor') # hashes are used for
comments too
"" You usually use hashes at the end of a line
rather than for a block comment like this one.
""
###############################################################
# Nevertheless you can still use hashes for block comments
# Especially if you want to have a specific visual effect
###############################################################
if DEBUG:
print('I think I need another print statement in this code block')
print('See that the comments were ignored?') # even this one

2. Select the lines to indent.


Click and drag with your mouse to select the code (the last print statement), or press Shift
while using your arrow keys.
3. Choose Format → Indent Region.
Ctrl+] also works.
4. Make sure the code’s indented into a valid code block.

Indentation is meaningful to Python. You’ll get a syntax error if you have the wrong level of
indent.
It’s best to use four spaces of indent for each code block level. If you use another number of
spaces (2, 6, 8), that’s fine. The important thing is that all the code in the code block must
have the same number of spaces.
To go the other way, select the code and choose File → Dedent Region (or press Ctrl+[).

1. Read the text and find examples of:

a. Imperatives:

b. Conditional sentences

2. Try to write your own examples

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy