Basic Python and python tutorials

Source: Internet
Author: User

Basic Python and python tutorials
When Python executes a certain type of code, it is equivalent to executing the _ init _ method, for example, list () list _ init __

A set of unordered and non-repeating elements can be considered as a set in mathematics.

Usage: 1. create a set s = set () Create an empty set s = set ([11,22, 33]) s = set ('asdfghh') s = {'asd ', 'saf'} 2. access Collection a in s3. update collection s. add () --- add. Only one element s can be added at a time. clear () ---- clear s. upadte () --- update the Set and assign it to s. You can add multiple iterations, which is equivalent to executing the for loop and then repeating the. add method. S. discard () --- Delete the specified element. If the specified element does not exist, the error s. remove () --- Delete the specified element. If the specified element does not exist, the error s. pop () ----- Delete the element randomly

4. Join (|)

Union operations are equivalent to OR operations on the set. The union symbol has an equivalent method, union ().

>>> s1=set('begin')>>> s2=set('man')>>> s3=s1|s2>>> s3set(['a', 'b', 'e', 'g', 'i', 'm', 'n'])

5. Intersection (&)

It is equivalent to the set AND. The equivalent method of the intersection symbol is intersection ()

>>> s1&s2set(['n'])>>> s1.intersection(s2)set(['n'])

6. Check and supplement (-)
The equivalent method is difference ()

>>> s1-s2  # s1 - (s1 & s2)set(['i', 'b', 'e', 'g'])>>> s1.difference(s2)set(['i', 'b', 'e', 'g'])

7. Symmetric Difference (^)

Symmetric Difference is the XOR ('exclusive or ') of a set. The obtained element belongs to s1, s2, but not both s1 and s2. the equivalent method of Symmetric Difference ()

>>> s1^s2   #(s1 U s2)-(s1  & s2)set(['a', 'b', 'e', 'g', 'i', 'm'])>>> s1.symmetric_difference(s2)set(['a', 'b', 'e', 'g', 'i', 'm'])

Note: and, or between Sets

>>> S1 and s2 are equivalent to s2set (['A', 'M', 'n']) # s2 >>> s1 or s2 is equivalent to s1set (['I', 'B', 'E', 'G', 'n']). # obtain s1 >>>

5. Conversion between collections, lists, tuples, and strings

>>> List (s1) ['I', 'B', 'E', 'G', 'n'] >>> str (s1) "set (['I', 'B', 'E', 'G', 'n'])"> tuple (s1) ('I ', 'B', 'E', 'G', 'n ')


**************************************** * ****** The reference address is missing, sorry *************************************** ***********

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.