Eighth lesson: Python Indentation, statements

Source: Internet
Author: User

Before you talk about a statement, briefly say Python's indentation. For Python, code indentation is a syntax, and Python does not use {} or begin...end to separate blocks of code, as in other languages, but instead uses code indentation and colons to differentiate between code levels. The amount of whitespace to indent is variable, but all code block statements must contain the same amount of indentation whitespace, which must be strictly enforced, or the SyntaxError exception will be thrown. There are two ways to indent, the first is the TAB key, and the second is the space.

Like other programming languages, Python statements also have conditional selection and looping statements, which are similar in style to other languages, but there are some differences in writing and usage. Let's take a look at the following.

I. Conditional SELECTION statements

The keywords for conditional selection statements in Python are: If, elif, else three. Its basic form is as follows:

If < conditions 1>:

Statement

Elif < conditions 2>:

Statement

Elif < conditions 3>:

Statement

............

Else

Statement

Where the elif and else statement blocks are optional. For if and elif only if the condition or expression is true, the spoke statement executes, and the Else branch is executed only if and all of the elif conditions or expressions are false. Note the differences between conditional selection statements and other languages in Python, such as conditional or expression expressions in the C language, must be enclosed in parentheses, not in Python, but be aware that there is a colon (:) after the condition or expression.

The following examples illustrate the use of conditional selection statements: (Rating grades According to student grades, excellent, good, pass, fail)

Num=input ("Please input your source:")

If num >=85:

Print ("Congratulations, you are excellent")

elif num>=75 and num <85:

Print ("Congratulations, you ' re doing well")

Elif num>=60 and num<75:

Print ("Congratulations, you passed the exam")

Else

Print ("Sorry, you didn ' t pass the exam")

Demo effect

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201710/25/35a85451d73fd47154075b7149d6105f.png-wh_500x0-wm_3 -wmp_4-s_2466329859.png "title=" Qq20171025105640.png "alt=" 35a85451d73fd47154075b7149d6105f.png-wh_ "/>

Two. Looping statements

Like other languages, Python has its own loop statement, which provides a for loop and a while loop, with no do. While loop. the For loop usage in Python is similar to the for loop usage in Java, C #, and while loop usage is roughly similar to the C language.

For follow-through bad

The basic form of a For loop is as follows:

For variable in list:

Block

The following example shows the use of a for-based bad statement: (Calculates the and from 1 to 100)

Sum1=0

For I in Range (1,101):

Sum1+=i

Print (SUM1),

Demo effect

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201710/25/c6ecc0c4b23e993408510f7cfd1cc74e.png-wh_500x0-wm_3 -wmp_4-s_688046038.png "title=" Qq20171025105748.png "alt=" C6ecc0c4b23e993408510f7cfd1cc74e.png-wh_ "/>

Note: Range () is a built-in function that can generate a list of numbers in a range. For example, range (1,6) generates a list of [1,2,3,4,5], and a range (8) generates a list of [0,1,2,3,4,5,6,7]. There is also the xrange () function, which uses the same as range (), but returns a generator, such as xrange (1,6), that will return Xrange (1,6). Using xrange in Bad times is a lot better than range performance, especially when it comes back, because there is no need to open up a big memory space. Example

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201710/25/b79c44ee3a8717fcc3db7736bd6cb724.png-wh_500x0-wm_3 -wmp_4-s_2368092243.png "title=" Qq20171025131812.png "alt=" B79c44ee3a8717fcc3db7736bd6cb724.png-wh_ "/>

As shown above, when it comes to bad, let's look at nested loops, such as a list of lists, to iterate through each character. As follows:

list=[' China ', ' Britain ', ' French ', ' Russia ', ' Germany ', ' Dubai ', ' Auatralia '

For I in range (len (list)):

A=list[i]

Print A,

For x in range (Len (a)):

B=A[X]

Print B,

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201710/25/e19bfabe004aef1b7f3ff96ee4ddfdc2.png-wh_500x0-wm_3 -wmp_4-s_1933273123.png "title=" Qq20171025105843.png "alt=" E19bfabe004aef1b7f3ff96ee4ddfdc2.png-wh_ "/>

Note: The built-in function Len () can be used not only to calculate the length of a string, but also to find the number of members in a list or collection.

Here is a sample to show you the 99 multiplication table, the code is as follows:

For I in Range (1,10):

For j in Range (1,i+1):

Print J, "X", I, "=", J*i, "",

If j>=i:

Print (")

In this code, it is important to note that print is automatically wrapped by default, the result of the output is an entire column, the addition of commas to indicate that does not wrap, will be formatted to display the multiplication table appearance, the effect

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201710/25/81204999a88945dd7615981125621b88.png-wh_500x0-wm_3 -wmp_4-s_3965803854.png "title=" Qq20171025105053.png "alt=" 81204999a88945dd7615981125621b88.png-wh_ "/>

While loop

The basic form of a while loop:

While < conditions:

Statement

The loop is executed only if the condition is true. Once the condition is false, the loop terminates.

The following example demonstrates the use of a while-through-bad statement: (Calculates the and from 1 to 100)

I=1

Sum1=0

While i<=100:

Sum1+=i

I+=1

Print (SUM1)

If you want to terminate a loop during a block of statements, you can use break or continue. Break is jumping out of the loop, and continue is jumping out of the loop.

Here is an example of the following: (To determine whether the user entered Exit program exit, if not the output user input command, add a judge the character length of the condition)

While True:

str1 = raw_input (' Please enter a command: ')

if str1 = = ' exit ':

Break

Elif Len (STR1) < 3:

print ' The characters you entered are a little short yo, please re-enter! ‘

Continue

Else

print ' The command you entered is: ', str1

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201710/25/654efe9223899885ea45c57329e86fa5.png-wh_500x0-wm_3 -wmp_4-s_749979862.png "title=" Qq20171025112839.png "alt=" 654efe9223899885ea45c57329e86fa5.png-wh_ "/>

As you can see from the example above, the for and while loops in Python can be conditionally selected, where the ELSE clause executes when the entire loop execution condition does not match.


This article is from the "Dreamscape" blog, make sure to keep this source http://dyqd2011.blog.51cto.com/3201444/1975883

Eighth lesson: Python Indentation, statements

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.