Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Python Set

Python set is the unordered collections of the elements which are non-duplicated, unindexed, and not changeable. However, the set itself is a mutable data structure in which we can add or remove elements.

Sets are created using two curly brackets.

#Create a set


A set cannot have two same elements, i.e, duplicated elements. When we print the set, all the members with the no-duplicate members are printed.



{'Berries', 'Apple', 'Orange', 'Banana'}
Set with multiple data types

We can store multiple non-duplicated data types in the set except they are mutable. So, mutable elements cannot be stored in the set

set1 = {"abc", 34, True, 40, "male"}


set3={"Hello","Python",5,[1,2,3]} #but cannot add list


{"abc", 34, True, 40, "male"}
{'Python', (1, 2, 3), 5, 'Hello'}

TypeError: unhashable type: 'list'
Set Modifications

Data inside the sets are immutable. But the set itself is a mutable data structure. Inset datatype, we can't perform slicing and indexing but we can add or remove the items to and from the set using different functions available

Add() and Update() Function

Add() function simply adds the element to set and Update() function adds an iterable to set. We can append a single element using add() function and we can append iterables using update() function.



   {1, 2, 3, 4, 5, 12}

>>>#add list and set
   {1, 2, 3, 4, 5, 35, 12, 44, 23, 26}
Removing the elements from the set

Basically, we have two functions in python to remove an item from the set. One is remove() and another one is discard(). Both are quite similar, But only one difference is, if we try to remove the element which is not in the list, the discard() method will leave the set unchanged but the remove() function produces an error.

   {1, 2, 3, 4, 5}

   {3, 4, 5}


>>>#let's remove the number which is not on the set.

   #KeyError: 6 (Error got)
The pop() and clear() function.

We can remove and return an item from the set using the pop() function. As the set is an unordered, unindexed datatype, there is no way to determine which element is popped and returned. The clear() function is used to remove all the items from the set.

Set Operations

Set can handle some set operations such as Union of two sets, Intersection of two sets, Difference and Symmetric Difference between two sets.

Firstly let us create two sets, Do some operations which are discussed below.

  • Set Union

    Set union can be performed using either | operator or union() method.

    >>>print(set1|set2) #Using Operator.
       {'e', 'a', 'g', 'b', 'c', 'f', 'd'}
    >>>#Using union() method.
       {'e', 'a', 'g', 'b', 'c', 'f', 'd'}
  • Set Intersection

    The intersection of two sets is the set of common elements. Set Intersection can be performed using either & operator or intersection() method.

    >>>print(set1&set2) #Using Operator.
  • Set Difference

    The difference of set1 from set2 i.e, set2-set1 is the set of elements that are only in set2 not in set1. Similarly, the difference of set2 from set1 is the set of elements that are only in set1 not in set2. We can perform set differences using the '-' operator as well as using the difference() method.

    >>>#The difference of set2 from set1
       {'b', 'a'}
       {'b', 'a'}
    #The different of set1 from set2
       {'f', 'g'}
       {'f', 'g'}
  • Symmetric Difference

    The symmetric difference between set1 and set2 is the elements that are in set1 and set2 but not in both. That is the set of elements that are in set1 and set2 except intersection elements are called the symmetric difference.

    The symmetric difference is calculated using the '^' operator or symmetric_difference() method as follows:

       {'f', 'g', 'a', 'b'}
       {'f', 'g', 'a', 'b'}
    >>>#this is similar to:
       {'f', 'g', 'a', 'b'}
       {'f', 'g', 'a', 'b'}
  • Looping through the set

    We can use for loop to iterate through the set.

    for n in names:
  • Membership Test in set

    We can check whether an element is present in the set or not by using the membership test.

    if "Harry" in Names:
       print("It is in the set.")
    It is in the set.

Some Builtin Functions in Set

Some of the built-in functions that we can use in the set are discussed below:

>>>number_set={2,3,1,4,5,0} #create a set of number

>>>print(max(number_set)) #prints maximum number from the set.
>>>print(min(number_set)) #prints minimum numbers from the set.
>>>print(len(number_set)) #prints the length of the set.
>>>print(sorted(number_set)) #prints sorted set.
   [0, 1, 2, 3, 4, 5]
>>>print(sum(number_set)) #prints sum of the elements of the set.
>>>print(enumerate(number_set)) #prints an enumerate object.
   <enumerate object at 0x00000244B139FDC0>