You are on page 1of 16

70

PART 4 – B: TUPLES
71

TUPLES

A tuple is a collection which is ordered and unchangeable. In Python tuples are written
with round brackets ().

thistuple = ("apple","banana","cherry") Output:


print(thistuple) ('apple', 'banana', 'cherry')
72

ACCESS TUPLE ITEMS


• You can access tuple items by referring to the index number, inside square brackets:

thistuple = ("apple","banana","cherry") Output:


print(thistuple[1]) banana

• Once a tuple is created, you cannot change its values. Tuples are unchangeable.

thistuple = ("apple","banana","cherry") Output:


thistuple[1]="blackcurrant" TypeError: 'tuple' object does not
print(thistuple) support item assignment
73

TUPLES

• You can loop through the tuple items by using a for loop.

thistuple = ("apple","banana","cherry") Output:


apple
for x in thistuple:
banana
print(x) cherry

• To determine if a specified item is present in a tuple use the in keyword:

thistuple = ("apple","banana","cherry") Output:


if "apple" in thistuple: Yes, 'apple' is in the fruits
print("Yes, 'apple' is in the fruits tuple") tuple
74

TUPLES
• To determine how many items a tuple has, use the len() method:

thistuple = ("apple","banana","cherry") Output:


print(len(thistuple)) 3

• Tuples are unchangeable, so you cannot remove items from it, but you can delete
the tuple completely:

thistuple = ("apple","banana","cherry") Output:


del thistuple NameError: name 'thistuple' is not
print(thistuple) defined
75

TUPLES
• Using the tuple() method to make a tuple:

thistuple = tuple(("apple","banana","cherry")) Output:


print(thistuple) ('apple', 'banana', 'cherry')

• Python has two built-in methods that you can use on tuples.

Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found
76

TUPLES

thistuple = (1,3,7,8,7,5,4,6,8,5) Output:


x = thistuple.count(5) 2
print(x)

thistuple = (1,3,7,8,7,5,4,6,8,5)
Output:
x = thistuple.index(8)
3
print(x)
77

PART 4 – C: SETS
78

SETS
• A set is a collection which is unordered and unindexed. In Python sets are written
with curly brackets { }.

Output:
thisset = {"apple","banana","cherry"} {'apple', 'banana', 'cherry'}
print(thisset) Or
{'cherry', 'apple', 'banana'}

• Note: Sets are unordered, so the items will appear in a random order.
79

SETS
• You cannot access items in a set by referring to an index, since sets are unordered
the items has no index.
• But you can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword.

thisset = {"apple","banana","cherry"} Output: Output:


cherry apple
for x in thisset:
banana cherry
print(x) apple banana

thisset = {"apple","banana","cherry"} Output:


print("banana" in thisset) True
80

SETS
• Once a set is created, you cannot change its items, but you can add new items.
• To add one item to a set use the add() method.
• To add more than one item to a set use the update() method.

thisset = {"apple","banana","cherry"}
Output:
thisset.add("orange")
{'cherry', 'banana', 'apple', 'orange'}
print(thisset)

thisset = {"apple","banana","cherry"}
thisset.update(["orange","mango","grapes"])
print(thisset)

Output:
{'mango', 'apple', 'grapes', 'orange', 'banana', 'cherry'}
81

SETS
• To determine how many items a set has, use the len() method.
• To remove an item in a set, use the remove(), or the discard() method.

thisset = {"apple","banana","cherry"} Output:


print(len(thisset)) 3

Note: If the item to


thisset = {"apple","banana","cherry"} Output: remove does not
thisset.remove("banana") {'cherry', 'apple'} exist, remove() will
print(thisset) raise an error.

thisset = {"apple","banana","cherry"} Note: If the item to


Output: remove does not exist,
thisset.discard("banana")
{'cherry', 'apple'} discard() will NOT raise
print(thisset) an error.
82

SETS
• You can also use the pop(), method to remove an item, but this method will remove
the last item. Remember that sets are unordered, so you will not know what item that
gets removed.

• The return value of the pop() method is the removed item.

thisset = {"apple","banana","cherry"}
Output:
x =thisset.pop()
apple
print(x)
{'cherry', 'banana'}
print(thisset)
83

SETS
• The clear() method empties the set:

• The del keyword will delete the set completely:

thisset = {"apple","banana","cherry"} Output:


thisset.clear() set()
print(thisset)

thisset={"apple","banana","cherry"}
Output:
del thisset
NameError: name 'thisset' is not defined
print(thisset)
84

SETS
• It is also possible to use the set() constructor to make a set.

thisset=set(("apple","banana","cherry"))
Output:
# note the double round-brackets
{'banana', 'cherry', 'apple'}
print(thisset)
85

SETS
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set

discard() Remove the specified item


intersection() Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from this set and another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others

You might also like