Python Control Structure

Source: Internet
Author: User

Python is a process control that relies on tab tabs and line breaks.Programming LanguageSo you must note that its white space characters areCode.

 

I. Condition Selection statement

1. If statement

Code
If X < Y:
  Print   " X <Y "
Elif X = Y:
  Print   " X = y "
Else :
  Print   " X> Y "

Elif is short for else if. Each judgment statement is followed by a colon:

The end of the IF statement block is identified by a blank line.

 

 

Ii. Cyclic statements

1. For... in... statement

Code
A = [ ' Cat ' , ' Window ' , ' Description ' ]
For X In A
  Print X, Len (X)

Similarly, the end of the for... in... statement is also ended by an empty row.

 

 

3. Range () function

The range function can generate a linked list.

When there is only one parameter, It is a linked list from 0 to which the step is 1.

When there are two parameters, it is a linked list with Step 1 from parameter 1 to parameter 2.

When there are three parameters, it is a linked list with Step 3 from parameter 1 to parameter 2.

 

 

4. The break statement and the continue statement, as well as the else statement in the loop.

Break indicates that the loop exists.

Continue indicates ignoring the following statements and entering the condition judgment of the next loop.

 

 

The else of the for... in... statement indicates the statement executed when the loop is not entered. The Code is as follows:

1 For X In Range (0)
2   Print X
3 Else
4   Print   ' End '
5
6

 

5. Pass statements

Similar to an empty statement in C, that is, there is only one semicolon;

 

6. define functions

Syntax: Def ..(..):

The Code is as follows:

 

1 Def Leesay (STR ):
2   Print   ' Lee: ' , STR
3
4

 

7. In-depth Function Definition

 

1. default values

1 Def Say (S, P = " Lee " ):
2   Print P, " : " , S
3
4

Note that parameters with default values must be placed at the end of the parameter.

 

2. Display parameter passing

1 Def Fun (A, B ):
2   Print A, B
3
4 Fun (B = 2 , = 1 )

It should be noted that when one parameter uses the display parameter, other parameters must use the display parameter.

 

3. Variable Parameter List

1 Def Fun (, * B)
2   Print A, B
3
4 Fun ( 1 , 2 , 3 ) # 1 (2, 3)
5 Fun ( 1 , 2 ) # 1 (2 ,)
6 Fun ( 1 ) # 1 () Empty list by default

 

4. Splitting the parameter list

The first knowledge point is that a list can be accepted on the form parameter. When passing the parameter, you can separate the real parameters of the list type and pass them in.

1 ARGs = [ 1 , 100 , 10 ]
2 Range ( * ARGs)

 

5. Lambda form

1 Vfun =   Lambda X, Y: x + Y
2 Vfun ( 5 , 6 ) # Result 11

Lambda can create an anonymous function to implement simple functions. Compared with Ruby block, this function is truly tasteless.

 

6. Document string

1 Def Fun ():
2   """ This is
3
4 Doc """
5   Pass
6
7
8 Print Fun. _ Doc __

 

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.