Python--Functions, collections

Source: Internet
Author: User
Tags function definition

1. Collection

A collection is also a data type, similar to a list, characterized by disorder, deduplication, where there is no duplicate data in the collection.

The collection is defined by {}, and set () is used to cast to the collection.

1 list = [1,2,2,4,5,3,2]      # definition listing 2 s_list = set (list)          # cast to collection  3print(s_list)45 s_list_2 = {1,2,3,3,4,5}    #  defining the collection directly through {} 6print(s_list_2)

  Collection operations

Set1.intersection (Set2) Intersection: two sets of

Set1.union (SET2) union: Two sets all merges, automatic de-weight

Set1.difference (Set2) Difference set: two sets, one of which has, that is, set 1 has, set 2 No

Set3.issubset (SET1) subset: Set3 is a subset of Set1

Set1.issuperset (SET3) Parent set: Set1 is the parent set of Set3

1Set1 = {1,2,3,4,5,6}2Set2 = {2,3,4,7,8}3Set3 = {5,6}4 5 Print(Set1.intersection (Set2))#intersection6 Print(Set1.union (Set2))#and set7 Print(Set1.difference (Set2))#difference Set, Set1 there, Set2 no8 Print(Set3.issubset (Set1))#subset, whether Set3 is a subset of Set19 Print(Set1.issuperset (SET3))#parent Set, whether Set1 is a parent set of Set3Ten  OneSet1.add (7)#adding elements ASet1.update ([77,66,66])#adding elements -Set1.remove (77)#Delete element, if not present will error -Set1.pop ()#randomly deletes an element and returns the deleted element theSet1.discard (2)#Delete element, if present, delete, not present, do not handle

2. Functions

A function is a set of statements that are encapsulated by a function name and are called directly when used.

function Definition

Define the function with the DEF keyword, followed by the function name, note that the function name cannot be duplicated

1 def Fun (): # defines a function, followed by the name 2     print("Hello world< /c11>"# function Body

  function parameters

The parameter of the function is the part of the physical parameter and the actual parameter, and the shape parametric allocates the memory unit only when it is called, and immediately releases the allocated memory unit at the end of the call.

Therefore, the formal parameter is only valid inside the function. Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument in which the function is called

, they must have a definite value in order to pass these values to the parameter. The parameter variable can no longer be used after the function call finishes returning the keynote function.

1 def Plus (A, b):      #A, a, parameter 2     ,print(A +3 )Plus ( 2, 3)           #2,3 as an argument

  Formal parameters

Divided into positional parameters, default parameters, variable parameters, keywords four kinds of parameters;

    The positional parameter is the parameter location, as in the following example, A, B is the positional parameter, the positional parameter is a must pass parameter

    The default value parameter is given a default value when defining a formal parameter, and if a default value is specified, the default value parameter is the specified value, and if no default value is specified,

Recognition value of the parameter;

    The parameters of the variable parameter are not fixed, the variable parameter is denoted by "*args", the variable parameter must be after the position parameter and the default value parameter, is the parameter that is required to pass

    keyword parameter with "**kwargs", need to use keyword to pass parameter, also is non-required parameters;

1 def regedit (name,sex,age=18,*args,**Kwargs):2     print(name,sex,age=18,* args,**Kwargs)3 regedit ("Bob","male" ,"score","addr", phone=" 137xxxxxx ")

  function return value

function has a return value, if the return value is not specified in the function, the function can have more than one return value and deposit a tuple if none is returned after execution.

function return value use return, function encounters return immediately end

1 def Plus (A, b      ): 2     c = A + b3     return  c,a,b4print(plus (2,3)) #返回一个元组 (5, 2 , 3)

3. Local variables and global variables

Local variables take effect only within the function, the global variables in the entire program, the definition of the Love program is the first global variables, in the function to modify, you need to

Use the "global" keyword to declare that if it is a local variable, you do not need to add "global"

1Name ='Marry'#String Global Variables2names = []#List Global Variables3 Print(name)4 Print(names)5 6 defTest ():7     GlobalName#changing the value of name requires the Global keyword8Name ='Sriba'9Names.append (name)#Modifying the value of a global variable namesTen     returnnames One test () Call function A Print('after modification', name) - Print('names after modification', names)

    

Python--Functions, collections

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.