A collection of Python basics, function--06

Source: Internet
Author: User
Tags define function

Collection

Collections in Python

consists of different elements (deduplication, no repeating elements)

s = set ('hello')print(s)# The result is:s = {'  H'o'e'  l'}

The elements in the collection are unordered

Each element must be an immutable type (for example: numbers, strings, tuples)

Adding a collection element

S.add (3)

Emptying the collection

S.clear ()

To delete a collection element

S.pop ()# randomly delete s.remove (3)# Specify Delete, no element is present when delete will error S.discard (3)# Specify Delete, No error when the element is not present

Relational operations for collections
L1 = [1,2,3,5,'winsdom','xl'= [1,4,5,'  winsdom', 7]
S1 = set (L1)
S2 = Set (L2)

Intersection

s1.intersection (S2) S1&s2
#结果是:
{1, ' Winsdom ', 5}

and set (will be two sets together and then go heavy, get the new collection)

s1.union (s2) S1| S2 Print (s1| S2) # The result is: ' XL '}

Difference level

s1.difference (S2) S1-s2print(s1-s2)# The result is:  'xl'}

Cross complement set

s1.symmetric_difference (S2) S1^S2print(s1^s2)# The result is:'  xl', 7}

Asks if there is no intersection between S1 and S1, and returns True if there is no intersection.

Print (S1.isdisjoint (S2)) # The result is False

Determines whether a subset, parent set

S1.issubset (S2)# determines if the subset is FalseS1.issuperset (S2)# Determines if the parent set is False

Update

S1.update (S2)# update S1 update    multiple groups   can pass the value of an iteration S1.add ()# does not update

Function
def Test (x):     ' The function defintions '     x+=1    return x def : Define function keyword Test: function name (): Within definable parameters "' : Document description x+=1: code block return: Define return value

arguments, parameters, and arguments of a function
1-Parametric the memory unit is allocated only when it is called, and the allocated memory unit is freed immediately at the end of the call. Therefore, the formal parameter is only valid internally. The parameter variable can no longer be used after the function call finishes returning the keynote function.

2 arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when making a function call, they must have a definite value in order to pass these values to the parameter. Therefore, the parameters should be pre-assigned, input and so on to obtain a definite value.

3 Positional parameters and keywords (standard call: actual participation parameter position one by one corresponds)

4 default parameter 5 parameter group

A collection of Python basics, function--06

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.