sets ppt_00001

Published on Slideshow
Static slideshow
Download PDF version
Download PDF version
Embed video
Share video
Ask about this video

Scene 1 (0s)

sp-aSD 9610113006 Rppau eRAeaqs- j NOHlkd NI SIAS.

Scene 2 (6s)

CONTENTS. Introduction Creating a set Adding items to the set Removing items to the set Set operations Other set methods.

Scene 3 (15s)

INTRODUCTION. A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.

Scene 4 (28s)

CREATING A SET. The set can be created by enclosing the comma-separated immutable Atems with the curly braces {}. Python also provides the set() method, which can be used to create the set by the passed sequence..

Scene 5 (40s)

day", "Sunday'} print(Days) print(type(Days)) #output: <class 'set'> CREATING : Example 1: Using curly braces Days = <class 'set'> 'Saturday', 'Thursday', 'Thursday', 'Sunday', "S 'Sunday',.

Scene 6 (59s)

ADDING ITEMS 10 SET. Python provides the add() method and update() method which can be used to add some particular item to the set. The add() method is used to add a single element whereas the update() method is used to add multiple elements to the set. Consider the following example. 6.

Scene 7 (1m 14s)

PIE. add0: prime numbers - 7} # add 11 to prime numbers prime_numbers.add(11 ) print(prime_numbers) # Output: update0: result = A.update(B) print('A =', A) print('result =', result) #output:A = result = None.

Scene 8 (1m 29s)

REMOVING ELEMENTS FROM SET. Python provides the discard() method and remove() method which can be used to remove the items from the set. The difference between these function, using discard() function if the item does not exist in the set then the set remain unchanged whereas remove() method will through an error..

Scene 9 (1m 45s)

PIE. discard0: numbers = numbers.discard(3) print('numbers = ' numbers) numbers.discard(10) print('numbers = ' numbers) #output: numbers = numbers = remove0: languages = # remove English from the set languages.remove('English') print(languages) # Output:.

Scene 10 (2m 0s)

SET OPERATIONS: Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetric difference. We can do this with operators or methods. UNION. Union of A and B is a set of all elements from both sets. Union is performed using I operator. Same can be accomplished using the union() method. 10.

Scene 11 (2m 17s)

PIE. Using I operator: # Set union method # initialize A and B # use I operator print(A I B) #0utput: union0: # compute union between A and B print('A U B = ' A. union(B)) # Output: AU B = II.

Scene 12 (2m 29s)

SET INTERSECTION. Intersection of A and B is a set of elements that are common in both the sets. Intersection is performed using & operator. Same can be accomplished using the intersection() method.

Scene 13 (2m 40s)

PIE. Using & operator: # Intersection of sets # initialize A and B # use & operator print(A & B) #0utput: Using intersection0: # compute intersection between A and B print(A. intersection (B)) # Output: 13.

Scene 14 (2m 51s)

PYTHON SET SYMMETRIC _DlffERENCE(). The Python symmetric difference() method returns the symmetric difference of two sets. The symmetric difference of two sets A and B is the set of elements that are in either A or B, but not in their intersection. EXAMPLE: print(A.symmetric_difference(B)) print(B.symmetric_difference(A)) print(A.symmetric_difference(C)) print(B.symmetric_difference(C)) #0utput:.

Scene 15 (3m 7s)

OTHER METHODS(). Method difference() difference_update() discard() intersection() isdisjoint() issubsetO issuperset() remove() symmetric_difference() symmetric_difference_update() union() Python Sets Methods Description Adds an element to the set Removes all the elements from the set Returns a copy of the set Returns a set containing the difference between two or more sets Removes the items in this set that are also included in another, specified set Remove the item Returns a set, that is the intersection of two other sets Removes the items in this set that are not present in other, specified set(s) Retums whether two sets have intersection or not Returns whether another set contains this set or not Returns whether this set contains another set or not Removes an element from the set Removes the specified element Retums a set with the symmetric differences of two sets inserts the symmetric differences from this set and another Return a set containing the union of sets Update the set with the union of this set and others .4.