Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Python Programming-20CS31P

WEEK -5

SETS AND TUPLES


• Mutability – ability to change
• In python, the data types are of two types
• Mutable and Immutable.
Mutability o Mutable : The data types whose values can be changed are termed as
mutable.
• Eg: List, Set, Dictionary
o Immutable : The data types whose values cannot be changed are
termed as immutable.
o Eg: int, float, bool, strings, Tuple, frozen sets
• myset = { “apple”, “banana”, “cherry”}
• Set is a collection of data items under a single name.
• Set is a collection of items, which is unordered, unchangeable*, and
unindexed.
• Set is a one of the four built in data types for data collection in python.
SETS- Features • The other 3 data types for data collection are List, Tuple and Dictionary.
• Duplicates are not allowed. – No two items with same value.
• Sets are written with curly braces i.e, { }
• Ex: set1 = {1, 2, 3, 4, 5} – set with integer values
• set2 = { “apple”, 25, True, 30.5,} – set with string, numbers and boolean
values
• Using curly braces { }
myset = {“apple”, “banana”, “cherry”}
print(myset)
Creating sets
• Using set( ) function
myset = set((“apple”, “banana”, “cherry”))
print(myset)
• Using for loop
myset = {“apple”, “banana”, “cherry”}
for x in myset:
Accessing set
print(x)
items
• Using in operator
myset = {“apple”, “banana”, “cherry”}
print(“banana” in myset)
 Get the type of the set
 Get the length of the set
 Add items to the set
 Remove item from the set
Operations on
 Clear the items of the set
sets
 Deleting the set
 Union
 Intersection
 Difference

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 1


Python Programming-20CS31P

• Get the data type of the set


myset = {“apple”, “banana”, “cherry”}
print(type(myset))

• Get the lenghth of the set using len( ) function


myset = {“apple”, “banana”, “cherry”}
print(len(myset))

• Adding an item to the set using add( ) function


myset = {“apple”, “banana”, “cherry”}
myset.add(“orange”)
print(myset)

• Remove item from the set using remove ( ) or discard ( ) function


myset = {“apple”, “banana”, “cherry”}
myset.remove(“apple”)
print(myset)

myset = {“apple”, “banana”, “cherry”}


myset.discard(“banana”)
print(myset)

myset = {“apple”, “banana”, “cherry”}


Operations on
myset.pop() - removes the last item in the set
sets
print(myset)

• Clear the all items of the set using clear ( ) function


myset = {“apple”, “banana”, “cherry”}
myset.clear(myset)
print(myset)

• Deleting the set using del function


myset = {“apple”, “banana”, “cherry”}
del myset
print(myset) # error is generated because myset no longer exists

• Joining two sets using update( ) or union ( ) function


fruits = {“apple”, “banana”, “cherry”}
fruits1 = {“kiwi”, “pineapple”, “papaya”}
fruits.update(fruits1)
print(fruits)

set1 = { “a”, “b”, “c”}


set2 = { 1, 2, 3 }
set3 = set1.union(set2)
print(set3)

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 2


Python Programming-20CS31P

• Intersection of two sets using intersection ( ) function


• The intersection method will return the items that are present in both the sets
Ex: fruits1 = {“apple”, “banana”, “cherry”}
fruits2 = {“kiwi”, “apple”, “papaya”}
fruits = fruits1.intersection(fruits2)
print(fruits)

• Difference of two sets using difference ( ) function


• The difference method will return a set containing the difference between two
sets.
Ex: fruits1 = {“apple”, “banana”, “cherry”}
fruits2 = {“kiwi”, “apple”, “papaya”}
fruits = fruits1.difference(fruits2)
print(fruits)
fruits = fruits2.difference(fruits1)
print(fruits)
• A set comprehension is a method to create new set using a list or a tuple or a
set.
• Ex: myList = [1, 2, 3, 4, 5]
Set newSet = { element*3 for element in myList }
Comprehension print(“ The existing list is : “)
print(myList)
print(“The new set is:”)
print(newSet)

• mytuple = (“apple”, “banana”, “cherry”)


• Tuple is a collection of data items under a single name.
• Tuple is a collection of items, which is ordered, unchangeable.
TUPLES - • Tuple is a one of the four built in data types for data collection in python.
Features • Duplicates are allowed.
• Tuples are written with parenthesis i.e, ( )
• Ex: tuple1 = (1, 2, 3, 4, 5) –tuple with integer values
• tuple2 = ( “apple”, 25, True, 30.5) – tuple with string, numbers and boolean
values

• Using parenthesis ( )
mytuple = (“apple”, “banana”, “cherry”) # creating tuple with 3 items
print(mytuple)
mytuple = ( ) # creating an empty tuple
Creating Tuples print(mytuple)
mytuple = ( “apple”,) # creating a tuple with one item
print(mytuple)
• Using tuple( ) function
mytuple = tuple((“apple”, “banana”, “cherry”))
print(mytuple)

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 3


Python Programming-20CS31P

• Using index number


mytuple = (“apple”, “banana”, “cherry”)
print(mytuple[1])
>>> “banana”
• First item in the tuple has index 0.
• Negative indexing means start from the end.
• -1 refers to last item, -2 refers to second last item
• mytuple = (“apple”, “banana”, “cherry”)
print(mytuple[-1])
>>> “cherry”

• Using in operator to check the existance of an item


mytuple = (“apple”, “banana”, “cherry”)
print(“apple” in mytuple)
>>> True
Accessing Tuple
Items • Using for loop
mytuple = (“apple”, “banana”, “cherry”)
for x in mytuple
print(x)

• Looping through index numbers


mytuple = (“apple”, “banana”, “cherry”)
for i in range(len(mytuple)):
print(mytuple[i])

• Using while loop


mytuple = (“apple”, “banana”, “cherry”)
i=0
while i < len(mytuple):
print(mytuple[i])
i=i+1

• Obtaining a subset of tuple elements is termed as tuple slicing


• mytuple= (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
print(mytuple[2:5]) # item in position 5 is NOT included
>>> (“cherry”, “orange”, “kiwi”)
Slicing Tuples
• mytuple= (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
print(mytuple[:4]) # item in position 4 is NOT included
>>> (“apple”, “banana”, “cherry”, “orange”)

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 4


Python Programming-20CS31P

mytuple= (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)


print(mytuple[2:])
>>> (“cherry”, “orange”, “kiwi” ,“melon”, “mango”)

mytuple= (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)


print(mytuple[-4:-1]) # item in position -1 is NOT included
>>> (“orange”, “kiwi”, “melon”)

 Get the type of the tuple


 Get the length of the tuple
 Count the number of occurrence of an item
 Get the position of the element
Operations on
 Updating tuples – adding and removing items
Tuples
 Joining tuples
 Multiplying tuples
 Packing and Unpacking of tuples
 Deleting a tuple

• Get the data type of the set


mytuple = (“apple”, “banana”, “cherry”)
print(type(mytuple))

• Get the lenghth of the set using len( ) function


mytuple = (“apple”, “banana”, “cherry”)
print(len(mytuple))

• Count the number of occurrence of an item using count( )


mytuple = (1,2,3,4,5,6,7,5,3)
x = mytuple.count(5)
print(x) >>> 2

• Get position of the item using index ( ) function


mytuple = (“apple”, “banana”, “cherry”)
x = mytuple.index(“banana”)
print(x) >>> 1

• Updating Tuples – adding items


• Convert the tuple into a list to add or remove items to it.
• Ex: x = (“apple”, “banana”, “cherry”)
y = list(x)
y[1] = “kiwi”
x = tuple(y)
print(x)

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 5


Python Programming-20CS31P

• Updating Tuples –removing items


• Ex: x = (“apple”, “banana”, “cherry”)
y = list(x)
y.remove(“apple”)
x=tuple(y)
print(x)

• Joining Tuples using “+” operator


t1 = ( “a”, “b”, “c”)
t2 = (1,2,3)
t3 = t1 + t2
print(t3)

• Multiplying Tuples using “*” operator


fruits = (“apple”, “banana”, “cherry”)
mytuple = fruits * 2
print(mytuple)

• Packing of Tuples
• assigning values to a tuple, when it is created is called packing
Ex : fruits = (“apple”, “banana”, “cherry”)

• Unpacking of Tuples
• extracting values from a tuple, back into variables is called unpacking
Ex : fruits = (“apple”, “banana”, “cherry”)
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)

• Deleting a tuple
Ex : fruits = (“apple”, “banana”, “cherry”)
del fruits
print(fruits) # error is generated, fruits not defined

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 6


Python Programming-20CS31P

Sample programs

# Python program to create a set and display it.

myset = {“apple”, “banana”, “cherry”}


print(myset)
print(type(myset))

# Python program to illustrate accessing set items


myset = {“apple”, “banana”, “cherry”}
for x in myset:
print(x)
print(“banana” in myset)

# Python program to demonstrate in built functions of sets


myset = {“apple”, “banana”, “cherry”}
print(len(myset))
myset.add(“orange”)
print(myset)
myset.remove(“apple”)
print(myset)
myset.discard(“banana”)
print(myset)
myset.clear(myset)
print(myset)
del myset
print(myset)

# Python program to demonstrate Union, Intersection and set difference opearations


fruits = {“apple”, “banana”, “cherry”}
fruits1 = {“kiwi”, “pineapple”, “papaya”}
fruits.union(fruits1)
print(fruits)
fruits = fruits1.intersection(fruits2)
print(fruits)
fruits = fruits1.difference(fruits2)
print(fruits)
fruits = fruits2.difference(fruits1)
print(fruits)

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 7


Python Programming-20CS31P

# Python program to illustrate set comprehension


myList = [1, 2, 3, 4, 5]
newSet = { element*3 for element in myList }
print(“ The existing list is : “)
print(myList)
print(“The new set is:”)
print(newSet)

# Python program to illustrate arithmetic operations


mytuple = (“apple”, “banana”, “cherry”)
print(mytuple)
print(type(mytuple))

# Python program to illustrate accessing tuple items

mytuple = (“apple”, “banana”, “cherry”)


print(mytuple[1])
print(mytuple[-1])
print(“apple” in mytuple)
for x in mytuple
for i in range(len(mytuple)):
print(mytuple[i])
mytuple = (“apple”, “banana”, “cherry”)
i=0
while i < len(mytuple):
print(mytuple[i])
i=i+1
print(x)

# Python program to illustrate tuple slicing.


mytuple= (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
print(mytuple[2:5]) # item in position 5 is NOT included
print(mytuple[:4]) # item in position 4 is NOT included
print(mytuple[2:])
print(mytuple[-4:-1]) # item in position -1 is NOT included

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 8


Python Programming-20CS31P

# Python program to demonstrate basic opearations

mytuple = (“apple”, “banana”, “cherry”)


print(len(mytuple))
x = mytuple.index(“banana”)
mytuple = (1,2,3,4,5,6,7,5,3)
x = mytuple.count(5)
del fruits
print(fruits)

# Python program to demonstrate adding and removing tuple items

x = (“apple”, “banana”, “cherry”)


y = list(x)
y[1] = “kiwi”
x = tuple(y)
print(x)
y = list(x)
y.remove(“apple”)
x=tuple(y)
print(x)

# Python program to demonstrate nesting and multiplying tuples


t1 = ( “a”, “b”, “c”)
t2 = (1,2,3)
t3 = t1 + t2
print(t3)
fruits = (“apple”, “banana”, “cherry”)
mytuple = fruits * 2
print(mytuple)

# Python program to demonstrate packing and unpacking of tuples


fruits = (“apple”, “banana”, “cherry”)
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)

SHESHADRI A V, DEPT OF CSE, GPT CHN Page 9

You might also like