0% found this document useful (0 votes)
2 views15 pages

Python Lecture 5

The document provides an overview of Python tuples, highlighting their characteristics as ordered, unchangeable collections that allow duplicates. It explains how to create tuples, access their items, and the methods available for manipulating them, such as count() and index(). Additionally, it mentions the immutability of tuples and a workaround for changing their values by converting them to lists.

Uploaded by

2022332023
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)
2 views15 pages

Python Lecture 5

The document provides an overview of Python tuples, highlighting their characteristics as ordered, unchangeable collections that allow duplicates. It explains how to create tuples, access their items, and the methods available for manipulating them, such as count() and index(). Additionally, it mentions the immutability of tuples and a workaround for changing their values by converting them to lists.

Uploaded by

2022332023
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/ 15

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

You might also like