Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of indentation in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "the use of indentation in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the use of indentation in Python.

Indent

The most distinctive feature of Python is the code marked as blocks with indentation. Let me take the if selection structure as an example. The if is followed by a condition, and if the condition is true, a block of code belonging to the if is executed.

First look at the expression of the C language (note that this is C, not Python!)

If (I > 0) {x = 1; y = 2;}

If I > 0, we will do the two assignments included in parentheses. What is contained in parentheses is the block operation, which belongs to if.

In Python, for the same purpose, this passage goes like this

If I > 0: X = 1 y = 2

In Python, the parentheses around I > 0 are removed, the semicolon at the end of each statement is removed, and the curly braces indicating the block are also gone.

There's more, if. This is followed by: (colon), and there is the indentation of four spaces before x = 1 and y = 2. By indenting, Python recognizes that these two statements belong to if.

The reason for Python's design is purely for the beauty of the program.

If statement

Write a complete program named ifDemo.py. This program is used to implement the if structure.

I = 1x = 1if I > 0: X = x+1print x

$python ifDemo.py # run

When the program runs to if, the condition is True, so execute x = x.

The print x statement is not indented, so it is outside of if.

If you change the first sentence to I =-1, then if encounters a false value (False), and x = x belongs to if, which is skipped. Print x is not indented, it is outside of if, do not skip, continue to execute.

This way of expressing affiliation in the indentation of four spaces will be seen in the future. Forced indentation enhances the readability of the program.

A more complex choice of if:

I = 1if I > 0: print'positive i'i = I + 1elif I = 0: print'i is 0'i = I * 10else: print 'negative i'i = I-1print' new iRu I

Here are three blocks, which belong to if, elif, and else leader.

Python detection condition, if the condition of if is found to be false, then skip the following block and detect the condition of the next elif; if it is still false, then execute the else block.

Through the above structure, the program is divided into three branches. The program executes only one of the three branches according to the condition.

The entire if can be placed in another if statement, that is, the nested use of the if structure:

I = 5if I > 1: print'i bigger than 1 'print' good' if I > 2: print'i bigger than 2 'print' even better'

The block after if I > 2 indents four spaces relative to the if to indicate that it belongs to the if, not the outer if.

Summary

The colon after the if statement

Membership is indicated by indentation of four spaces, and cannot be indented at will in Python

If:

Statement

Elif:

Statement

Elif:

Statement

Else:

Statement

At this point, I believe you have a deeper understanding of "the use of indentation in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report