One. Python If and for

Source: Internet
Author: User

1. Indentation and syntax

Python Indents and colons

Python is simple because of his indentation and the colon

A = 100

If a > 10:

Print (' 11111 ')

Print (' 22222 ')

"Note Indent distance"


2.if condition judgment

Format:

If judging condition:

Execute statement ....

Else

Execute statement ....


3.while Cycle

Format:

While judging condition:

Execute statement ...

Explanatory notes:

While executing, the statement is executed first, as long as it is true (true),

After executing the statement, then execute the judgment statement, generally we will set a variable in the judgment condition,

This variable is executed at the time of execution of the statement, until execution is judged false (false).

Exit, execute the program code that follows the while loop.

If a start while execution is False (false), the while loop is skipped directly.



4.for Cycle

For use in the main work to traverse lists, strings, files, etc.;

The For loop defaults to looping to the end of the element, and the for loop is formatted as follows:

Format:

For Iterating_var in sequence:

Statements (s)

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/25/fe5329daa116e3b5c58702018cd70e78.png "title=" 1.png " alt= "Fe5329daa116e3b5c58702018cd70e78.png"/>

Example:

#!/usr/bin/python

#-*-Coding:utf-8-*-

L = List (' Linan ')

For I, J in Enumerate (L):

Print (I, j)

[Note: I denotes subscript, j denotes list element]

Return:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/25/a868820ad4e3f0cdcaa01c7cca1e572d.png "title=" 2.png " alt= "A868820ad4e3f0cdcaa01c7cca1e572d.png"/>


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

For x, y in Test.iteritems ():

Print (x, y)

[Note: X indicates key,y represents value]

Return:

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/25/f693502ad3c4f2b783e40322a9812525.png "title=" 3.png " alt= "F693502ad3c4f2b783e40322a9812525.png"/>


The difference between 5.range and xrange

Range ()

Example:

Range (5)

[0, 1, 2, 3, 4]

Range (1,5)

[1, 2, 3, 4]

Range (0,6,2)

[0, 2, 4]

The xrange usage is exactly the same as range, and the difference is not a list object but a generator.

>>> 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]

So, to return 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.

Xrange does not directly generate a list, but each call returns one of the values.


Different methods of 6.break and continue

Break is used to terminate the loop, even if the loop condition does not have a false condition or the sequence has not been fully traversed, the loop statement is stopped.

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, stop the loop and jump out of the For loop

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))

The result is:

A = 0

A = 1

A = 2

A = 4

Explanation: When i = 3 o'clock, I jump out of the loop, and then the next loop continues, so a = 3 o'clock ignores the past.


This article is from the "Note space" blog, so be sure to keep this source http://286577399.blog.51cto.com/10467610/1976201

One. Python If and for

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.