While using Python IDLE, by how many spaces are the code suites indented

That’s it for Chapter 1. In the next chapter, you are going to learn a bit more about how Python handles data. We only just touched on lists in this chapter, and it’s time to dive in a little deeper.

Indentation is a very important concept of Python because without properly indenting the Python code, you will end up seeing IndentationError and the code will not get compiled.

Python Indentation

Python indentation refers to adding white space before a statement to a particular block of code. In another word, all the statements with the same space to the right, belong to the same code block.

Example of Python Indentation

  • Statement (line 1), if condition (line 2), and statement (last line) belongs to the same block which means that after statement 1, if condition will be executed. and suppose the if condition becomes False then the Python will jump to the last statement for execution.
  • The nested if-else belongs to block 2 which means that if nested if becomes False, then Python will execute the statements inside the else condition.
  • Statements inside nested if-else belong to block 3 and only one statement will be executed depending on the if-else condition.

Python indentation is a way of telling a Python interpreter that the group of statements belongs to a particular block of code. A block is a combination of all these statements. Block can be regarded as the grouping of statements for a specific purpose. Most programming languages like C, C++, and Java use braces { } to define a block of code. Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code. 

Example 1

The lines print(‘Logging on to geeksforgeeks…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces. The final print(‘All set!’) is not indented, so it does not belong to the else block. 

Python3




# Python program showing

# indentation

 

site= 'gfg'

 

if site== 'gfg'1 2 3 4 52

1 2 3 4 531 2 3 4 541 2 3 4 551 2 3 4 561 2 3 4 57

1 2 3 4 581 2 3 4 52

1 2 3 4 531 2 3 4 541 2 3 4 55# Python program showing31 2 3 4 57

1 2 3 4 541 2 3 4 55# Python program showing71 2 3 4 57

Output:

Logging on to geeksforgeeks... All set !

Example 2

To indicate a block of code in Python, you must indent each line of the block by the same whitespace. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to. For example, j=1 and while(j<=5): is not indented, and so it is not within the Python while block. So, Python code structures by indentation. 

Python3




# Python program showing9= # indentation1

# indentation2 

# indentation3# indentation4= # indentation6# indentation7

# indentation81 2 3 4 54site0

# indentation8# Python program showing9= # Python program showing9site5 # indentation1

Output:

1 2 3 4 5

Note: Python uses 4 spaces as indentation by default. However, the number of spaces is up to you, but a minimum of 1 space has to be used.

While using Python IDLE, 4 spaces are the code suites indented.  However, the number of spaces is up to you, but a minimum of 1 space has to be used.

Explanation:

According to PEP 8, which is the official “Python style guidelines”, the user should indent with 4 spaces.

One space per level, one tab per level, 13 spaces per level, but rather than just leave this something for every project to arbitrarily choose, PEP 8 says: “Use 4 spaces per indentation level”. 

  • Share:

Python Point Team

Previous post

How to capitalize first letter in Python

November 29, 2020

Next post

What is lambda function in Python

November 30, 2020

You may also like

“not in” belongs to which type of operator in python?

29 December, 2020

“not in” belongs to the membership operator in python. Membership operator have two operators “in” and “not in”. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary). e.g., …

What is the difference between Python 2 and 3

8 December, 2020

Python 2 and 3 got many differences between them. In this article, we will go through some important differences between them. While comparing these two, it is pretty clear that python 3 is much better than 2. Python 2 is …

How To Check If A List Is Empty In Python

7 December, 2020

In this article, we will see how to check if a list is empty or not. A list is said to be empty when there are no elements in the list. Method 1: Output: Method 2: Output:

How many space are the code Suites indented in Python idle?

Note: Python uses 4 spaces as indentation by default.

How many spaces does \t give in Python?

Python \n gives two spaces in between output and \t gives one.

How many idle are there in Python?

IDLE has two main window types, the Shell window and the Editor window.

Under which menu item will you find commands for dedent region?

See also the indent/dedent region commands in the edit menu.

Toplist

Última postagem

Tag