Sayantan's Blog On Python Programming

INTRODUCTION TO PYTHON SETS

INTRODUCTION TO PYTHON SETS

INTRODUCTION

  • Python Sets are collection of unique elements
  • Set contains no duplicates
  • If duplicate elements are added in sets then they will be visible once in output.
  • Sets do not maintain any intrinsic order
  • Sets do not contain any mutable elements like list, dictionaries etc. But tuples are allowed in sets as they are immutable.
  • Sets are enclosed with curly braces ({})

SET DATA TYPE

# INTRODUCTION TO PYTHON SETS
# Example 1

class_wise_prog_set = {'Python', 'XML', 'PHP', 'Java', 'C++'}
print("\nData type of class_wise_prog_set ->", type(class_wise_prog_set))

Data type of class_wise_prog_set -> <class 'set'>

Data type of elements in the set can be of any data type.

STRING SET

String sets are containing all the string elements.

# INTRODUCTION TO PYTHON SETS
# Example 2

class_wise_prog_set = {'Python', 'XML', 'PHP', 'Java', 'C++'}
print("\nList of elements in class_wise_prog_set ->", class_wise_prog_set)

INTRODUCTION TO PYTHON SETS : Output

List of elements in class_wise_prog_set -> {'XML', 'Java', 'Python', 'PHP', 'C++'}

In the above example print command just has printed all the elements in no particular order. Now if we change the order in the set then the printing order may not catch the changed order. In fact the order will change in every execution of run command.

# INTRODUCTION TO PYTHON SETS
# Example 3

class_wise_prog_set = {'XML', 'PHP', 'Python', 'Java', 'C', 'C++'}
print("\nList of elements in class_wise_prog_set ->", class_wise_prog_set)

INTRODUCTION TO PYTHON SETS : Output

List of elements in class_wise_prog_set -> {'Python', 'C++', 'C', 'Java', 'XML', 'PHP'}

NUMBER SETS

Number sets contain all the number elements either Integer or Floating point number.

INTEGER SETS

Integer sets contain all the integer number elements.

# INTRODUCTION TO PYTHON SETS
# Example 4

int_set = {400, 200, 600, 100, 150, 500}
print("\nList of elements in int_set ->", int_set)

INTRODUCTION TO PYTHON SETS : Output

List of elements in int_set -> {400, 100, 500, 150, 200, 600}

FLOATING SETS
# INTRODUCTION TO PYTHON SETS
# Example 5

float_set = {4.6, 2.8, 6.5, 1.9, 15.3, 5.9}
print("\nList of elements in float_set ->", float_set)

INTRODUCTION TO PYTHON SETS : Output

List of elements in float_set -> {1.9, 2.8, 4.6, 5.9, 6.5, 15.3}

BOOLEAN SETS

# INTRODUCTION TO PYTHON SETS
# Example 6

bool_set = {True, False}
print("\nList of elements in bool_set ->", bool_set)

INTRODUCTION TO PYTHON SETS : Output

List of elements in bool_set -> {False, True}

COMBINED DATA TYPE SETS

# INTRODUCTION TO PYTHON SETS
# Example 7

comb_set = {'Python', 'Java', 400, 150, 7.3, 1.9, True, False}
print("\nList of elements in comb_set ->", comb_set)

INTRODUCTION TO PYTHON SETS : Output

List of elements in comb_set -> {False, 1.9, True, 7.3, 400, 'Java', 'Python', 150}

In the above example we can see that comb_set variable contains elements of String, Number, Floating number, Boolean data type.

DUPLICACY IN SETS

Set contains no duplicates. If duplicate elements are added in sets then they will be visible once in output.

# INTRODUCTION TO PYTHON SETS
# Example 8
# Duplicacy in sets example

# String set example

str_set = {'Python', 'Java', 'C', 'Python', 'XML', 'Java'}
print("\nString set contains -> ", str_set)

# Integer set example

int_set = {4, 6, 2, 4, 15, 6}
print("\nInteger set contains -> ", int_set)

# Floating set example

float_set = {5.8, 2.6, 5.8, 6.3, 9.5, 2.6}
print("\nFloating set contains -> ", float_set)

# Boolean set example

bool_set = {True, False, True}
print("\nBoolean set contains -> ", bool_set)

# Combined data type set example

comb_set = {'Python', 10, 12, 5.8, 6, 5.8, True, 'Java', True, (2, 5, 6), (2, 5, 6), ('XML', 'C++'), (True, False)}
print("\nCombined set contains -> ", comb_set)

INTRODUCTION TO PYTHON SETS : Output

String set contains ->  {'XML', 'C', 'Python', 'Java'}

Integer set contains ->  {2, 4, 6, 15}

Floating set contains ->  {9.5, 2.6, 5.8, 6.3}

Boolean set contains ->  {False, True}

Combined set contains ->  {('XML', 'C++'), True, 5.8, 6, 10, 12, 'Python', (True, False), (2, 5, 6), 'Java'}

In the above example we can see that every type of set contains duplicate values. But in output all the duplicates have been removed.
In String set Python and Java both elements are occurred twice but in output just once.
In Integer set 6 is occurred twice but in output just once.
In Floating set 5.8 and 2.6 both are occurred twice but in output just once
In Boolean set True is occurred twice but in output just once.
In Combined set also the duplicate elements including tuple element containing (2, 5, 6) occurred just once.

INTRODUCTION TO PYTHON SETS : Output
INTRODUCTION TO PYTHON SETS : Output

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *