Learn the path to PYTHON, Day 3-python basic 3 (function)

Source: Internet
Author: User
Tags new set set set shallow copy

A set set (unordered andcollection of elements that are not duplicated)
Basic Operation: T.add (‘X‘)#Add an itemS.update ([10,37,42])#Adding multiple items in SDelete an item:
T.discard (' H ') t.remove (‘H‘) Difference: If delete is not in the collection, discard will not error, remove error
T.pop ()# no arguments Randomly deletes and returns the deleted value
Len (s) set length Xin s test if X is a member of S x not in s test if X is not a member of S S.issubset (t) s <= T test if every element in S is in T S.issuperset (t) s >= T tests if every element in T is in S s.union (t) s | T returns a new set containing each element in S and T S.intersection (t) s & T returns a new set package Common elements with S and T S.difference (t) s- T returns a new set containing elements in s that have but not in T s.symmetric_difference (t) s ^ T returns a new set containing a non-repeating element in S and T S.copy () returns a shallow copy of the set "s"
span>
two or three-dollar operation (if else shorthand)
1 if Else 23# when a "10,result = 30, otherwise a =50
Three lambda expressions (two arguments can be followed, only one line)
1 Lambda A, b:a + B + 100

Four function I return value

If return does not specify a value, it is none.

1 def F1 (): 2     Print (123) 3     return ' 111 ' 4     # in the function, once the return is executed, the function execution process terminates immediately, and the following print will no longer execute 5     Print (456)
II parameters

1 General parameters (in strict order, the actual parameters are assigned to the formal parameters)

2 default parameters (must be placed at the end of the parameter list)

3 Specifying parameters (assigning actual parameters to the established formal parameters)

4 Dynamic Parameters:

(1) * Default will pass in the parameters, all placed in Yongzu

1 def F1 (*args):2     print(args)34'  Lala']5F1 (LI)6 F1 (*li)

The first result is ([One, ' Lala ']), which treats the list as an element

The second result is (one, ' Lala '), putting all the elements in the list into the meta Zanzuri

(2) * * The parameters passed in by default are all placed in the dictionary

1 def F1 (**kwargs):2     print(Kwargs)3'Nikita  ')

The result is {' N1 ': ' Nikita '}

50,000 Energy parameters

Def f1 (*args, **kwargs)

III Other

1 Local variables and global variables

Only local variables are used in the function, all scopes are available as global variables (note capitalization), and global is used in the function, and local variables can be changed.

' Lulu '  
def F1 (): = Ten # Change the global variable of name to Nikita, the name of the following function is Nikita Global name ' Nikita ' Print (age, NAME)

If the global variable is a list, the dictionary can only be modified and cannot be re-assigned. That is, you cannot use global.







Learn the path to PYTHON, Day 3-python basic 3 (function)

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.