0% found this document useful (0 votes)
4 views17 pages

Tuples

Tuples are used to store multiple items in a single variable and are defined with round brackets. They are ordered, unchangeable, and can contain different data types, allowing duplicates. While tuples cannot be modified directly, they can be converted to lists for updates, and they support indexing, slicing, and various methods for accessing and counting items.

Uploaded by

gumbobuhle14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Tuples

Tuples are used to store multiple items in a single variable and are defined with round brackets. They are ordered, unchangeable, and can contain different data types, allowing duplicates. While tuples cannot be modified directly, they can be converted to lists for updates, and they support indexing, slicing, and various methods for accessing and counting items.

Uploaded by

gumbobuhle14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Tuples

mytuple = ("apple", "banana", "cherry")

 Used to store multiple items in a single


variable.

 Written with round brackets ().


 A tuple is ordered and unchangeable.
o Ordered
 the items have a defined order,
 and that order will not change.
o Unchangeable
 we cannot change, add or
remove items after the tuple has
been created.
Allow Duplicates
 Since tuples are indexed,
 they can have items with the same
value:

Data Types
 A tuple can contain different data types:

 tuple1 = ("abc", 34, True, "male")


The tuple () Constructor
 Used to make a tuple.

Indexing / Accessing tuple items


 Tuple items are indexed,
o the first item has index [0],
o the second item has index [1] etc.

Negative Indexing
 Starting from the end.
o -1  last item,
o -2  second last item etc.

Slicing
 Ranges of Indexes
o where to start and where to end the
range.
o new tuple with the specified items
will be returned.

o Note: The search will start at index 2


(included) and end at index 5 (not
included).

Not specifying start value [ :4]


 starts at the first item:

Not specifying end value [3: ]


 will go on to the end of the list:
Where tuples can be used?

Check if Item Exists


 e.g. check if banana is present in the
tuple, thistuple
 use the in keyword:

Update Tuples
 Tuples are unchangeable,
o meaning that you cannot change,
add, or remove items once the tuple
is created.
 But there are some workarounds. i.e.
i. convert the tuple into a list(changeable),
ii. change the list,
iii. convert back to a tuple. (type
casting)
 e.g. insert another item to the tuple, X
Add Items
 Since tuples are unchangeable,
o they do not have a built-in append()
method,
 So to do it,
i. Convert into a list,
ii. Use append () on the list
iii. convert it back into a tuple.
 e.g. append item to tuple, X
Remove Items
 Note: You cannot remove items in a
tuple.
 Tuples are unchangeable,
 so you can use the same workaround
as we used for changing and adding
tuple items:
Unpack Tuples
 When we create a tuple,
o we normally assign values to it,
called "packing" a tuple: e.g.
o fruits = ("apple", "banana", "cherry")
 We can also extract the values back and
store the values into individual variables.
o This is called "unpacking":

 Nb: No of variables must match the no


of values in the tuple,
Using Asterisk*
 If No of variables is less than No of
values,
 you can add an * to the variable name
and the values will be assigned to the
variable as a list:

Loop Through a Tuple


For loop
for loop-variable in tuple

Loop Through the Index Numbers


 by referring to their index number.
o Use the range () and len () functions
to create a suitable iterable.

Using a While Loop


 Use the len () function to determine the
length of the tuple,
 then start at 0,
 loop your way through the tuple items by
referring to their indexes.
 Remember to increase the index by 1
after each iteration.

Tuple Methods
.count() Method
 Return the number of times a value appears
in the tuple. E.g. 5

.index() Method
 Returns position of first occurrence of a
value,
 E.g. index of 8 will be 3

You might also like