0% found this document useful (0 votes)
23 views8 pages

Tuples

Uploaded by

supriya sundaram
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)
23 views8 pages

Tuples

Uploaded by

supriya sundaram
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/ 8

Tuples

 Tuples
 Tuples are used to store multiple items in a single
variable.
 Tuple is one of 4 built-in data types in Python used
to store collections of data
 A tuple is a collection which is ordered and
unchangeable.
 Tuples are written with round brackets.
Tuples
 Creating Tuples
 Tuples can be created using ()
 thistuple = ("apple", "banana", "cherry")
print(thistuple)
• Tuple Items
– Tuple items are ordered, unchangeable, and allow
duplicate values.
– Tuple items are indexed, the first item has index [0],
the second item has index [1] etc.
Tuples
 Characteristics
 Ordered
 When we say that tuples are ordered, it means that the
items have a defined order, and that order will not
change.
 Unchangeable
 Tuples are unchangeable, meaning that 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:
Tuples
• Tuple Length
 To determine how many items a tuple has, use
the len() function
 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.
Tuples
• Example
thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple

thistuple = ("apple")
print(type(thistuple))
Tuples
• 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)
Tuples
• How to Access Items From a Tuple
• We can access a tuple’s elements by referring to the index
number. Remember that the index in a Python tuple starts
from ‘0’.
• To access elements in a tuple, we provide the index (as
integer) inside the square brackets ( [ ] )
• To access the elements from the end of the tuple, we use
negative indexing. So, -1 means the last element, -2 the
second last element, and so on.
Tuples
• How to Access Items From a Tuple
• We can also access the elements from a specific
range.
– Syntax: tuple_name[starting index : ending index]
• Remember that the first item is position 0.
– Note: The search will start at index 0 (included) and
end at index 4 (not included)
– If we leave out the starting value, the range will
begin from 0th index.

You might also like