Python Programming
  • Course Index
  • Python Data types
    • Numbers
    • String
    • List
    • Tuple
    • Dictionary
    • Sets
  • Type Conversion
  • Operators
  • Conditional Statements & Loops
    • Conditional Statements
    • Loops In Python (for, while loop)
    • Practice Problems - Loops
  • Functions in Python
    • Functions - 1
    • Examples of Function - 1
    • Function - 2
    • Examples of Function - 2
    • Local/Global Variables
    • Date Time Functions
  • File Handling In Python
  • Error Handling
  • OOPs
    • Classes & Objects
    • Constructors, Attributes & Objects
    • Polymorphism
    • Constructors
    • Built-In Class Attributes
    • Inheritance, Method Overriding
    • Inheritance Examples
    • Encapsulation
    • Overriding Methods & Overloading Operators
    • Declaring Enumerations
    • MultiThreading
    • Destructors
    • Exception Handling
  • CRUD Operation in MySQL using Python
  • Working with CSV & Excel Files
  • Advanced Data Types
    • Deque in Python
    • Python orderedDict
    • Python namedTuples
    • Python FrozenSet
    • Difference btwn List, Tuple, Dict, Set, FrozenSet
  • Python Optimizations (Comprehensions)
  • Threading
  • Web Scrapping
    • News Web Scraping
    • IMDB Data Scraping
  • API request
    • Google Maps API request
  • Email Automation
  • Outlook Integration
  • Twitter Sentiment Analysis
  • NumPy
  • Pandas
  • Python Practice Papers
Powered by GitBook
On this page

Was this helpful?

  1. Python Data types

Sets

A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements.

Example - 1:

s1 = { 1,1,1,1,1,1,1,1,1,2,3,4,5}
s2 = { 4,5,6,7,8}
print("Set1 = ",s1)
print("Set2 = ",s2)

Set1 = {1, 2, 3, 4, 5}

Set2 = {4, 5, 6, 7, 8}

Methods for sets

Example - 2:

#union -> Returns a union of two set.Using the ‘|’ operator between 2 sets is the same as writing set1.union(set2)
#intersection -> Returns an intersection of two sets.The ‘&’ operator comes can also be used in this case.
#Difference -> Returns a set containing all the elements of invoking set but not of the second set. We can use ‘-‘ operator here.
print("Union of s1 and s2  = ",s1.union(s2))
print("Intersection of s1 and s2 = ",s1.intersection(s2))
print("Difference of s1 with s2 = ",s1.difference(s2))

Union of s1 and s2 = {1, 2, 3, 4, 5, 6, 7, 8}

Intersection of s1 and s2 = {4, 5}

Difference of s1 with s2 = {1, 2, 3}

Example - 3:

#add -> Adds the item x to set if it is not already present in the set. 
people = {"Jay", "Idrish", "Archil"}
people.add("Daxit") 
print(people)

{'Archil', 'Jay', 'Idrish', 'Daxit'}

PreviousDictionaryNextType Conversion

Last updated 5 years ago

Was this helpful?