xxxxxxxxxx
#Accessing Tuple
#with Indexing
Tuple1 = tuple("Softhunt")
print("\nFirst element of Tuple: ")
print(Tuple1[1])
#Tuple unpacking
Tuple1 = ("Python", "Softhunt", "Tutorials")
#This line unpack
#values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)
xxxxxxxxxx
#Tuples are ordered, indexed collections of data
#Tuples can store duplicate values
#You cannot change the data of a tuple after that you have assigned it the values
#Like lists, it can also store several data items
xxxxxxxxxx
# plz suscribe to my youtube channel -->
# https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A
fruits = ("apple", "banana", "cherry")
print(fruits[1])
xxxxxxxxxx
# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print(Tuple1)
# Creating a Tuple
# with the use of string
Tuple1 = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple1)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
# Creating a Tuple
# with the use of built-in function
Tuple1 = tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)
xxxxxxxxxx
strs = ['ccc', 'aaaa', 'd', 'bb'] print sorted(strs, key=len) ## ['d', 'bb', 'ccc', 'aaaa']
#the list will be sorted by the length of each argument
xxxxxxxxxx
# Tuples are immutable data structures in Python, meaning they cannot be modified once created.
# They are ordered and can contain elements of different types.
# Creating a tuple
my_tuple = (1, 2, "Hello", 3.14)
# Accessing elements of a tuple
print(my_tuple[0]) # Output: 1
# Getting the length of a tuple
print(len(my_tuple)) # Output: 4
# Iterating over a tuple
for item in my_tuple:
print(item) # Output: 1, 2, "Hello", 3.14
# Concatenating tuples
new_tuple = my_tuple + (5, 6)
print(new_tuple) # Output: (1, 2, "Hello", 3.14, 5, 6)
# Tuple packing and unpacking
a = 1
b = "Python"
c = 3.14
my_packed_tuple = a, b, c # Packing variables into a tuple
print(my_packed_tuple) # Output: (1, "Python", 3.14)
x, y, z = my_packed_tuple # Unpacking tuple into variables
print(x, y, z) # Output: 1, "Python", 3.14
xxxxxxxxxx
#A tuple is essentailly a list with limited uses. They are popular when making variables
#or containers that you don't want changed, or when making temporary variables.
#A tuple is defined with parentheses.
xxxxxxxxxx
#!/usr/bin/python
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0];
print "tup2[1:5]: ", tup2[1:5];