0% found this document useful (0 votes)
15 views19 pages

Tuples Eng

Engengtupless.ia

Uploaded by

gamer55515555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views19 pages

Tuples Eng

Engengtupless.ia

Uploaded by

gamer55515555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Tuples

A tuple is a sequence or ordered collections of elements of different data types.

Characteristics of Tuple:

 Tuple is a sequence Type. Tuples are a sequence of any elements.


 Tuples are Immutable. The elements in tuple are fixed: that is, once a tuple is created,
we cannot add new elements, delete elements, replace elements, or record the
elements in the tuple.
 A tuple can have elements of different data types, such as integer, float, strings, list, or
even another tuple.
 A tuple contains items separated by commas and enclosed within parentheses ().
 A tuple are indexed. Elements in a tuple can be accessed through an index. The first
item has index [0], the second item has index [1] and so on.
 The values stored in a tuple can be accessed using the slice operator ([] and [:]) with
indexes starting at 0 in the beginning of the list.

Creating Tuples:

Tuples can be created by placing the elements in parentheses (). Each value inside a tuple is
called an element or item. The elements in the tuple are separated by the commas (,).

Syntax:- tuple_name=()
tuple_name=(element 1,element 2,.....element n)

tuple_name=tuple()
tuple_name=tuple(sequence)

Examples : creating tuple Using ()

emptytuple = () #Creating a Empty tuple

inttuple = (1,2,3) #Creating tuple of integers

floattuple = (10.20, 13.75, 100.5, 90.56) #Creating tuple of floats

colors = ("red”,”green”,”blue”) #Creating tuple of Strings

students = (1897,”Rakesh”,98.85) #Creating tuple of mixed values

nestedtuple = (‘Rama’, 4, 1, (5,23,4), 98) #Creating nested tuple


nestedtuple contains 5 elements while inner tuple contains 3 elements (5,23,4). The inner tuple (5,23,4) is
considered as one element in nestedtuple.

Creating tuples using tuple() function

In python, we use tuple() method to create a tuple from tuple() method is also used to create a tuple through
an existing sequence keyboard

Syntax: tuple_name=tuple(sequence) Example:

Sequence can be list, strings or tuples >>> a = tuple(input(“Enter the elements :”)

Example: Enter the elements : 23456

>>>str = "PYTHON" >>> print (a)


>>>A1 =tuple(str) (‘2’ , ‘3’ ,’4’, ‘5’, ‘6’)
>>>A1
('P', 'Y', 'T', 'H', 'O', 'N')

>>>list1 = [ 10,20,30 ]
>>> A2 = tuple(list1)
>>> A2
(10,20,30)

Tuples are immutable:


A tuple is an immutable data type. It means that the contents of the tuple cannot be changed
after it has been created. An attempt to do this would lead to an error.
Example: Output:
mytuple=("Apple”,”Banana”,”Orange”)
("Apple”,”Banana”,”Orange”)
print(mytuple)
mytuple[3]=”Mango” TypeError: ‘tuple’ object does not support
item assignment.

Traversing a tuple:
A tuple is a iterable. This means that we can use a for loop to traverse all the elements in the
tuple sequentially.
Example Output
A = (10 ,20, ”Srikanth”, 39.99) 10
20
for item in A: Srikanth
print(item) 39.99

Indexing (Accessing Tuple Elements by using Index Number)


Each individual element in a tuple can be accessed using a technique called Indexing. The index
specifies the element to be accessed in the tuple and is written in square brackets ( [] ). The
index of the first element (from left) in the tuple is 0 and the last element is n-1 where n is the
length of the Tuple.

T1 = ( 5, 2, 9, 7, 5, 8, 1, 4, 3 )

Example: Output

T1 = ( 5, 2, 9, 7, 5, 8, 1, 4, 3 )
print ( T1[3] ) 7
print ( T1[7] ) 4

Example: Accessing elements in a tuple by index number Output


and using for loop

t1= (10,20,30,40) 10
20
for i in range(0, len(t1) ) :
30
print( t1[i] )
40
i=i+1

Negative Indexing:
The negative index starts from last element and denotes by –1 , negative indices are used when
we want to access the elements of the tuple from right to left. Starting from right hand side, the
first element has the index as –1 and the last element has the index of –n where n is the length
of the tuple.

Positive Indices 0 1 2 3 4
Tuple 10 20 30 40 50
Negative Indices -5 -4 -3 -2 -1
Example: Output:

t = (10,20,30,40,50)
#print each element using index
50
print( t[-1] )
40
print( t[-2] )
30
print( t[-3] )
20
print( t[-4] )
10
print( t[-5] )

Example: (accessing tuple elements by negative index Output:


number using for loop)

t =( 10 , 20, 30, 40 ) 40
30
for i in range(-1, -(len(t)+1), -1):
20
print ( t[i] )
10

Slicing
To access some part of a tuple, we use a method called slicing. The slicing operator [start :end]
is used for slicing. This can be done by specifying an index range, the slice operation tuple
[start : end] returns the part of the tuple starting from index start (inclusive) and ending at end
(exclusive). In other words, we can say that tuple[start :end] returns all the elements starting
from tuple[start] till tuple[end-1].

Positive Indices 0 1 2 3 4
Tuple 10 20 30 40 50

Example: Output
tuple =(10,20,30,40,50)

#This line of code will print complete tuple (10,20,30,40,50)


print(tuple[:])

#this line of code will print complete tuple


print(tuple[0:]) (10,20,30,40,50)
#This line of code will print tuple elements from 0th to 2nd Index
print(tuple[:3]) (10,20,30)

#This line of code will print tuple elements from 0th to 1st index
print(tuple[0:2])
(10,20)
#This line of code will print the elements from 1st to 2nd index
print(tuple[1:3])

#This line of code will print tuple elements from 1st to 4th index (20,30)
print(tuple[1:5])

(20,30,40,50)

Negative Slicing
We can also perform the slice operation on tuple using the negative index. The negative
indexing in tuple starts from the rightmost or last element present in the tuple. In the negative
slicing, we have to write negative index of tuple inside the slicing operator and between colon
operators to print the part of the tuple.
Tuple 10 20 30 40 50
Negative Indices -5 -4 -3 -2 -1

Example: Output

tuple = ( 10, 20, 30, 40, 50)

# Slice from –5 to end index –1. It will print full tuple


(10,20,30,40,50)
print ( tuple[-5 : 1] )

#Slice from beginning to –2 index (10,20,30,40)


print ( tuple[ : -1] )

#Slice from -4 to –2 index.


(20,30,40)
print ( tuple[-4 : -1])

#Slice from –5 to –3 index.


print ( tuple[-5 : -2] ) (10,20,30)

Specifying steps in slice operation:


In the slice operation, a third argument called step which is optional can be specified along with
the start and end index numbers. This step refers to the number of elements in the tuple that
can be skipped after the start indexing elements in the tuple. The default vale of the step is one.
In the previous slicing examples, step is not specified and in its absence, a default value of one
is used.
Positive Indices 0 1 2 3 4 5 6 7
Tuple 10 20 30 40 50 60 70 80
Negative Indices -8 -7 -6 -5 -4 -3 -2 -1

Examples: Output

tuple = (10, 20, 30, 40, 50, 60, 70, 80 )


#This line of code will print the full tuple
print ( tuple [ : : ] ) (10,20,30,40,50,60,70,80)

#Step 2 indicates to consider every 2nd element, prints from 0th element to
7th element
print ( tuple[ 0 : 8 : 2 ] ) (10,30,50,70)

#Step 3 indicates to consider, every 3rd element from beginning to end.


print ( tuple [ : : 3 ] )
(10,40,70)

#Step –1 indicates to the slicing will starts from the end of the
String with one step back each time. It is like reversing tuple elements.
print (tuple [ : : -1 ] )

(80,70,60,50,40,30,20,10)

Basic Tuple Operatios:


As we know that tuple is a sequence of elements .Python allows certain operation on tuple data
type, such as concatenation, repetition , membership, comparing . These operations are
explained in the following subsections with suitable examples.
Concatenation:

Concatenating two tuples means joining two types. We can concatenate two tuples using
concatenation operation plus which is denoted by symbol + .

Syntax: Tuple = tuple1 + tuple2 +.... + tuple n

We can use the concatenate operator in two followings ways:

1. Add two or more tuples and assign its values to a another tuple
2. Concatenate two or more tuples using printing output.
Example 1: concatenation using + operator Output

tuple1 = (10, 20, 30)


tuple2 = (40, 50, 60)
print(“Tuple1=”,tuple1)
tuple1=(10,20,30)
print(“Tuple2=”,tuple2)
tuple2=(40,50,60)
tuple3 = tuple1 + tuple2
concatenated Tuple=(10,20,30,40,50,60)
print(“Concatenated Tuple=”, tuple3)

Example 2: concatenation while printing Output

tuple1 = (10, 20, 30)


tuple2 = (40, 50, 60) tuple1=(10,20,30)
print(“Tuple1=”,tuple1)
tuple2=(40,50,60)
print(“Tuple2=”,tuple2)
concatenated Tuple=(10,20,30,40,50,60)
print(“Concatenated Tuple=”, tuple1+tupl2)

Repetition of tuples

Python allows us to repeat or replicate the elements of the tuple using repetition operator
which is denoted by symbol*. We can also assign the repetition value of a tuple to another
tuple.
Example: Output

tuple1=(10,20,30)

tuple2=tuple * 3
print(tuple2) (10,20,30,10,20,30,10,20,30)

Membership Operators (in and not in)

We can check the membership of an element of the tuple using the membership operator. The
membership operators are used to check if a particular element belongs to the tuple or not.
Membership operators are of two types:-

1. ‘in’ Membership Operator: The ‘in’ operator is a type of membership operator. It is


used to check if a particular element presents in tuple or not. The output given by ‘in’
operator is same of Boolean operator. It gives output as either true or false.
Syntax: element ‘in’ tuple

Example: Output

tuple = (10, 20, 30, ”python”, ”java”) True


print (20 in tuple)
print (40 in tuple) False

2. ‘not in’ Membership operator: The ‘not in’ operator is also a tuple of membership
operator. It is used to check if particular element is not present in the tuple. The output
given ‘not in’ operator is also same of Boolean operators. It gives output as either true
or false. The ‘not in’ operator always gives opposite output of ‘in’ operator i.e., If ‘not in’
gives False then ’in’ operator will gives true in output or vice-versa.
Syntax: element ‘not in’ tuple

Example: Output

tuple = (10, 20, 30, ”python”, ”java”) False


print (20 not in tuple)
print (40 not in tuple) True

3. Comparing tuples: A comparison operator also called as relational operator compares


the values of two operands and returns True or False.

Comparison Operator Examples


Less than(<) operator : It checks if the left value is lesser than on >>> a = (1,2,3,4)
the right. >>> b = (5,2,3,4)
>>> a<b
True

Greater than (>) operator : It checks whether the left value is >>> a = (1,2,3,4)
greater than that on the right >>> b = (5,2,3,4)
>>> a>b
False

Less than or Equal to (<=) Operator: This operator returns True only >>> a = (2,4,6,8)
when the value on the left is either less than or equal to that on the >>> b = (3,4,5,7)
right of the operator. >>> a<=b
True

Greater than or equal to (>=) operator: This operator returns True >>> a = (4,3,6,8)
only when the value on the right is either less than or equal to that >>> b = (2,5,4,3)
on the right of the operator. >>> a>=b
True
Equal to (==) operator: his operator return true, if the values on
either side of the operator are equal. >>> a = (2,3,4,6)
>>> b = (2,(3,4),6)
>>> c = (2,3,4,6)
>>> a==b
False
>>> a==c
True

Not equal (!=) operator: This operator return true, if the values on >>> a = (2,3,4,6)
the either side of the operator are not equal. >>> b = (2,(3,4),6)
True

4. Copying Tuple: To make a copy of the tuple is to assign a tuple to another tuple using
assignment operator (=).

Example Explanation
>>> tuple1 = (10 ,20, 30) The statement tuple2=tuple1 does not create a
>>> tuple2 = tuple1 new tuple. Rather, it just makes tuple1 and tuple2
>>>print (tuple2) refer to the same tuple object in the memory.
(10, 20, 30)
>>> print (tuple1) We can observe that the id of tuple1 and tuple2
(10, 20, 30) are same. There is only one tuple with two
>>> id (tuple1) references.
2343455465467
>>> id (tuple2)
2343455465467

Nested tuples : A tuple written inside another tuple is knows as a nested tuple.
Syntax:- Tuple_name = (value1, value2, (value3, value4, value5 ), value6, ...)

Example: Tuple=(10, 20, 30, 40, 50, 60, (100, 200, 300 ) )

The last element consisting of 3 elements written within parentheses is called a nested tuple as
it is inside another tuple.

Access elements of nested tuples by index


To access the element f the nested tuple of tuple1, we have to specify two indices tuple1[i] [j].
The first index i will take us to the desired nested tuple and second index j will take us to the
desired element in that nested tuple.
Example Output

L = ( 'a’, ’b’, ( ‘cc’, ’dd’, (‘eee’, ’fff’ ) ), ’g’, ’h’ )

print ( L[2] ) ( ‘cc’, ’dd’, (‘eee’, ’fff’ ) )

print ( L[2][2] ) ( ‘eee’, ’fff’ )

print ( L[2][2][0] ) eee

Negative indexing in a nested tuple:


We can access a nested tuple by negative indexing as well. Negative indexes count backward
from the end of the tuple. So, T[-1] refers to the last item, T[-2] is the second last item and so
on.

Example Output

L = ( 'a’, ’b’, ( ‘cc’, ’dd’, (‘eee’, ’fff’ ) ), ’g’, ’h’ )

print ( L [-3] ) ( ‘cc’, ’dd’, (‘eee’, ’fff’ ) )

print ( L [-3] [-1] ) ( ‘eee’, ’fff’ )

print ( L [-3] [-1] [-2] ) eee

Built-in functions used on tuples


Example Output

1. len(): This method is used to calculate the >>> tuple1 = ( 4, 3, 5, 2, 4, 4, 5 )


>>> len (tuple1)
total length of tuple. 7
Syntax: len(tuple_name)

2. sum(): This method is used to calculate the sum of all >>> t = (10, 20, 30, 40, 50 )
the elements in the tuple. >>> sum ( t )
The sum() method is used for only numeric values 150
otherwise it gives an error(TypeError).

Syntax: sum(tuple_name)

3.max(): It is used to return the maximum element in the >>> t = (34, 76, 89, 33, 54, 65 )
tuple. >>> max ( t )
Syntax : max(tuple_name) 89

>>> t = ( 't , ’e’, ’E’, ’u’, ’v’ )


>>> max ( t )
‘v’
It will return max value of character using ASCII
value.

>>> t = ( 34, 76, 89, 33, 54, 65 )


4.min(): It is used to return the minimum element in the >>> min ( t )
tuple. 34
Syntax: min(tuple_name) >>> t = ( 't’, ’e’, ’E’, ’u’, ’v’ )
>>> min ( t )
‘E’

5.any(): The any() function return true if any of the >>> t = (10, 20, 0, 0, -30 )
Boolean values in the tuple is true. >>> any ( t )
Syntax: any(tuple_name) True

>>> t = (0, 0, 0, 0, none )


>>> any ( t )
False

6.all(): The all() function returns true if all the Boolean >>> t = (10, 20, 0, 0, -30 )
values in the tuple are true, else returns false. >>> all ( t )
False
Syntax: all(tuple_name)
>>> t = (10, 20, 30, 40, ”Python”, 3.99 )
>>> all ( t )
True

7.Sorted(): The sorted() function returns a sorted list of >>> t1 = (20, 30, 40, 10, 50 )
>>> t2 = sorted ( t1 )
the specified iterable object. >>> print ( t2 )
[ 10, 20, 30, 40, 50 ]
To charge the sort order, we need to use “reverse= True” >>> t2 = sorted ( t1, reverse=True )
option with sorted() function. >>> print ( t2)
[ 50, 40, 30, 20, 10 ]

Tuple Methods:
Example Output

1. count(): The count() method returns the number of >>> t = (4, 3, 5, 2, 4, 6, 5, 4, 5 )


times the specified element appears in the tuple. >>> t.count ( 5 )
3
Syntax: tuple_name.count(element) >>> t.count ( 9 )
0

2. index(): The index() method searches for the given >>>t = (3, 5, 2, 6, ’hello’, 5 )
>>> t.index(5)
element from the start of the list and returns its index. 1
if the value appears more than once, it will return the >>> t.index(‘hello’)
index of the first one. If the item is not present in the 4
>>> t.index (9)
list then ValueError is thrown by this method. ValueError :tuple.index(x) : x not in tuple

Syntax: list.index(element)

Using zip() Function:


An iterable is any python object capable of returning its members one at a time, permitting it to
be iterated over in a for-loop. Objects like list, tuple, sets, dictionaries, strings ,etc. are called
iterables.

The Zip() method in python takes one or more iterable( list,set,stringsetc) as arguments and
merges each of them element-wise to create a single iterable. The return value is a zip object,
which is also on iterable and consists of tuples.

This means that the first elements of all the input iterables are zipped together to the second
element of all the input iterables and so on.

The zip method maps the same index of multiple input iterables and converts them into a tuple.
The final zip objects can contain all the mapped tuples in the same order.
Syntax: zip ( iterable1, iterable2, iterable3,...)

 If we do not pass any parameter ,zip() returns an empty iterator.


 If a single iterable is passed, zip() returns an iterator of tuples with each tuple having
only one element.
 If multiple iterables are passed, zip()returns an iterator of tuples with each tuple having
elements from all the iterables.
Example Output

#no iterables passed


result = zip()
print (tuple (result ) )
()
#defines three list of different sizes

list1 = [10, 30, 50 ]


list2 = [20, 30, 60, 70 ]
list3 = ['A’, ’B’, ’C’, ’D’, ’E’ ]
#pass only List1 to rip method
result = zip( list1)
print ( tuple( result )) ( (10, ), (30, ), (50, ) )

#pass List1 and List2 to zip method


result = zip (list1, list2)
print (tuple (result ))
( (10, 20), (30, 40), (50, 60) )
#pass List1,List2,List3 to zip method
result = ziplList1, list2, list3)
print (tuple(result)) ( (10, 20, ’A’), (30 ,40 ,’B’), (50, 60, ’C’) )

Unzipping
There is no special function to unzip the zipped iterables, we can use the zip() method with the
* operator to unzip a zip object.
Example : Output

states = ["Karnataka”, ”Orissa”, ”Bihar”, ”Rajasthan”]

capitals = ["Bengaluru”, ”Bhubaneswar”, ”Patna”,


”Jaipur”]

state_capital = tuple(zip ( states, capitals ) )

print (state_capital)
( (‘Karnataka’,’Benagluru’), (‘Orissa’,’Bhubaneswar’),
states_unzipped, capitals_unzipped = (‘Bihar’,‘Patna’), (‘Rajasthan’,’Jaipur’))
zip(*state_capital)
States are : (‘Karnataka’, ’ Orissa’, ‘Bihar’, ‘Rajasthan’)
print (“States are :”, states_unzipped)

Capitals are: (‘Benagluru’, ’Bhubaneswar’, ’Patna’, ‘Jaipur’)

print( Capitals are :”, captials_unzipped)

Tuples Packing and Unpacking


Tuple packing refers to assigning multiple values into a tuple. Tuple unpacking refers to
assigning a tuple into multiple variables. Tuple unpacking is also called as tuple Assignment.
Example Output

person=("Snigdha”,25,”India”,”Karnataka”)
name, age, country, state = person
print(“Name :”, name) Name : Snigdha
print(“Age :”, age) Age : 25
print(“Country :”, country) Country : India
print(“State :”, state) State : Karnataka

Sometimes assigning every item from tuple to individual variable is not required. In those cases,
we can ignore them by using underscore (_).

Example: If we want to unpack only name and country only then code is :

name, _, country, _ = person

Extended Unpacking: Sometimes it is required to unpack some specific items by skipping some
items from a tuple. In those cases, writing underscores (_) for every skipped item is not so
helpful. In such cases extended unpacking is very useful. It can done by using * as shown below:

name, *_, state = person

The del Statement:


The del keyword is used to delete objects. Python do not support deleting a specific element
in a tuple because it immutable.
Syntax: del tuple_name

Example Output
t = (10, 20, 30 )
print(t) (10, 20, 30)
del t
print(t) NameError: name ‘t’ is not defined

Converting tuple to list and list to tuple


In some situations, we may need to modify or change the contents of a tuple and in such
situations the list can be helpful.

 The tuple can be converted to list using list() function by passing tuple as an argument.
 Once it is converted to the list, the contents in the list can be modified or altered.
 Finally, we can convert the updated list into tuple by using tuple() function by passing
the updated list as an argument
Example Output
#create a tuple
languages = ("Python”, ”Java”, ”PHP”)
#print tuple contents Tuple Items: (‘Python’, ’Java’, ’PHP’)
print (“Tuple Items :”, Languages)

#convert tuple to list


languageList = list ( languages )
#print list contents
print (“List item :”, languageList)
List Items: [‘Python’, ’Java’, ’PHP’]
#update list
languageList[0] = ”C”
#print list contents
print ( List Items :”, languageList )

#convert list to tuple List Items: [‘C’, ’Java’, ’PHP’]


languages = tuple ( languageList)
#print tuple contents
print (“Tuple Items :”, languages)
Tuple Items (‘C’, ’Java’, ’PHP’)

A List as an element in tuple


Tuple can have list, strings, dictionary as an item inside a tuple. If an item within a tuple is
mutable, then we can change it. Consider the presence of a list as an item in a tuple, then any
changes to the list gets reflected on the overall item in the tuple.
Example Output
#create a list
Mylist=["one”, ”two”, ”three”]
#print list contents
Print(“List Items : ”, Mylist) List Items : [‘one’,’two’,’three’]

#create a tuple with list as an item in it


Mytuple=(10,20,30,Mylist)
#print tuple contents
print(“Tuple Items : ”, Mytuple) Tuple Items : (10,20,30, [‘one’,’two’,’three’] )

#update list by adding new item


Mylist.append(“four”)
#print list contents
print(“Update List Item : ”, Mylist) Update List Items : [‘one’,’two’,’three’,’four’]

#print tuple contents


print(“Update list in Tuple : ”, Mytuple ) Update list in Tuple: ( 10, 20, 30, [‘one’, ’two’, ’three’, ’four’] )

Difference between List and Tuple


List Tuple
List is a group of comma-separated values within Tuple is a group of comma-separated values within
square brackets are mandatory. parenthesis ()

Example : List= [10,20,30,40] Example :Tuple=(10,20,30,40)


Lists are mutable. It can be altered or changed after its Tuple is immutable. It cannot be altered or changed
creation. after its creation.

Lists have several built-in methods. A tuple does not have many built in methods because
it is immutable.

Lists are better for insertion and deletion operations. Tuple are appropriate for accessing the elements.

An unexpected change or error is more likely to occur In a tuple, changes or error don't usually occur
in a list. because of immutability.

List objects cannot be used as keys for dictionaries Tuple objects can be used as keys for dictionaries
because keys should be immutable. because keys should be mutable.

List consumes more memory than tuples. As lists are Tuples consume less memory than list. As tuples are
mutable, python needs to allocate an extra memory immutable and of a fixed size, python allocates only
block in case there is need to extend the size of the list the minimum memory block required for the data.
objects after we create it.
List can be copied. Tuples cannot be copied

Relation between tuple and dictionaries:

1. Tuples can be key:value pairs to built dictionaries:-


The tuples can be converted to dictionaries by passing the tuple name to the dict()
function. This is achieved by nesting tuples within tuples, wherein each nested tuple
item should have two items in it. The first item becomes the key and the second item as
its values when the tuple gets converted to a dictionary.
Example Output

# create a tuple
T1=( (1, “one”), (2,”two”), (3,”three”) )

#create a dictionary using dict()


D1=dict (T1)

#print dictionary contents


print(“D1 Contents :\n”, D1)
D1 Contents: {1: ‘one’, 2: ‘two’, 3: ‘three’}

Difference between Tuple and Dictionary


TUPLE DICTIONARY
Tuple is a group of comma-separated values within A dictionary is a collection of data that stores data in
parenthesis () and parenthesis optional. key:value pair.

Example : T=(10,20,30) Example : Mydict={1:’one”,2:”two”}

Tuple is an ordered collection of elements. Dictionary is unordered collection of elements.


Tuples are enclosed within parenthesis() Dictionary are enclosed in curly brackets
{} in the form of key-value pairs.

Tuples are immutable. It cannot be altered or changed Dictionaries are mutable because we can add, delete,
after its creation. and changed the data values even after its creation.

Tuples allow duplicate values Dictionary keys do not allow duplicates. The values can
have duplicates.

An element cannot be added to the tuple as it is The update() method updates the dictionary with the
immutable. specified key-value pairs.

Tuple can be created using tuple() function. Dictionary can be created using dict() function.

The tuple element can be accessed by using index Dictionary works on key based indexing i.e. key
number. The index number starts from 0. identify the values.

Negative indexing and slicing is possible. No concept of negative indexing and slicing in
dictionary.

You might also like