Python Process Control

Source: Internet
Author: User

Condition test:

An If condition test expression

Different types of comparisons: numbers: Comparing sizes

String: Per-character comparison by ASCII code table

Lists and tuples: Comparing the contents of each part from left to right

Dictionaries: Comparing sorted (key, value) lists

True and False in Python:

1, non-0 numbers and non-empty objects are true

2, number 0, empty object and none is False

3. Comparisons and equivalence tests are applied recursively to data structures

4, the return value is TRUE or False

Combination condition test:

X and y, x or y, not X

If syntax structure

If Boolean_expression1:

Suite1//Indent 4 characters

A = X If Y else Z:

If Y:

A = X

Else

A = Z

expression1 If boolean_expression else expression2

While and for loops

While loops are used to write common iteration structures

A For loop is a generic sequence iterator

Python Stealth Iteration Tool:

In member relationship testing; list parsing; map, reduce, filter functions

While Boolean_expression:

While_suite

Else

Else_suite

Else optional, boolean_expression end loop When the result is false, execute else.

Print x, which can be displayed on a single line in a non-wrapped line

Break: Jump out of the inner loop

Continue: advance to the next cycle and move to the beginning of the nearest layer loop.

ELSE code block: The loop terminates normally and else will not execute if the loop termination is caused by a break.

Range generates a sequence directly in memory before the for loop starts. Xrange is a data element that is generated at a time, one out, and can save memory resources.

The For loop executes faster than while.

Zip: Returns a list of tuples of parallel elements, often traversing several sequences in a for loop.

L1 = [1,2,3,4,5,6]

L2 = [' A ', ' B ', ' C ', ' d ', ' e ', ' F ']

Zip (L1,L2)

[(1, ' a '), (2, ' B '), (3, ' C '), (4, ' d '), (5, ' E '), (6, ' F ')]

Zip is used to construct the dictionary dynamically:

D = {}
for (k,v) in Zip (L1,L2):d [k] = V
D
{1: ' A ', 2: ' B ', 3: ' C ', 4: ' d ', 5: ' E ', 6: ' F '}

Python Process Control

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.