Alternative
Amazon
Article
Writing
Art
AI
Angular
Photoshop
Premiere
Animal Crossing
Blog
Story
Android
Android Studio
Davinci Resolve
CSS
Clipchamp
ChatGPT
Crypto
DALL-E
Discord
Davinci Resolve
Davinci Resolve 18
Dream Studio
Express
Filmora
Flutter
PC Games
Git
GPT
GPT3
GPT4
GTA
GTA 6
Ghost Together
Ghost Together
HTML
iOS
iMovie
Illustrator
JavaScript
Mac
Mongo
Midjourney
Minecraft
Node.js
NBA 2k24
OSX
PHP
Palworld
Python
Playground AI
Roblox
React
Recipe (Cooking)
React Native
Semicolon.dev
Starfield PC Game
Snapchat
Steam
Stable Diffusion
SVG
Threads
Text To Image (AI)
VSCode
Vim
Web Development
Windows
WebGL

Python Indentation Rules

Python block scope can be created by white space or tabs.

To create a new block scope use the same number of them consistently.

i = 1

while (i <= 3):
..print(i)
..i = i + 1

Output:

1
2
3

Any number of spaces will work as long as it's consistent.

def fun():
....print("statement 1")
....print("statement 2")
....print("statement 3")

Run the function:

fun()

Output:

statement 1

statement 2

statement 3

Tabs or Spaces?

It is generally suggested to use spaces. But tabs are preference of many and can be used with same degree of success. What's important is to remain consistent. For that reason in Python 3 you can't mix spaces and tabs.

When choosing Python indentation rules you're going to follow when writing your program just make sure to keep the same number of spaces within same scope.

Incorrect indentation in Python will produce TabError error

The following is an example of improper indentation that will result in an error:

def fun():
..print("statement 1")
...print("statement 2")

The second line in function body has inconsistent number of spaces to the first line.

TabError: inconsistent use of tabs and spaces in indentation
Write For Us
Sign Up Now  -  It's Free!

Python Indentation Rules

Comments (2) New! (You can now post comments on articles!)

(By posting a comment you are registering a Ghost Together account.)
Register
Register
Post It
DM Coming Soon
f f