Python built-in functions and built-in methods learning tutorial

Source: Internet
Author: User
This article describes how to integrate built-in functions and built-in methods in Python, including factory functions and methods for Variable Sets. For more information, see Set built-in functions and built-in methods
(1) Standard functions
Len (): Pass the set as a parameter to the built-in function len () and return the base number (or number of elements) of the set ).
(2) set type factory functions
The set () and frozenset () factory functions are used to generate mutable and immutable sets, respectively. If no parameter is provided, an empty set is generated by default. If a parameter is provided, the parameter must be iterated, that is, a sequence or Iterator or an object supporting iteration, such as a file or a dictionary.
(3) method (all set methods)
S. issubset (t) if s is a subset of t, True is returned; otherwise, False is returned.
S. issuperset (t) if t is the superset of s, True is returned; otherwise, False is returned.
S. union (t) returns a new set, which is the union of s and t.
S. intersection (t) returns a new set, which is the intersection of s and t.
S. difference (t) returns a new set, which is a member of s but not a member of t.
S. symmetric_difference (t) returns a new set, which is a member of s or t, but not a member of s and t.
S. copy () returns a new set, which is a shortest copy of set s.
The built-in method copy () does not have an equivalent operator. Like the dictionary method with the same name, the copy () method is faster than copying a copy of an object using a factory method such as set (), frozenset (), or dict.
(4) method (only applicable to variable sets)

Methods for variable set types:

Demo instance:
I. 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. method of variable set type

1. s. update (t) -- use the element in t to modify s, that is, s now contains s or t members.

>>> s.update(t)>>> sset(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])

2. members in s. intersection_update (t) -- s belong to elements in s and t.

>>> s = set('cheeseshop')>>> t = set('bookshop')>>> s.intersection_update(t)>>> sset(['h', 's', 'o', 'p'])


3. members in s. difference_update (t) -- s belong to s but are not included in t.

>>> s = set('cheeseshop')>>> t = set('bookshop')>>> s.difference_update(t)>>> sset(['c', 'e'])


4. members in s. symmetric_difference_update (t) -- s are updated to elements that are included in s or t but not shared by s and t.

>>> s = set('cheeseshop')>>> t = set('bookshop')>>> s.symmetric_difference_update(t)>>> sset(['c', 'b', 'e', 'k'])


5. s. add (obj) -- add Object obj to set s.

>>> s.add('o')>>> sset(['c', 'b', 'e', 'k', 'o'])


6. s. remove (obj) -- delete Object obj from set s. if obj is not an element in set s (obj not in s), a KeyError is thrown.

>>> s.remove('b')>>> sset(['c', 'e', 'k', 'o'])>>> s.remove('a')

Traceback (most recent call last): File " ", line 1, in s.remove('a')KeyError: 'a'


7. s. discard (obj) -- If obj is an element in set s, delete the Object obj from set s.

>>> s.discard('a')>>> sset(['c', 'e', 'k', 'o'])>>> s.discard('e')>>> sset(['c', 'k', 'o'])

8. s. pop () -- delete any object in the set and return it.

>>> s.pop()'c'>>> sset(['k', 'o'])

9. s. clear () -- delete all elements in set s.

>>> s.clear()>>> sset([])

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.