0% found this document useful (0 votes)
23 views13 pages

Unit 2 (Second)

Tuples are an immutable data structure in Python used to store multiple items in a single variable, allowing for ordered collections with duplicate values. They can contain any data type and can be created from existing sequences or through input functions. Various operations can be performed on tuples, including slicing, concatenation, and using built-in functions like len(), max(), min(), and sum().
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)
23 views13 pages

Unit 2 (Second)

Tuples are an immutable data structure in Python used to store multiple items in a single variable, allowing for ordered collections with duplicate values. They can contain any data type and can be created from existing sequences or through input functions. Various operations can be performed on tuples, including slicing, concatenation, and using built-in functions like len(), max(), min(), and sum().
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/ 13

Tuples

Tuples
• Tuples is a data structure which is used to store multiple
items in a single variable.
• A tuple is a collection which is ordered (it means that the
items have a defined order, and that order will not
change.),
• unchangeable or immutable and
• allow duplicate values.
• 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.
• Eg : thistuple = ("apple", "banana", "cherry")
• print(thistuple)
• #Duplicate Items
• thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
• To 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.
• Eg :
– thistuple = ("apple“,)
– print(type(thistuple))
• Tuple items can be of any data type.
• Eg :
– tuple1 = ("apple", "banana", "cherry")
– print(type(tuple1))
– tuple2 = (1, 5, 7, 9, 3)
– print(type(tuple2))
– tuple3 = (True, False, False)
– print(type(tuple3))
Operations
• Adding Elements to a Tuple
– the data stored in a tuple cannot be edited, but it's possible
to add more data to a tuple. This can be done using
the addition operator.
– The ‘+’ can be used to combine two tuples.
• Eg :
– t = (1, 2, 3, 4, 5)
– t = t + (7,8,9,10)
– print(t)
• Eg :
– t1=(1, 2, 5, 8)
– t2= (2, 9, 4)
– t3=t1+t2
– t3
• Deleting a Tuple:
– to delete a tuple, the del keyword is used.
• Eg :
– del (t1)
• Slicing in Tuples:
– Slicing in tuples, works exactly the same like in the case of lists.
Slicing can be done backwards as well using the negative indexes
for traversing the tuple from backward direction.
• Eg :
– t = (1, 2, 3, 4)
– print(t[-2:4])
• in keyword
– It is used to check, if any element is present in the sequence or
not.
• Eg :
– t = (1, 2, 3, 6, 7, 8)
– 2 in t
– #5 in t
• len() function
– this function is used to get the number of elements
inside any tuple.
• Eg :
– t = 1, 2, 3,"hello",253.201,78,"msi"
– print (len(t))
• max() and min() function
– To find the maximum value in a tuple, we can use
the max() function, and for finding the minimum value
use min() function.
• Eg :
– t = (1, 4, 2, 7, 3, 9)
– print (max(t))
– print (min(t))
• Change mutable item in a tuple:
• Eg :
– T=(1,[2,3,4,5],6)
– T[1][1]=33
– Print(t)
– O/P : (1,[2,33,4,5],6)
• Index : to find the index of element, by default
start with 0. if we set the index position then write
the position.(i.e. where we have to start the
search)
• Eg :
– T=(11,12,13,14,13,15,16,17,13,18,19,13)
– i=t.index(13)
– i=t.index(13,3)
– Print(i)
• Search in range:
• Eg:
– T=(11,12,13,14,13,15,16,17,13,18,19,13)
– i=t.index(13,3,7)
– Print(i)
• Create Tuples from existing Sequences:
– Use built in type object(tuple) to create tuples from sequences.
– Syntax : T=tuple(sequences)
– Where sequences can be any kind of object like string,list,tuples etc.
– Eg :
• t1=tuple("hello")
• T1
– Eg :
• l=['w','e','r','t','y']
• t=tuple(l)
• print(t)

• To create tuple from input function:


– Using this method we create tuples of single characters or single digits
via input function.
– Eg :
• # create tuple with input
• t1=tuple(input("enter tuple element"))
– By using eval(input) method create the tuple.
– Eg :
• t=eval(input("enter tuple to be added"))
• print("tuple you entered is: ",t)
• Traversing a tuple: means accessing and processing each element of it. The for loop
makes it easy to traverse the items in a tuple.
• Syntax : for<item> in <tuple>:
• my_tuple = (1, 2, 3, 4, 5)
• for item in my_tuple:
• print(item)
• Eg :
• my_tuple = (1, 2, 3, 4, 5)
• for item in range(len(my_tuple)):
• print(my_tuple[item])

• Joining tuples : the + operator used with two tuples.


• Eg :
– t1 = ("a", "b", "c")
– t2=(1, 2, 3, 4, 5)
– t1+t2
• #concatenate tuple with comma(,)
– t2=(1, 2, 3, 4, 5)
– t2+(10,)
• Slicing the tuples :
– Tuples slices like list or string are the sub part of the tuple extracted out.
• Eg :
– t=(10,12,14,16,18,20,22,24,26,28)
– seq=t[3:-3]
– seq
• ## print tuples values using slicing using for
• x=(101,102,103,104,105,106,107)
• print("from first pos. to 4 pos")
• a=x[1:5]
• for i in a:
• print(i)
• # print last 4 elements
• # print 0 pos to last position use step value 2

• # print last 5 elements with [-5-(-3)]=2 elements towards right


• x=(101,102,103,104,105,106,107)
• f=x[-5:-3]
• for i in f:
• print(i)
• The tuple() Function
• We can use the tuple() constructor or function to create a tuple. It basically performs
two functions as follows:
• Creating an empty tuple if we give no arguments.
• Creating a tuple with elements if we pass the arguments.

>>>tup = tuple ((22, 45, 23, 78, 6.89))
– >>>tup
– (22, 45, 23, 78, 6.89)
– >>> tup2 = tuple()
– >>> tup2 ()
• len() Function
• This function returns the number of elements present in a tuple
– >>>tup = tuple ((22, 45, 23, 78, 6.89))
– >>>len(tup )

• count() Function
• This function help us to find the number of times an element is present in the tuple.
• tup = (22, 45, 23, 78, 22, 22, 6.89) >>> tup.count(22)

• sorted() Function
– This method takes a tuple as an input and returns a sorted
list as an output. it does not make any changes to the
original tuple.
– tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
– >>> sorted(tup)
• min(), max(), and sum() Tuple Functions
– min(): gives the smallest element in the tuple as an output.
– max(): gives the largest element in the tuple as an output.
– sum(): gives the sum of the elements present in the tuple
as an output.

– Eg :
• tup = (22, 3, 45, 4, 2, 56, 890, 1)
• >>> sum(tup)

You might also like