# Built-in functions and built-in methods for collections in Python learning tutorial

Source: Internet
Author: User
Set built-in functions and built-in methods
(1) Standard type function
Len (): Passes the set as an argument to the built-in function Len (), which returns the cardinality (or number of elements) of the collection.
(2) collection type factory function
The set () and Frozenset () factory functions are used to generate mutable and immutable collections, respectively. If no arguments are supplied, an empty collection is generated by default. If you supply a parameter, the argument must be iterative, that is, a sequence or iterator, or an object that supports iterations, such as a file or a dictionary.
(3) method (all collection methods)
S.issubset (t) returns True if S is a subset of T, otherwise false
S.issuperset (t) if T is a superset of S, returns true, otherwise false
S.union (t) returns a new collection that is the combination of S and T
S.intersection (t) returns a new collection that is the intersection of S and T
S.difference (t) returns a new collection that is a member of S, but not a member of T
S.symmetric_difference (t) returns a new collection that is a member of S or T, but not a member of S and T altogether
S.copy () returns a new collection, which is a shallow copy of the collection s
The built-in method copy () does not have an equivalent operator. As with the dictionary method of the same name, the copy () method is faster than copying a copy of an object with a factory method such as set (), Frozenset (), or dict ().
(4) method (for variable sets only)

Methods for variable collection types:

Demo Example:
First, the collection type method

`>>> s = set (' Cheeseshop ') >>> t = set (' Bookshop ') >>> sset ([' C ', ' e ', ' h ', ' o ', ' P ', ' s ']) >> ;> Tset ([' B ', ' H ', ' k ', ' o ', ' P ', ' s ']) >>> s.issubset (t) false>>> S.issuperset (t) false>>> S.union (t) set ([' C ', ' B ', ' e ', ' h ', ' k ', ' o ', ' P ', ' s ']) >>> s.intersection (t) set ([' H ', ' s ', ' o ', ' P ']) >>> ; S.difference (t) set ([' C ', ' e ']) >>> s.symmetric_difference (t) set ([' B ', ' e ', ' k ', ' C ']) >>> s.copy () Set ([' P ', ' C ', ' e ', ' h ', ' s ', ' o '])`

Ii. methods of variable set types

1, s.update (t)--modify s with the elements in T, i.e. s now contains members of S or T.

`>>> s.update (t) >>> sset ([' C ', ' B ', ' e ', ' h ', ' k ', ' o ', ' P ', ' s '])`

2. The members in S.intersection_update (t)--s are elements that belong together in S and T.

`>>> s = set (' Cheeseshop ') >>> t = set (' Bookshop ') >>> s.intersection_update (t) >>> Sset ([' H ', ' s ', ' o ', ' P '])`

3. The members in S.difference_update (t)--s are elements that belong to s but are not contained in T.

`>>> s = set (' Cheeseshop ') >>> t = set (' Bookshop ') >>> s.difference_update (t) >>> Sset ([' C ', ' e '])`

4. The members in S.symmetric_difference_update (t)--s are updated to those elements that are contained in s or T, but are not common to s and T.

`>>> s = set (' Cheeseshop ') >>> t = set (' Bookshop ') >>> s.symmetric_difference_update (t) > >> Sset ([' C ', ' B ', ' e ', ' K '])`

`>>> s.add (' o ') >>> sset ([' C ', ' B ', ' e ', ' k ', ' O '])`

6, S.remove (obj)-Removes an object from the set S obj, if obj is not an element in the set S (obj not in s), the Keyerror is raised.

```>>> s.remove (' B ') >>> sset ([' C ', ' e ', ' k ', ' O ']) >>> s.remove (' a ')Traceback (most recent): File "

", line 1, in

s.remove (' a ') Keyerror: ' A '

c7/>
```

7, S.discard (obj)--If obj is an element in the set S, the object obj is removed from the collection S.

`>>> S.discard (' a ') >>> sset ([' C ', ' e ', ' k ', ' O ']) >>> s.discard (' e ') >>> sset ([' C ', ' K ', ' O '])`

8, S.pop ()--Deletes any object in the collection, and returns it.

`>>> S.pop () ' C ' >>> sset ([' K ', ' O '])`

9, S.clear ()--Deletes all the elements in the set S.

`>>> s.clear () >>> sset ([])`
• Related Keywords:
Related Article

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.