Tuple
Tuple is an ordered and immutable collection data structure. Ordered meaning it keeps the order of elements in which they are inserted, and immutable means that once the tuple is created it cannot change, and you cannot add or remove elements from it.
Lesson Overview
In this lesson we will cover:
- How to create tuple
- Accessing the elements in tuple
- Useful methods on tuples
Creating Tuple
A tuple is created by enclosing items in parentheses () separated by comma.
Tuples containing a single element still must contain a comma, otherwise python will read the wrong data type:
Accessing Elements
Accessing elements of the tuple is done with index, just like lists.
Useful methods
Unlike lists, as we cannot modify tuples, we don't have a lot of methods available to us, but we will list a few.
count()- returns the number of times a specified value appears in tuple.Python Sandboxindex()- returns index of the first occurrence of the element.Python Sandbox
Deep dive into Python tuples in this Deep Dive: tuples article by Real Python which takes you deep in topic of tuples and their usecases.
Exercise
Complete [TODO] Exercise 06 — WorkingTitle to practice tuples.
Assignment
Using lists to separatly hold multiple related values is brittle and will break if you do not pay attention to all lists containing data. In this assigment we will deal with those issues by using tuples.
- Open our
main.pyfile we have been working on - Create a new empty list called
inventory. This will hold tuples of related data(product_name, product_price, product_stock). - Delete previusly defined
item_name,item_priceanditem_stockvariables, we wont need them anymore. - Just like before, in infinite loop, first check if the input is an empty string and if it is, break out of the loop, otherwise get the users input containing name, price and stock in a single line, then split the line using
split()method to get a list of inputed data. - Instead of appending each data to their own list, create a tuple called
itemcontainingname,priceandstockin that particular order and append that to ourinventorylist we declared earlier. Print out eachinventoryelement usingforloop. - On to customer ordering items now. Keep
orderlist and instead of storing only totals for each item, we will now storeitem_name,item_quantityandline_totalin tuple calledline_itemand append that to ourorderlist so our shop can better keep track of the orders. Keep in mind - tuples are immutable which means that you will have to reducestockof the item by replacing entire tuple with newstockvalue. - Same as before, calculate the
totalfor all the items bought, apply a 10% discount if thetotalis over 100. - In the end, print out the inventory, to make sure that the items are actually removed once bought.
- Make sure your program runs, then commit the changes and push to Github
What's Next
Now that you have seen data structures that can contain duplicates, it's time to meet set - a unique data structure that does not allows for duplicates.