Course Code: CSE 0613 2203B
Course Title: Introduction to Programming with
Python
Md. Shymon Islam
Lecturer
Department of Computer Science and Engineering
Shahjalal University of Science and Technology, Sylhet
Python 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, the other 3 are List, Set, and Dictionary, all with different
qualities and usage.
• A tuple is a collection which is ordered, unchangeable and allow
duplicates.
Python Tuples
Tuples are written with round brackets.
Tuple Items
Tuples items are indexed, the first item has index [0], the second item
has index [1] etc.
Tuple Length
To determine how many items a tuple has, use the len() function.
Create Tuple With One Item
To determine how many items a tuple has, 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.
Tuple Items - Data Types
Access Tuple Items
Range of Indexes
Range of Indexes
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.
Unpacking a Tuple
Packing a tuple:
Unpacking a tuple:
Join Tuples
Use the + operator.
Tuple Methods
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