Python 3rd Day

Source: Internet
Author: User

Function:

    • To define a function:
    • def function (param1, param2 ...):    bodyreturn XXX
    1. function name in lowercase

    2. Immediately exits when the return function is encountered
    3. The default return value is NULL
  • Function parameters:
    • General parameters
    • The default parameter, called function Send_1, can not pass in the B parameter, the B parameter value defaults to ' Gary '
    • # Default Parameters def Send_1 (A, b='Gary'):    print(A, B)     return True
    • Specify parameters that can not be passed by default location
    • # Specifying Parameters def Send (A, b):     Print (A, b)     return Truesend (b='aaa', a='bbb')
    • Dynamic parameters,*argsand**kwargs,reference:http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/
      • The special syntax, *args and **kwargs in function definitions are used to pass a variable number of arguments to a functio N.
      • The single asterisk form (*args) was used to pass a non-keyworded, variable-length argument list.
      • The double asterisk form (**kwargs) is used to pass a keyworded, variable-length argument list.
      •  def  F1 (*args):  print   (args, type (args)) F1 ([ "  Span style= "color: #800000;" >AAA  , "    ]) 

        When passing in  [' AAA ', ' BBB '] , the entire list becomes an element of the progenitor: ([' AAA ', ' BBB '],)

      • def F2 (*args):    print(args, type (args)) F2 (*['aaa' ,'bbb'])

        When passing in *[' AAA ', ' BBB '], loop list each element is Ganso element: (' aaa ', ' BBB '),

      • def f3 (* *Kwargs)    :print(Kwargs, type (Kwargs))

        Dict = {' AAA ': 1, ' BBB ': 2}

        You can use (**kwargs) to pass in a dictionary parameter, and when you call a function in F3 (K=dict), you can build the dictionary automatically:

      • {'k': {'aaa''bbb': 2}}

        When a function is called using F3 (**dict), the return

      • {'bbb' 'aaa': 1}
    • Function arguments are passed by reference
  • function variables:
    • Global variables, out-of-function definitions, and any scope is readable.
    • Local variables, which are used internally by the function.
    • Local variables have higher precedence, and the program reads first.
    • The function internal plus global can modify global variables, that is, re-assign values.
    • If the global variable is a modifiable type (sting, List, dict), the function can also be modified without global, but the re-assignment must be added to the global, re-assignment is to create a new value in memory
    • Global variables must be uppercase

string format printing:

String.Format ():

#STR formatList_1 = ['Gary', 31]s1="im {0}, age {1}". Format (*list_1)Print(s1) S2="im {name}, age {age}". Format (name='Gary', age=31)Print(S2) dict= {'name':'Gary',' Age': 31}S3="im {name}, age {age}". Format (* *dict)Print(S3)

Lambda: Constructing simple functions

Colon connection parameters and return values

Lambda a1:a1 + 100

Equivalent to

def F1 (A1):     return A1 + 100

Three-mesh operation:

For a simple logical expression

" Gary " if Else " NB "

Equivalent to

if 1 = =1    :"Gary"else    :"  NB"

File:

Open function

    • Open opening file handle
    • R: Read Only, W: Write Only (empty the file first), X (python3): File exists error, does not exist create and assign W permission, prevent overwrite existence file, a: Append
    • r+: Read and Write, w+: Read and write, but each time the file is emptied, A +: No matter where the pointer is appended from the last
    • with open () as F: Complete the operation to automatically close the file
    • Mode has "B" to read or write in bytes, otherwise read and write in characters

Python 3rd Day

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.