Introduction to Python base indentation and selection

Source: Internet
Author: User

Indent in

The most distinctive feature of Python is the code that is labeled as a block in indentation. I will use the if selection structure below for an example. If followed by the condition, if the condition is true, then a block of code that belongs 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 carry out the two assignment operations that are included in parentheses. The parentheses contain the block operation, which is subordinate to the IF.

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

If i > 0:    x = 1    y = 2

In Python, the parentheses around the i > 0 are removed, the semicolon is stripped at the end of each statement, and the curly brackets of the block disappear.

Come out more if ... After: (colon), there is the indentation of x = 1 and y = 2 preceded by four spaces. By indenting, Python recognizes that both statements are subordinate to the IF.

The reason Python is designed purely for the sake 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 x = X+1 is executed.

The print x statement is not indented, so it is outside the IF.

If you change the first sentence to i =-1, then if you encounter a false value (false), x = x+1 is subordinate to if, this sentence skips. Print x does not indent, is outside of if, does not skip, continue execution.

This indentation, with four spaces, indicates how the affiliation is written and will be seen later. Forced indentation enhances the readability of the program.

A more complex if selection:

i = 1if i > 0:    print ' positive i    = i + 1elif i = = 0:    print ' i is 0 '    i = i * 10else:    print ' Negat ive i '    i = I-1print ' new i: ', I

Here are three blocks, each belonging to if, Elif, else led.

The python detection condition, if the condition of the if is found to be false, then skip the immediately following block, detect the next elif condition, if it is false, then execute the else block.

The above structure divides the program into three branches. The program executes only one of the three branches according to the conditions.

The entire if can be placed in another if statement, which 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 '        prin T ' even better '

The block after the If > 2 is indented with four spaces relative to the if, to indicate that it is subordinate to the IF, not the outer one.

Summarize

The colon after the IF statement

The affiliation is represented by the indentation of four spaces and cannot be indented in Python

If  < conditions 1>:    Statementelif < condition 2>:    statementelif < conditional 3>:    statementelse:    Statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.