Python's function of learning notes

Source: Internet
Author: User
Tags ord

Function

Functional programming The most important thing is to enhance the reusability and readability of code

    1. Define and use

def function name (parameter):

...

function body

...

return value

The definition of a function has the following main points:

def: A keyword that represents a function

Function name: the names of functions, which are called later by function name

Function Body: A series of logical computations in a function

Parameters: Providing data for the function body

Return value: When the function is finished, you can return the data to the caller

Example:

1 def Mail (): 2     n = 1233     n + = 14     print(n)5Mail ()6 F = Mail7 F ()

Results:

124

124

1-1 return value

A function is a function block that executes successfully or is required to inform the caller by the return value .

1 def Show (): 2     Print ('a') 3     return [11,22] 4     Print ('b') 5 Show ()

Results:

A

1-2 parameters

There are three different parameters in the function:

    • General parameters
    • Default parameters
    • Dynamic parameters

General parameters

1 def Show (A1,A2): 2     Print (A1,A2) 3 4 Show (11,22)

Results:

11 22

default parameter: The default parameter needs to be placed at the end of the parameter list

1 def Show (a1,a2=99):2     print(a1,a2)34 Show (11)

Results:

11 99

Specifying parameters

1 def Show (A1,A2): 2     Print (A1,A2) 3 4 Show (a2=123,a1=190)

Results:

190 123

Dynamic parameters

1 def Show (*arg):#  *-Receive parameters and convert tuples 2     print(Arg,type (ARG))  34 Show (1,56,78,8979)

Results:

(1,56,78,8979) <class ' tuple ' >

1 def #   * *-Receive parameters and convert     dictionary 2     print(Arg,type (ARG))34 Show (n1=78,uu=123)

Results:

{' UU ': 123, ' N1 ': <class ' dict ' >

1 def Show (*arg1,**args):#  one * in front, two * in the rear 2     print(Arg1,type (ARG1) )3     print(Args,type (args))45 Show (12,34,34,54,n9= 33,n2=99)#  A * in front, two * in the back

Results:

(<class, si, si) ' tuple ' >

{' N2 ': N9 ': <class ' dict ' >

1 defShow (*arg1,**args):#A * In the front, two * in the back2     Print(Arg1,type (arg1))3     Print(Args,type (args))4 5L = (11,22,3,4)6D = {'N1': 89,'K1':'Hello'}7Show (L,D)

Results:

((One, 3, 4), {' K1 ': ' Hello ', ' N1 ':] <class ' tuple ' >

{} <class ' dict ' >

Obviously, the results are not what we want . The point is if we use Show (L,D)

So there's no difference between using show (1,56,78,8979). Therefore, in order to solve this conflict problem , only need to add a *in front of the D , plus two * in advance * can:

1 defShow (*arg1,**args):#A * In the front, two * in the back2     Print(Arg1,type (arg1))3     Print(Args,type (args))4 5L = (11,22,3,4)6D = {'N1': 89,'K1':'Hello'}7Show (*L,**D)

Results:

(One, 3, 4) <class ' tuple ' >

{' N1 ': ' K1 ': ' Hello '} <class ' Dict ' >

To meet our requirements .

Dynamic parameter implementation of string formatting:

1 " {0} is {1}"2 ret = S1.format ('young','  Handsome')3print(ret)

Results:

Young is handsome

1 " {0} is {1}'2 l = ['young','handsome2'  ]3 ret = S1.format (*l)4print(ret)

Results:

Young is handsome2

1S1 ='{Name} is {value}'#Dictionary here is not {0}, {1}, but with {key}, {value}2ret = S1.format (name=' Young', value='Handsome')3 Print('First:', ret)4 5S2 ='{name2} is {key}'6D = {'name2':' Young','Key':'Smart'}7RET = S2.format (* *d)8 Print('Second:', ret)

Results:

First: Young is handsome

Second: Young is smart

2. built-in functions

any: As long as one is true , it is true.

Ord and chr used together

Chr Ord Random.randint can produce verification codes

Enumerate

Eval

Map

Filter

1 li=[1,22,33,45,74,788]2def  func (num):3     if num>33:4         return  True5     Else : 6         return False 7 n = filter (func,li)8Print(list (n))

Results:

[45, 74, 788]

take out the elements in the list that are greater than

Python's function of learning notes

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.