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
"""tuples in python:
the lists are mutable, Python needs to allocate an extra memory block
in case there is a need to extend the size of the list object after it is created.
In contrary, as tuples are immutable and fixed size, Python allocates
just the minimum memory block required for the data.
As a result, tuples are more memory efficient than the lists."""
import sys, platform, time
a_list = list()
a_tuple = tuple()
a_list = [1,2,3,4,5]
a_tuple = (1,2,3,4,5)
print(sys.getsizeof(a_list))
print(sys.getsizeof(a_tuple))
start_time = time.time()
b_list = list(range(10000000))
end_time = time.time()
print("Instantiation time for LIST:", end_time - start_time)
start_time = time.time()
b_tuple = tuple(range(10000000))
end_time = time.time()
print("Instantiation time for TUPLE:", end_time - start_time)
start_time = time.time()
for item in b_list:
aa = b_list[20000]
end_time = time.time()
print("Lookup time for LIST: ", end_time - start_time)
start_time = time.time()
for item in b_tuple:
aa = b_tuple[20000]
end_time = time.time()
print("Lookup time for TUPLE: ", end_time - start_time)
"""As you can see from the output of the above code snippet,
the memory required for the identical list and tuple objects is different.
When it comes to the time efficiency, again tuples have a slight advantage
over the lists especially when lookup to a value is considered."""
"""When to use Tuples over Lists:
Well, obviously this depends on your needs.
There may be some occasions you specifically do not what data to be changed.
If you have data which is not meant to be changed in the first place,
you should choose tuple data type over lists.
But if you know that the data will grow and shrink during the runtime of the application,
you need to go with the list data type."""
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
#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
xxxxxxxxxx
# Creating a Tuple with
# the use of Strings
Tuple = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
Tuple = tuple(list1)
# Accessing element using indexing
print("First element of tuple")
print(Tuple[0])
# Accessing element from last
# negative indexing
print("\nLast element of tuple")
print(Tuple[-1])
print("\nThird last element of tuple")
print(Tuple[-3])
xxxxxxxxxx
# Membership test in tuple
my_tuple = ('a', 'p', 'p', 'l', 'e',)
# In operation
print('a' in my_tuple)
print('b' in my_tuple)
# Not in operation
print('g' not in my_tuple)
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
# 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
name_of_students = ("Jim" , "yeasin" , "Arafat")
print(name_of_students.index('Arafat'))