Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
docu...
Typing Speed
Week

Target

Achieved

1

25

23

2

30

26

3

30

28

4

35

32

5

40

38
Jobs Applied
#

Company

Designation

Applied Date

1
2
3

Aug 24

Current
Status
Sets in python
shafeeque
●

shafeequemonp@gmail.com

●

www.facebook.com/shafeequemonppambodan

●

twitter.com/shafeequemo...
Sets:
●

●

●

A set is an unordered collection of objects, unlike sequence objects such as lists and
tuples
Sets cannot h...
Constructing Sets:
One way to construct sets is by passing any sequential object to the "set"
constructor

●

>>> set([0, ...
●

Update set:
>>> s.update([26, 12, 9, 14])
>>> s
set([32, 9, 12, 14, 54, 26])

●

Copy set:
●

The set function also pro...
Membership Testing:
●

We can check if an object is in the set using the same "in" operator as with
sequential data types
...
●

Note that the <= and >= operators also express the issubset and issuperset functions
respectively.
>>> set([4, 5, 7]) <...
●

We also have the "remove" function to remove a specified element.
>>> s.remove(3)
>>> s
set([2,4,5,6])
●

However, remo...
Iteration Over Sets:
●

We can also have a loop move over each of the items in a set.

●

However, since sets are unordere...
Set Operations:
●

Python allows us to perform all the standard mathematical set operations,
using members of set
●

Union...
●

Intersection
●

Any element which is in both s1 and s2 will appear in their intersection
>>> s1 = set([4, 6, 9])
>>> s2...
Set Difference

●

●

Python can also find the set difference of s1 and s2 , which is the elements that
are in s1 but not ...
Frozenset:
●

●

●

A frozenset is basically the same as a set, except that it is immutable once it is created, its member...
If this presentation helped you, please visit our page facebook.com/baabtra and
like it.

Thanks in advance.
www.baabtra.c...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

Start up Vil...
Sets in python
Upcoming SlideShare
Loading in …5
×

Sets in python

289 views

Published on

Sets in python

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
289
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • &lt;number&gt;
  • &lt;number&gt;
  • &lt;number&gt;
  • &lt;number&gt;
  • &lt;number&gt;
  • Sets in python

    1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
    2. 2. Typing Speed Week Target Achieved 1 25 23 2 30 26 3 30 28 4 35 32 5 40 38
    3. 3. Jobs Applied # Company Designation Applied Date 1 2 3 Aug 24 Current Status
    4. 4. Sets in python shafeeque ● shafeequemonp@gmail.com ● www.facebook.com/shafeequemonppambodan ● twitter.com/shafeequemonp ● in.linkedin.com/in/shafeequemonp ● 9809611325
    5. 5. Sets: ● ● ● A set is an unordered collection of objects, unlike sequence objects such as lists and tuples Sets cannot have duplicate members - a given object appears in a set 0 or 1 times All members of a set have to be hashable, just like dictionary keys ● Integers, floating point numbers, tuples, and strings are hashable process ● dictionaries, lists, and other sets (except frozensets) are not. ● Eg: set(['b', 'e', 'o', 's', 'u', 't']) set([32, 26, 12, 54])
    6. 6. Constructing Sets: One way to construct sets is by passing any sequential object to the "set" constructor ● >>> set([0, 1, 2, 3]) set([0, 1, 2, 3]) >>> set("obtuse") set(['b', 'e', 'o', 's', 'u', 't']) ● Add elements to sets >>> s = set([12, 26, 54]) >>> s.add(32) >>> s set([32, 26, 12, 54])
    7. 7. ● Update set: >>> s.update([26, 12, 9, 14]) >>> s set([32, 9, 12, 14, 54, 26]) ● Copy set: ● The set function also provides a copy constructor. However, remember that the copy constructor will copy the set, but not the individual elements >>> s2 = s.copy() >>> s2 set([32, 9, 12, 14, 54, 26])
    8. 8. Membership Testing: ● We can check if an object is in the set using the same "in" operator as with sequential data types >>> 32 in s True >>> 6 in s False >>> 6 not in s True ● We can also test the membership of entire sets. Given two sets s1 and s2 , we check if s1 is a subset or a superset of s2 >>> s.issubset(set([32, 8, 9, 12, 14, -4, 54, 26, 19])) True >>> s.issuperset(set([9, 12])) True
    9. 9. ● Note that the <= and >= operators also express the issubset and issuperset functions respectively. >>> set([4, 5, 7]) <= set([4, 5, 7, 9]) True >>> set([9, 12, 15]) >= set([9, 12]) True Removing Items: ● ● There are three functions which remove individual items from a set, called pop, remove, and discard The first, pop, simply removes an item from the set >>> s = set([1,2,3,4,5,6]) >>> s.pop() 1 >>> s set([2,3,4,5,6])
    10. 10. ● We also have the "remove" function to remove a specified element. >>> s.remove(3) >>> s set([2,4,5,6]) ● However, removing a item which isn't in the set causes an error. >>> s.remove(9) Traceback (most recent call last): File "<stdin>", line 1, in ? KeyError: 9 ● We also have another operation for removing elements from a set, clear, which simply removes all elements from the set >>> s.clear() >>> s set([])
    11. 11. Iteration Over Sets: ● We can also have a loop move over each of the items in a set. ● However, since sets are unordered, it is undefined which order the iteration will follow. >>> s = set("blerg") >>> for n in s: ... print n, ... rbelg
    12. 12. Set Operations: ● Python allows us to perform all the standard mathematical set operations, using members of set ● Union The union is the merger of two sets. Any element in s1 or s2 will appear in their union >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.union(s2) set([1, 4, 6, 8, 9]) >>> s1 | s2 set([1, 4, 6, 8, 9])
    13. 13. ● Intersection ● Any element which is in both s1 and s2 will appear in their intersection >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.intersection(s2) set([6]) >>> s1 & s2 set([6]) >>> s1.intersection_update(s2) >>> s1 set([6]) ● Symmetric Difference ● The symmetric difference of two sets is the set of elements which are in one of either set, but not in both >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.symmetric_difference(s2) set([8, 1, 4, 9]) >>> s1 ^ s2 set([8, 1, 4, 9]) >>> s1.symmetric_difference_update(s2) >>> s1 set([8, 1, 4, 9])
    14. 14. Set Difference ● ● Python can also find the set difference of s1 and s2 , which is the elements that are in s1 but not in s2. >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.difference(s2) set([9, 4]) >>> s1 - s2 set([9, 4]) >>> s1.difference_update(s2) >>> s1 set([9, 4]) Multiple sets: ● Starting with Python 2.6, "union", "intersection", and "difference" can work with multiple input by using the set constructor. For example, >>> s1 = set([3, 6, 7, 9]) >>> s2 = set([6, 7, 9, 10]) >>> s3 = set([7, 9, 10, 11]) >>> set.intersection(s1, s2, s3) set([9, 7])
    15. 15. Frozenset: ● ● ● A frozenset is basically the same as a set, except that it is immutable once it is created, its members cannot be changed Since they are immutable, they are also hashable, which means that frozensets can be used as members in other sets and as dictionary keys frozensets have the same functions as normal sets, except none of the functions that change the contents (update, remove, pop, etc.) are available >>> fs = frozenset([2, 3, 4]) >>> s1 = set([fs, 4, 5, 6]) >>> s1 set([4, frozenset([2, 3, 4]), 6, 5]) >>> fs.intersection(s1) frozenset([4]) >>> fs.add(6) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'frozenset' object has no attribute 'add'
    16. 16. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
    17. 17. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550

    ×