"""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."""
#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
#tuple unpacking
myTuple = ("Tim","9","Smith")
#tuple unpacking allows us to store each tuple element in a variable
#syntax - vars = tuple
"""NOTE: no of vars must equal no of elements in tuple"""
name,age,sirname = myTuple
print(name)
print(age)
print(sirname)
#extra
#this alows us to easily switch values of variables
name,sirname = sirname,name
print(name)
print(sirname)
# Creating a Tuple with
# the use of Strings
Tuple = ('Geeks', 'For')
print("
Tuple with the use of String: ")
print(Tuple)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("
Tuple using List: ")
Tuple = tuple(list1)
# Accessing element using indexing
print("First element of tuple")
print(Tuple[0])
# Accessing element from last
# negative indexing
print("
Last element of tuple")
print(Tuple[-1])
print("
Third last element of tuple")
print(Tuple[-3])
# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print(Tuple1)
# Creating a Tuple
# with the use of string
Tuple1 = ('Geeks', 'For')
print("
Tuple with the use of String: ")
print(Tuple1)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("
Tuple using List: ")
print(tuple(list1))
# Creating a Tuple
# with the use of built-in function
Tuple1 = tuple('Geeks')
print("
Tuple with the use of function: ")
print(Tuple1)
#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.