Functions of Python (ii)

Source: Internet
Author: User

First, the function related concepts

1. Global variables: Outside the function, the variable defined above is the global variable

2. Local variables: defined in the function, can not be used after the function, if you want to modify the value of the global variable in the function, you need to add the keyword global before the function, but the dictionary and list of the variable variable, do not need to use

Global has declared that it can be changed directly.

3. A few examples of parameters:

A. Variable parameter, parameter group-tuple form:

def send_mail (*args):

Print (args)

Send_mail (' [email protected] ', ' [email protected] ', ' [email protected] ')

B. Variable parameters, parameter groups-dictionary form:

def send_mail (**kwargs):

Print (Kwargs)

Send_mail (k1= ' v1 ', k2= ' v2 ')

Ii. recursion of functions

1. Function calls itself, up to 999 cycles

2. In the case of recursion, there must be a definite end condition

3. Example:

Def my2 ():
num = input (' Enter a number: ')
num = int (num)
If num%2!=0:
Print (' Please enter an even number ')
Return My2 ()
My2 ()

Iii. list derivation, List-generation

1. Example:

Import Random
res = [26, 7, 18, 27, 32, 28]
For I in range (len (res)):
Res[i] = str (res[i]). Zfill (2)
#列表推导式, List-generated
Res1 = [Str (i). Zfill (2) for I in res]--equivalent to the for loop above
Print (RES1)
Res2 = [i+10 for i in Res]
Print (Res2)
HH = [I for I in Range (0,1001,2)]
Print (HH)

Iv. built-in functions

1.max (range (1,28))--MAX, run result: 27

2.min (Range (28))-Minimum, run result: 0

3.sum (range (1,101))--summing, running result: 5050

4.res = sorted ([2,3,1,2,3],reverse=true)--Sort, plus reverse=true is in descending order

5.res = eval ('--eval '), execute Python code, execute simple Python code only

6.F = open (' Code ', encoding= ' utf-8 ') code = F.read () exec (code)---exec, Python code executable in the file

7.sql = ' INSERT into My_user value ({id},{name},{addr},{sex},{phone}) '

Sql.format (name= ' aaa ', addr= ' sdfsfd ', sex= ' xxx ', id=11)--format function is a function of formatting a string

8. For index,s in Enumerate (stus): The print (index,s)--enumerate () function is used to combine a traversed data object (such as a list, tuple, or string) into an index sequence, listing both data and data subscripts, typically used in For Loop, the enumerate (enum) object is returned, and if the subscript start position is written as 1, the enumeration object is returned from ordinal 1.

9.for Name,se,ag in Zip (stus,sex,age):p rint (NAME,SE,AG)-Multiple lists compressed together

  

Functions of Python (ii)

Related Article

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.