A collection of Python basics

Source: Internet
Author: User

Python basic Three:
First, sort data types:
Mutable and immutable:
1. Variable: list, dictionary
2, immutable: strings, tuples, numbers
Access Order:
1. Direct Access: Digital
2. Sequential access: String, list, tuple
3. Mapping: Dictionary
Number of elements to store:
1. Container type: list, tuple, dictionary
2. Atomic type: Number, string

Two, set
Characteristics:
1. Composition of different elements
2, is disorderly
3. The elements in the collection must be immutable types (numbers, strings, tuples)
4. How to define the collection: Test = {' Xyy ', ' Xyyp ', 1,2,3,4} or test = set (' XYY ')

Three, set function:
Add () Adds an element to the collection and can only add one element at a time

Test = {' Xyy ', ' Xyyp ', 1,2,3,4}test.add (5) test.add (' Xyp ') print (test)

Results:

Clear () empties the contents of the collection

Test = {' Xyy ', ' Xyyp ', 1,2,3,4}test.clear () print (test)

Results:

Copy () copies a new set of contents

Test = {' Xyy ', ' xyyp ', 1,2,3,4}n = Test.copy () print (n)

Results:

Pop () deletes a random element

Test = {' Xyy ', ' Xyyp ', 1,2,3,4}test.pop () print (test)

Results:

Remove () deletes the specified content and will error if the specified content does not exist

Test = {' Xyy ', ' Xyyp ', 1,2,3,4}test.remove (' Xyyp ') print (test)

Results:

Discard () deletes the specified content and does not error if the specified content does not exist

Test = {' Xyy ', ' Xyyp ', 1,2,3,4}test.discard (' XYY ') test.discard (' XYYFG ') print (test)

Results:

Update () adds multiple elements to the collection, which is a bit more powerful than add

Test = {' Xyy ', ' xyyp ', ' Xyp '}test.update (' Jony ', ' Tom ')//Add a tuple then the result is the complete string test.update (' Jony ', ' Tom ')// If you add a string, the result is a single character print (test)

Results:

Isdisjoint () determines that two sets have the same element as wood, and none is true (true)

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyyx ', ' xypc '}n = Test1.isdisjoint (test2)//Test1 and test2 do not have the same element print (n)

Results:

Iv. Python Set-relational operations:
1, Intersection: intersection () will output two sets of the same content

Test1 = {' Xyy ', ' xyyp ', ' xyp '}//define two sets, two sets have the same and different elements test2 = {' Xyy ', ' xyp ', ' fg '}n = test1.intersection (test2) print (n)

Results:

Or:

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp ', ' fg '}n = test1&test2//intersection can use & symbol instead of print (n)

Results:

2 Unions: Union () outputs different contents of two sets and synthesizes a collection

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp ', ' fg '}n = test1.union (test2) print (n)

Results:

Or:

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp ', ' fg '}n = test1|test2//a vertical bar (|) means union this function print (n)

3, Difference set: Difference () The first set minus the second set to the first set is different from the second set of an element, which in turn means

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp ', ' fg '}N1 = test1.difference (test2)//test1 collection minus test2 set n2 = Test2.difference (test1)//test2 collection minus Test1 collection print (N1,N2)

Results:

Or:

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp ', ' fg '}N1 = test1-test2//with a minus sign (-) instead of Differenceprint (n1) N2 = Test2-test1print (N2)

Results:

4, cross-complement set: Symmetric_difference () Only take two sets of different parts

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp ', ' fg '}N2 = test1.symmetric_difference (test2) print (N2)

Results:

Or:

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp ', ' Tom '}n = Test1^test2//symmetric_difference () can be used instead of print (n)

Results:

5. Subset: Issubset () determine who is a subset of WHO

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp '}n = Test2.issubset (test1)//two sets have the same part, but the second one is less than the first element, So the second one is a subset of the first print (n)

Results:

6. Parent set: Issuperset () judge who is the parent of the set

Test1 = {' Xyy ', ' xyyp ', ' xyp '}test2 = {' Xyy ', ' xyp '}n = Test1.issuperset (test2)//two sets have the same part, but the first one is more than the second element, So the first one is the second parent set print (n)

Results:

A collection of Python basics

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.