IT Versity

making IT resourceful

  • Courses
  • Blog
  • Labs
  • Discuss

July 25, 2017 By Bhanu Kaushik Leave a Comment

Sets

Topic Progress:
← Back to Lesson
  • A set is a well defined collection of objects.
  • The elements or members of a set can be anything: numbers, characters, words, names, letters of the alphabet, even other sets, and so on.
  • A set contains an unordered collection of unique and immutable objects.
  • The set data type is, as the name implies, a Python implementation of the sets as they are known from mathematics.
  • If we want to create a set, we can call the built-in set function with a sequence or another iterable object.
  • Sets are implemented in a way, which doesn’t allow mutable objects.
  • Though sets can’t contain mutable objects, sets are mutable:

  • Set Operations
    • add(element) -> A method which adds an element, which has to be immutable.
    • clear() -> All elements will be removed from set.
    • copy() -> Creates a shallow copy, which is returned.
    • difference() -> This method returns the difference of two or more sets as a new set.
    • difference_update() -> The method difference_update removes all elements of another set from this set. x.difference_update(y) is the same as “x = x – y”
    • discard(el) -> An element el will be removed from the set, if it is contained in the set. If el is not a member of the set, nothing will be done.
    • remove(el) ->works like discard(), but if el is not a member of the set, a KeyError will be raised.
    • union(s) -> This method returns the union of two sets as a new set, i.e. all elements that are in either set.
    • intersection(s) -> Returns the intersection of the instance set and the set s as a new set. In other words: A set with all the elements which are contained in both sets is returned.
    • isdisjoint() -> This method returns True if two sets have a null intersection.
    • issubset() -> x.issubset(y) returns True, if x is a subset of y.
    • issuperset() -> x.issuperset(y) returns True, if x is a superset of y.
    • pop() -> removes and returns an arbitrary set element. The method raises a KeyError if the set is empty.

Related

← Previous Topic

Filed Under: Programming Languages, Python

Start the discussion at discuss.itversity.com

Socially Connected

  • Facebook
  • Google+
  • Linkedin
  • Twitter
  • YouTube
Getting Started
  • Setting Up Python Environment
  • Python Basics
  • Declaring Variables
  • Operators
  • Conditional Statements and Loops
  • Errors and Exceptions
Functions
  • Pre-Defined Functions
  • Defining Functions
  • Argument Types
  • Anonymous Functions
Collections
  • Lists
  • Dictionary
  • Tuples
  • Sets
  • Map Reduce APIs
  • Task - Get count by date
  • Exercise - Get revenue per day for completed and closed orders
Pandas - data analysis toolkit
  • Getting started with pandas
  • pandas - Data Structures
  • Transformations on DataFrames
  • Exercises - Python Collections and Pandas
Developing applications using Python
  • Creating the project using PyCharm
  • Define Problem Statement
  • Implementing the application using Python
Return to Learn Python for Spark

Copyright © 2018 · Education Pro Theme On Genesis Framework · WordPress · Log in