Learning Note (October 25)--python If, while, for syntax

Source: Internet
Author: User
Tags terminates

Two weeks three sessions (October 25)


1. Learn Python's special indentation and syntax

Python Indents and colons

Python is so simple, thanks to his indentation mechanism, the strict indentation mechanism makes the code very neat and tidy, pleasing to the eye, improves readability, and improves maintainability in a certain sense. But for friends from other languages like: Java Developers, C language development, C + + developers, may have to adapt to a period of time, the code is very strict indentation format, if you do not follow the rules, accidentally will appear syntax error.

Python does not require {} As other languages do, just using the TAB key to differentiate the logic of the code.


2. Learn about the IF condition of Python

Python has a lot of logic to judge, then what to deal with, of course, the preferred is the IF statement, if followed by the condition to judge, if the following is true, only after the execution if the indentation code, if the condition is false, skip the IF statement directly.

Format:

If judging condition:

Execute statement ...

Else

Execute statement ...

Instance:

By changing the value of a to print different execution results, the code looks like this:

A = 3

If a < 0:

Print (' A is a negative number ')

elif A = = 0:

Print (' a equals 0 ')

Else

Print (' A is a positive number ')


3. Learning the while loop of Python

Many situations can be done for us to do a lot of repetitive and identical work, all through the loop to achieve, the approximate logic is as follows:

The format is:

While judging condition:

Execute statement ...

Explanatory notes:

While in the execution, the first look at the judgment statement, as long as true, executes the statement, executes the statement, then executes the judgment statement, generally we will be in the judgment condition here to set a variable, in the execution of the statement at the time of the variable to operate, until the judgment condition is false, exit, and then execute the following code, If the first judgment is false, skip the while

Example:

m = 10

Print ("Start process!")

While M > 0:

Print (' m = ' + str (m))

M-= 1

Print ("End process!")

Results:

Start process!

m = 10

m = 9

m = 8

m = 7

m = 6

m = 5

m = 4

m = 3

m = 2

m = 1

End process!


4. Learn Python for loop

The For loop is used primarily to traverse lists, strings, files, and so on, and the For loop defaults to looping until the element is finished.

The format for the For loop is as follows:

For Iterating_var in sequence:

Statements (s)

Example 1:

Test = Dict (A=1, b=2, c=3, d=4)

For I, J in Test.iteritems ():

Print (I, j)

The results are as follows:

(' A ', 1)

(' C ', 3)

(' B ', 2)

(' d ', 4)


Example 2:

Test = [' A ', ' B ', ' C ', ' d ']

For I, J in Enumerate (test):

Print (I, j)

The results are as follows:

(0, ' a ')

(1, ' B ')

(2, ' C ')

(3, ' d ')

Explanation: The enumerate () function, which separates key and value by default, automatically assigns a value of key and value by I,j in the For loop, and then all is traversed, and if it is a list, the subscript and list elements are automatically traversed.


Example 3:

For I in Xrange (1, 5):

Print (' a = ' + str (i))

Results:

A = 1

A = 2

A = 3

A = 4

Explanation: The Xrange function is often used in variable numbers, xrange the first parameter represents the start value, the second parameter represents the end value, but the maximum value can only be taken: The result value-1, as the last example shows, Xrange is to assign us a series of numbers, but for the sake of system consideration, It is not generated in memory, only generated when the Wimbledon is taken, which better utilizes the performance of the system.


5. Learn the difference between range and xrange

Range Function Description: Range ([Start,] stop[, step]), generating a sequence based on the range specified by start and stop and step set by step.

Range Example:

>>> Range (5)

[0, 1, 2, 3, 4]

>>> Range (1,5)

[1, 2, 3, 4]

>>> Range (0,6,2)

[0, 2, 4]

xrange function Description: The usage is exactly the same as range, the difference is not an array, but a builder.

xrange Example:

>>> xrange (5)

Xrange (5)

>>> List (xrange (5))

[0, 1, 2, 3, 4]

>>> xrange (1,5)

Xrange (1, 5)

>>> list (xrange (1,5))

[1, 2, 3, 4]

>>> xrange (0,6,2)

Xrange (0, 6, 2)

>>> list (xrange (0,6,2))

[0, 2, 4]


As can be known from the above example: in order to generate a large number sequence, using xrange will be much better than range performance, because there is no need to open up a large amount of memory space.

The result of the output is the same, in fact there are many different, range will directly generate a list object, and xrange will not directly generate a list, but each call returns one of the values.


6. Learn different ways to break and continue

6.1 Break

The break statement terminates the loop and terminates the loop even if the loop condition does not have a false condition or the sequence has not been fully traversed.

Example 1:

For I in Xrange (10):

If I > 3:

Break

Print (' a = ' + str (i))

Execution Result:

A = 0

A = 1

A = 2

A = 3

When a = 4 o'clock, the loop is stopped, so all subsequent operations do not make sense, just jump out of the loop end.


6.2 Continue

The continue statement jumps out of the loop, and break jumps out of the loop.

The continue statement is used to tell Python to skip the remaining statements of the current loop, and then proceed to the next round of loops.


Example:

For I in Xrange (5):

if i = = 3:

Continue

Print (' a = ' + str (i))

Results:

A = 0

A = 1

A = 2

A = 4

Explanation: When i = 3 o'clock, just jump out of the loop, and then the next cycle continues, so a = 3 this ignores the past!


Learning Note (October 25)--python If, while, for syntax

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.