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

Python 6-1 220510 092851

This document provides an overview of tuples in Python, highlighting their immutable nature and how they differ from mutable objects. It includes definitions, examples of tuple operations, and demonstrates how to convert lists or strings into tuples. The document emphasizes the efficiency of tuples and their role in Python dictionaries.
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)
11 views8 pages

Python 6-1 220510 092851

This document provides an overview of tuples in Python, highlighting their immutable nature and how they differ from mutable objects. It includes definitions, examples of tuple operations, and demonstrates how to convert lists or strings into tuples. The document emphasizes the efficiency of tuples and their role in Python dictionaries.
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/ 8

Lecture 6

Data Structures in Python Programming:


Tuples

M. Asif Farooq

1/8
Mutable and Immutable Objects

An object holds the data and has operations that can manipulate
the data.
Numbers, strings, lists and tuples are objects.
Objects that can be changed in places are called mutable.
Objects that cannot be changed in place are called immutable.

2/8
Defining Tuples

Tuples are immutable


The items should be written in parentheses.
The elements should be separated by comma.

3/8
Tuples: Examples
In [54]: t = (3, 4 , 5)
In [55]: print(len(t))
3
In [56]: print(max(t))
5
In [57]: print(min(t))
3
In [58]: print(sum(t))
12
In [59]: t[0]
Out[59]: 3
In [60]: t[0] = t[0] + 3
Traceback (most recent call last):
File "C:1 2760786003402.py ”, line1, in < cellline : 1 >
t[0] = t[0] + 3
TypeError: ’tuple’ object does not support item assignment

4/8
Comments on Tuples

The tuple function converts lists or strings to lists.


Tuples are more efficient than lists and should be used in
situations where no changes will be made to the items.
An important feature of Python is dictionary. The dictionary
requires the use of tuples.

5/8
Examples: Converting Lists or Strings into Tuples
In [1]: tuple([’fruits’, ’vegetables’])
Out[1]: (’fruits’, ’vegetables’)

In [2]: tuple([2, ’students’])


Out[2]: (2, ’students’)

In [3]: tuple("spam")
Out[3]: (’s’, ’p’, ’a’, ’m’)

In [7]: a[0] + ’t’


Out[7]: ’st’

In [8]: a[0] = a[0] + ’t’


Traceback (most recent call last):
File "C:1 54450862836.py ”, line1, in < cellline : 1 > a[0] = a[0] +′ t ′
TypeError: ’tuple’ object does not support item assignment
6/8
Examples
In [17]: L = [5,6]
In [18]: L.append(7)
In [19]: L
Out[19]: [5, 6, 7]
In [20]: n = 2
In [21]: n+=1
In [22]: n
Out[22]: 3
In [23]: s = "Python"
In [24]: s = s.upper()
In [25]: s
Out[25]: ’PYTHON’
In [26]: t = (’a’, ’b’, ’c’)
In [27]: t = t[1:]
In [28]: t
Out[28]: (’b’, ’c’)

7/8
Examples: Tuples
In [10]: t = (2,3,1,3)
In [11]: print(t[1])
3
In [12]: t.index(3)
Out[12]: 1
In [15]: t.count(3)
Out[15]: 2
In [16]: len(t)
Out[16]: 4
In [17]: sum(t)
Out[17]: 9
In [18]: t + (7,5)
Out[18]: (2, 3, 1, 3, 7, 5)
In [19]: t*2
Out[19]: (2, 3, 1, 3, 2, 3, 1, 3)

8/8

You might also like