20171025_python study two week three lessons

Source: Internet
Author: User

Today's Mission:

1. Learn Python's special indentation and syntax
2. Learn about the IF condition of Python
3. Learning the while loop of Python
4. Learn Python for loop
5. Learn the difference between range and xrange
6. Learn different ways to break and continue
corresponding to the ape lesson Chapter 5.1-5.4

Notes:

1. Indentation and algorithm:

A python conditional statement determines the code block that executes by executing the result of one or more statements (true or false).

The Python program language specifies any non-0 and non-null (NULL) values of true,0 or NULL to FALSE.

Python indent: 4 separators for one indent

2. If condition judgment

In Python programming, if statements are used in the execution of control programs, in the basic form:

If judging condition:
Execute statement ...
Else
Execute statement ...

Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range.

Else as an optional statement, you can execute the relevant statement when you need to execute the content when the condition is not true, as shown in the following example:

The judging condition of the F statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal) to indicate its relationship.

When judging a condition as multiple values, you can use the following form:

If judgment condition 1:
Execute Statement 1 ...
Elif Judgment Condition 2:
Execute Statement 2 ...
Elif Judgment Condition 3:
Execute Statement 3 ...
Else
Execute Statement 4 ...

3.while Cycle

In Python programming, a while statement is used to loop the execution of a program, which, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:

While judging condition:
Execute statement ...

4.for Cycle

A Python for loop can traverse any sequence of items, such as a list or a string.

Grammar:

The syntax format for the For loop is as follows:

5. Range and xrange are different

The Python range () function creates a list of integers, which are typically used in a for loop.

Different methods of 6.break and continue

The Python break statement , like in the C language, breaks the minimum enclosing for or while loop.

The break statement terminates the Loop statement, that is, the loop condition does not have a false condition, or the sequence is not fully recursive, and the loop statement is stopped.

The break statement is used in the while and for loops.

If you use a nested loop, the break statement stops executing the deepest loop and starts executing the next line of code.

The Python 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.

The continue statement is used in the while and for loops.

20171025_python study two week three lessons

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.