0% found this document useful (0 votes)
15 views25 pages

3 Python List, Tuple, Set 05-10-2022

Uploaded by

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

3 Python List, Tuple, Set 05-10-2022

Uploaded by

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

Python Tuples

A tuple is another sequence data type that is similar to the list.

A tuple consists of a number of values separated by commas.

Unlike lists, however, tuples are enclosed within parentheses.

The main differences between lists and tuples are:

Lists are enclosed in brackets ( [ ] ) and their elements and size can be
changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated.

Tuples can be thought of as read-only lists


Tuple Items
Tuple items are ordered, unchangeable, and allow
duplicate values.
You cannot delete or add elements in a tuple, once it is
created.

Tuple items are indexed, the first item has index


[0], the second item has index [1] etc.
Allow Duplicates
Since tuples are indexed, they can have items with the same
value:

Example
Tuples allow duplicate values:

thistuple = ("apple", "banana", "cherry",


"apple", "cherry")
print(thistuple)
Create Tuple With One Item
To create a tuple with only one item, you have to
add a comma after the item, otherwise Python
will not recognize it as a tuple.

Example
One item tuple, remember the comma:
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
Tuple Items - Data Types
Tuple items can be of any data type:

Example
String, int and boolean data types:

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


tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
Tuple4=(1,”apple”,2,”banana”)
A tuple can contain different data types:

Example
A tuple with strings, integers and boolean
values:

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


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

Example
Print the second item in the tuple:

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


print(thistuple[1])
Check if Item Exists
To determine if a specified item is present
in a tuple use the in keyword:

Example
Check if "apple" is present in the tuple:

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


if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
Negative Indexing
Negative indexing means start from the end.

-1 refers to the last item, -2 refers to the second


last item etc.

Example
Print the last item of the tuple:

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


print(thistuple[-1])
Range of Indexes
You can specify a range of indexes by specifying where to
start and where to end the range.
When specifying a range, the return value will be a new
tuple with the specified items.

Example
Return the third, fourth, and fifth item:

thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "mel
on", "mango")
print(thistuple[2:5])
indexing
thistuple =
("apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango")
print(thistuple[:4])

thistuple =
("apple", "banana", "cherry", "orange", "
kiwi", "melon", "mango")
print(thistuple[2:])
Python - Update Tuples
Change Tuple Values

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


are unchangeable, or immutable as it also is called.
But there is a workaround. You can convert the tuple into a list,
change the list, and convert the list back into a tuple.

Example
Convert the tuple into a list to be able to change it:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Add Items
Since tuples are immutable, they do not have a build-in append()
method, but there are other ways to add items to a tuple.

1. Convert into a list: Just like the workaround for changing a


tuple, you can convert it into a list, add your item(s), and convert it
back into a tuple.

Example
Convert the tuple into a list, add "orange", and convert it back into
a tuple:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
Add tuple to a tuple. You are allowed to add tuples to
tuples, so if you want to add one item, (or many), create
a new tuple with the item(s), and add it to the existing
tuple:
Example
Create a new tuple with the value "orange", and add that
tuple:

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


y = ("orange",)
thistuple += y

print(thistuple)
Remove Items
Tuples are unchangeable, so you cannot remove items
from it, but you can use the same workaround as we used
for changing and adding tuple items:

Example
Convert the tuple into a list, remove "apple", and convert
it back into a tuple:

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


y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
The del keyword can delete the tuple completely:
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple)
Unpacking a tuple:
we are also allowed to extract the values
back into variables. This is called
"unpacking":

Unpacking a tuple:
fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)
print(yellow)
print(red)
Packing a tuple:
fruits = ("apple", "banana", "cherry")
Join Two Tuples
To join two or more tuples you can use the +
operator:

Example
Join two tuples:

tuple1 = ("a", "b" , "c")


tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2


print(tuple3)
Multiply Tuples
If you want to multiply the content of a tuple
a given number of times, you can use the *
operator:

Example
Multiply the fruits tuple by 2:

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


mytuple = fruits * 2

print(mytuple)
Python - Tuple Methods
Tuple Methods
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
Change Tuple Values
Once a tuple is created, you cannot change its values.
Tuples are unchangeable, or immutable as it also is called.

But there is a workaround. You can convert the tuple into a


list, change the list, and convert the list back into a tuple.

Example
Convert the tuple into a list to be able to change it:

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


y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Add Items
Since tuples are immutable, they do not have a build-in
append() method, but there are other ways to add items to
a tuple.
1. Convert into a list: Just like the workaround for
changing a tuple, you can convert it into a list, add your
item(s), and convert it back into a tuple.
Example
Convert the tuple into a list, add "orange", and convert it
back into a tuple:

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


y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
Remove Items

Tuples are unchangeable, so you cannot remove items


from it, but you can use the same workaround as we used
for changing and adding tuple items:

Example
Convert the tuple into a list, remove "apple", and convert
it back into a tuple:

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


y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
Unpacking a Tuple
When we create a tuple, we normally assign values to it.
This is called "packing" a tuple:
Example
Packing a tuple:
fruits = ("apple", "banana", "cherry")
But, in Python, we are also allowed to extract the values
back into variables. This is called "unpacking":
Example
Unpacking a tuple:

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


(green, yellow, red) = fruits

print(green)
print(yellow)
print(red)

You might also like