










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Assignment No.
On
What is tuple? comparison with list, how tuples are created? basic tuple operations, built-in functions used in tuples, and examples.
Submitted By: ABDULLAH CU-1230-
session 2020-
Submitted To : Dr. Kifayat Ullah Assistant Professor Computer Science
DEPARTMENT OF COMPUTER SCIENCE CECOS University of IT and Emerging Sciences Peshawar 2 January 2021
A collection is a single object representing a group of
objects.
Or
Collections in Python are containers that are used to store collections of data .collections module was introduced to improve the functionalities of the built-in collection containers.
These collection types support various types of data
structures and ways to process elements within those structures.
There are four collection data types in the Python. o List o Tuple o Set o Dictionary
In Python, a tuple is similar to List except that the objects in tuple are immutable which means we cannot change the elements of a tuple once assigned. On the other hand, we can change the elements of a list.
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 and unchangeable.
Tuples are written with round brackets
Syntax of list and tuple is slightly different. Lists are surrounded by square brackets [] and Tuples are surrounded by parenthesis ().
Above, we defined a variable called list_num which hold a list of
numbers from 1 to 4.The list is surrounded by brackets []. Also, we
defined a variable tup_num; which contains a tuple of number from 1 to 4. The tuple is surrounded by parenthesis ().
List has mutable nature i.e., list can be changed or modified after its
creation according to needs whereas tuple has immutable nature i.e., tuple can’t be changed o r modified after its creation.
list_num[2] = 5 print(list_num)
tup_num[2] = 5 In above code we assigned 5 to “ list_num ” at index 2 and we found 5 at index 2 in output. Also, we assigned 5 to “ tup_num ” at index 2 and
we got type error. We can't modify the tuple due to its immutable
nature.
Tuples operation has smaller size than that of list, which makes it a bit faster but not that much to mention about until you have a huge number of elements
a= (1,2,3,4,5,6,7,8,9,0) b= [1,2,3,4,5,6,7,8,9,0]
print('a=',a.sizeof()) print('b=',b.sizeof())
Implication of iterations is comparatively Faster
Tuple data type is appropriate for accessing the elements
Tuple does no have must built-in methods.
In tuple, it is hard to take place.
There are 33 available methods on tuples.
Since tuples are quite similar to lists, both of them are used in similar situations. However, there are certain advantages of implementing a tuple over a list. Below listed are some of the main advantages:
o We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar) data types. o Since tuples are immutable, iterating through a tuple is faster than with list. So there is a slight performance boost. o Tuples that contain immutable elements can be used as a key for a dictionary. With lists, this is not possible. o If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
For the creating a python tuple .first we are
To declare a Python tuple, you must type a list of items separated by commas, inside parentheses. Then assign it to a variable.
>>> percentages=(90,95,89)
You should use a tuple when you don’t want to change just an item in future.
To get around this, we add a comma after the item.
>>> a=(1,)
>>> type(a)
Problem solved. And as we saw in tuple packing, we can skip the parentheses here.
>>> a=1,
>>> type(a)
In this example, we are creating few tuples. We can have tuple of same type of data items as well as mixed type of data items. This example also shows nested tuple (tuples as data items in another tuple).
my_data = ("hi", "hello", "bye") print(my_data)
ple of int, float, string my_data2 = ( 1 , 2.8, "Hello World") print(my_data2)
my_data3 = ("Book", [ 1 , 2 , 3 ]) print(my_data3)
my_data4 = (( 2 , 3 , 4 ), ( 1 , 2 , "hi")) print(my_data4) Output:
('hi', 'hello', 'bye') ( 1 , 2.8, 'Hello World') ('Book', [ 1 , 2 , 3 ]) (( 2 , 3 , 4 ), ( 1 , 2 , 'hi'))
my_data = ()
Note: When a tuple has only one element, we must put a comma after the element, otherwise Python will not treat it as a tuple.
# a tuple with single data item my_data = ( 99 ,)
Like a list, a Python tuple is of a certain length. The len() function returns its length.
my tuple
(1, 2, 3, [6, 5])
Len(my tuple)
It returned 4, not 5, because the list counts as 1.
It returns the item from the tuple with the highest value.
We can’t apply this function on the tuple my tuple, because its cannot be compared to a list. So let’s take yet another tuple in Python.
a=(3,1,2,5,4,6) max(a)
6
Let’s try that on strings.
max(('Hi','hi','Hello'))
‘hi’
‘hi’ is the greatest out of these, because h has the highest ASCII value among h and H.
But you can’t compare an int and a string.
max(('Hi',9))
Trace back (most recent call last):
**File “”, line 1, in **
sum((‘1′,’2′,’3’))
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
If even one item in the tuple has a Boolean value of True, then this function returns True. Otherwise, it returns False.
any(('','0',''))
True
The string ‘0’ does have a Boolean value of True. If it was rather the integer 0, it would’ve returned False.
any(('',0,''))
False
Unlike any(), all() returns True only if all items have a Boolean value of True. Otherwise, it returns False.
all(('1',1,True,''))
False
This function returns a sorted version of the tuple. The sorting is in ascending order, and it doesn’t modify the original tuple in Python.
sorted(a)
[1, 2, 3, 4, 5, 6]
This function converts another construct into a Python tuple. Let’s look at some of those.
list1=[1,2,3] tuple(list1)
(1, 2, 3)
string1="string" tuple(string1)
(‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’)
How well would it work with sets?