0% found this document useful (0 votes)
5 views3 pages

TupleDemo in Python

The document explains the differences between tuples and lists in Python, highlighting that tuples are immutable and cannot be modified, while lists are mutable. It provides examples of creating, accessing, and manipulating tuples, including converting tuples to lists for modification. Additionally, it covers checking for item presence, tuple length, copying, combining tuples, and comparing tuples and lists.

Uploaded by

sridhar.tsrt
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)
5 views3 pages

TupleDemo in Python

The document explains the differences between tuples and lists in Python, highlighting that tuples are immutable and cannot be modified, while lists are mutable. It provides examples of creating, accessing, and manipulating tuples, including converting tuples to lists for modification. Additionally, it covers checking for item presence, tuple length, copying, combining tuples, and comparing tuples and lists.

Uploaded by

sridhar.tsrt
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

# tuple has () whereas List has [] ----IMP

#tuple stores multiple values of different type


# tuple() is ordered and unchangeable i.e immutable
# immutable means
# we cannot insert a new value
# we cannot append a value
# we cannot remove a value
# we cannot modify an existing value

#List[] is ordered and changeable i.e mutable

#Example 1: creating a tuple


#mytuple=("Apple","banana","cherry")
#print(mytuple)

#Example2 Accessing Tuple items


# mytuple=("Apple","banana","cherry")
# print(mytuple[1]) # banana #1 is the index and index starts from 0 ,has []
#
# mytuple=() # This is an empty tuple

#Example3: Accessing tuple items using index


# mytuple=("Apple","banana","cherry")
# print(mytuple[-1],mytuple[-2]) #cherry banana

# Example 4: Accessing tuple items through Range of index


# mytuple = ("Apple","banana","cherry","orange","Mango","Grapes","melon")
# print(mytuple[2:5]) #('cherry', 'orange', 'Mango') range goes upto n-1 only
# print(mytuple[-4:-1])

#Example 5: To change tuple items


# By default, tuple doesnot allow to modify items bcz it is IMMUTABLE
#but,we can convert a tuple to list,change the items and
# convert the List back to tuple
# mytuple=("Apple","banana","cherry")
# mylist = list(mytuple)
# print(mylist) #['Apple', 'banana', 'cherry']
# mylist[0] = "orange"
# print(mylist) #['orange', 'banana', 'cherry']
# mytuple = tuple(mylist)
# print(mytuple) #('orange', 'banana', 'cherry')

# Example5: Accessing items of tuple using for loop


# mytuple = ("Apple","banana","cherry","orange","Mango","Grapes","melon")
# for i in mytuple:
# print(i)
#Example 6: To check whether an item is present in a tuple or not
# mytuple = ("Apple","banana","cherry","orange","Mango","Grapes","melon")
#
# if "cherry" in mytuple:
# print("Yes ,cherry is present in tuple")
# else:
# print("cherry is not present")

#Example7 : length of tuple


# mytuple = ("Apple","banana","cherry","orange","Mango","Grapes","melon")
# print(len(mytuple)) #7

# Example8: Add items to tuple---NOT POSSIBLE--- BCZ TUPLE IS IMMUTABLE


#but can convert to a list,change,again convert back to tuple

#Example 9: copy a tuple


# mytuple1 = ("red","green","yellow")
# mytuple2 = mytuple1
# print(mytuple2) #("red","green","yellow")

#Example 10: Removing items from tuple ---- Not possible---bcz IMMUTABLE

#Example11: join/combine 2 tuples


# mytuple1 = ("a","b","c")
# mytuple2 = (1,2,3,4)
# mytuple3 = mytuple1 + mytuple2
# print(mytuple3) ('a', 'b', 'c', 1, 2, 3, 4)

#Example12: Comparing two Tuples/Lists


# mytuple1 = (10,20,30)
# mytuple2 = ("abc","def","ghi")
# mytuple3 = (10,20,30)
# if mytuple1 == mytuple2:
# print("yes, two tuples are equal")
# else:
# print("two tuples are not equal")
# #-------
# if mytuple1 == mytuple3:
# print("yes, two tuples are equal")
# else:
# print("two tuples are not equal")
# #--------

# mylist1 = [10,20,30]
# mylist2 = ["A","b","c"]
# mylist3 = [10,20,30]
# if mylist1 == mylist2:
# print("yes, two lists are equal")
# else:
# print("two lists are not equal")
# #-------
# if mylist1 == mylist3:
# print("yes, two lists are equal")
# else:
# print("two lists are not equal")
#---------------END-----------

You might also like