StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Sets
Download
  1. Python
  2. Pyhton MCA (Machine Learning using Python)
  3. Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP
Tuple : Dictionaries
Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP

A set in Python is an unordered, mutable collection of unique items. Its primary characteristics are that it automatically removes duplicate elements and is highly optimized for mathematical set operations and membership testing. Sets are created by placing items inside curly braces {} or by using the set() constructor.

Key Characteristics of Sets

  • Unordered: The items in a set do not have a defined order. When you iterate over a set, the items may appear in a different order each time.

  • Mutable: You can add and remove items from a set after it has been created.

  • Unique Elements: Sets automatically enforce uniqueness. If you try to add an item that is already in the set, the set will not change.

  • Items Must be Immutable: The items within a set must be of an immutable data type (like numbers, strings, or tuples). You cannot have a list or another set as an item within a set.

Python

# A set of fruits - the duplicate "apple" is automatically removed

fruits_set = {"apple", "banana", "cherry", "apple"}

 

# Creating a set from a list to remove duplicates

numbers_list = [1, 2, 2, 3, 4, 3]

unique_numbers = set(numbers_list)

 

print(f"A simple set: {fruits_set}")

print(f"Set from list (duplicates removed): {unique_numbers}")


Operations with Operators

Sets are powerful because they support mathematical set operations directly with operators.

Union (|)

Returns a new set containing all the unique items from both sets.

Python

set_a = {1, 2, 3, 4}

set_b = {3, 4, 5, 6}

union_set = set_a | set_b

print(f"Union (|): {union_set}")

# Output: {1, 2, 3, 4, 5, 6}

Intersection (&)

Returns a new set containing only the items that are present in both sets.

Python

set_a = {1, 2, 3, 4}

set_b = {3, 4, 5, 6}

intersection_set = set_a & set_b

print(f"Intersection (&): {intersection_set}")

# Output: {3, 4}

Difference (-)

Returns a new set containing items that are in the first set but not in the second set.

Python

set_a = {1, 2, 3, 4}

set_b = {3, 4, 5, 6}

difference_set = set_a - set_b

print(f"Difference (-): {difference_set}")

# Output: {1, 2}

Symmetric Difference (^)

Returns a new set containing items that are in either of the sets, but not in both.

Python

set_a = {1, 2, 3, 4}

set_b = {3, 4, 5, 6}

symmetric_diff_set = set_a ^ set_b

print(f"Symmetric Difference (^): {symmetric_diff_set}")

# Output: {1, 2, 5, 6}


Built-in Functions for Sets

len()

Returns the number of items in a set.

Python

numbers = {10, 20, 30, 40}

print(f"Length of the set: {len(numbers)}") # Output: 4

sum(), min(), max()

Returns the sum, minimum, or maximum item in a set. These only work if all items are numbers.

Python

numbers = {5, 2, 8, 1, 9}

print(f"Sum of numbers: {sum(numbers)}")   # Output: 25

print(f"Min of numbers: {min(numbers)}")   # Output: 1

print(f"Max of numbers: {max(numbers)}")   # Output: 9

sorted()

Returns a new, sorted list of the items in the set.

Python

unsorted_set = {3, 1, 4, 1, 5, 9}

sorted_list_from_set = sorted(unsorted_set)

print(f"Original set: {unsorted_set}")

print(f"New sorted list: {sorted_list_from_set}")


Common Set Methods

Adding and Removing Items

  • .add(item): Adds a single item to the set.

Python

fruits = {"apple", "banana"}

fruits.add("cherry")

print(f"After .add('cherry'): {fruits}") # Output: {'apple', 'banana', 'cherry'}

  • .update(iterable): Adds all the items from an iterable (like a list or another set) to the set.

Python

fruits = {"apple", "banana"}

fruits.update(["cherry", "mango", "banana"])

print(f"After .update(): {fruits}") # Output: {'apple', 'banana', 'cherry', 'mango'}

  • .remove(item): Removes a specified item. It will raise a KeyError if the item is not found.

Python

fruits = {"apple", "banana", "cherry"}

fruits.remove("banana")

print(f"After .remove('banana'): {fruits}") # Output: {'apple', 'cherry'}

  • .discard(item): Removes a specified item, but it will not raise an error if the item is not found.

Python

fruits = {"apple", "banana", "cherry"}

fruits.discard("mango") # 'mango' is not in the set, but no error occurs.

print(f"After .discard('mango'): {fruits}")

  • .pop(): Removes and returns an arbitrary item from the set.

Python

fruits = {"apple", "banana", "cherry"}

removed_fruit = fruits.pop()

print(f"Removed fruit: {removed_fruit}, Set is now: {fruits}")

  • .clear(): Removes all items from the set.

Python

fruits = {"apple", "banana", "cherry"}

fruits.clear()

print(f"After .clear(): {fruits}") # Output: set()

Comparison Methods

  • .issubset(other): Returns True if all items in this set are also in the other set.

Python

set_a = {1, 2}

set_b = {1, 2, 3, 4}

print(f"Is {set_a} a subset of {set_b}? {set_a.issubset(set_b)}") # Output: True

  • .issuperset(other): Returns True if this set contains all items from the other set.

Python

set_a = {1, 2, 3, 4}

set_b = {1, 2}

print(f"Is {set_a} a superset of {set_b}? {set_a.issuperset(set_b)}") # Output: True

  • .isdisjoint(other): Returns True if the two sets have no items in common.

Python

set_a = {1, 2}

set_b = {3, 4}

print(f"Are {set_a} and {set_b} disjoint? {set_a.isdisjoint(set_b)}") # Output: True

 

Tuple Dictionaries
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website