### Week 8: Tuples,Set and Dictionary Data types In Python. Heap As A Data Structure/Memory Location And Stack As Memory Location. Passing Values By Value and By Reference. > By *Akinlade Temitope Victory* After being introduced to list as a data type in python lask week, we moved forward to other data types this week starting with tuples. #### Tuples This is a data type that collects and store data which basically by default cannot be changed or updated as this particilar data type is not mutable like a list, it is immutable. An example of a tuple is below: ```python= bio = ("Temitope",25,"O+",[4,7],("A","B"),"Temitope") ``` The above is an example of a tuple and as you can see, it takes in different data types even with similar values. For now, the only way we were able to make modification to a tuple was to change it's type to a mutable using the constructor method and making modification then changing it back to a tuple with the tuple constructor method An example of this is below: ```python= bio = ("Temitope",25,"O+",[4,7],("A","B"),"Temitope") bio = list(bio) bio.append("Akinlade") bio = tuple(bio) print(bio) # Output >>> bio = ('Temitope',25,'O+'',[4,7],('A','B'),'Temitope','Akinlade') ``` > Note: Values in a tuple are wrapped in parentheses and declaring a tuple that takes only 1 value require a comma afterwards like this ('Temitope',) else it will treated as a string #### Set Sets are unordered collections of unique values with no duplicates of same value and also accomodate other data types and are decleared with curly braces{}. Declaring an empty set without value requires the set constructor e.g set() else it will be treated as another data type. An example of a set is below: ```python= days_of_the_week = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"} print(days_of_the_week) # Output >>>> {,'Monday','Sunday','Tuesday','Wednesday','Thursday','Friday','Saturday'} ``` From the above output, you can notice the duplicate "Sunday" was ignored as the value is already in the set and another thing to notice is the change in order of arrangement, you cannot access values in set using indexing or slicing as it is unordered and values switch places at execution. You can carry out other operations on a set like adding a value, removing a values safely and more safely, clearing the values of a set e.tc Examples are: ```python empty_set = set() # How to declare a set with no values s = {1,2,3} s.add(4) print(s) # Output >>>> {1,2,3,4} (order not guaranteed) s.remove(2) # Removes 2 # s.remove(10) # Throws an error if 10 is not in set s.discard(10) # Does nothing if 10 is not there item = s.pop() print(item) # Removes and returns a random value from the set print(s) # Output >>>> 1 item less s.clear() print(s) # Output >>>> set() ``` #### Dictionary This is an ordered data type like list and tuple that takes in data with key and value in pairs. An example of a dictionary is: ```python details = {"First Name":"Temitope","Age":25,"Married?":false,"Hobbies":(games,music),{"Height":5.7}} ``` From the above, you can see that it is able to take in different data types including another dictionary and you can access values here from their index as it is ordered. There are different methods used to modify a dictionary and few of them are: ```python details.update({"Last Name": "Akinlade"}) # To add a value to a dictionary print(details) # Output >>>> {'Name':'Temitope','Age':25,'Married?'':false,'Hobbies':(games,music),{"Height":5.7},{"Last Name": "Akinlade"}} details.pop("Last Name") # Removes value of the key which will be "Akinlade" ``` #### Heap We were asked to read up about Heap as a data structure/memory location and stack as memory location. Passing values by value and by reference. ##### AS A MEMORY LOCATION Starting with heap, from my findings, it is a region in computer memory where dynamic data are stored at runtime(at execution) e.g list,objects e.t.c ##### AS A DATA STRUCTURE A heap as a data structure is a special kind of binary tree used to keep track of the smallest or largest value quickly. #### Stack Stack is a a memory area where data are stored in last in, first out order. it is used for function calls,local variables e.t.c #### Passing values by value This is a way of assigning a value in a variable to another but with different memory locations and an example is below: ```python a = ["Temitope","Akinlade"] b = a.copy() print(a) # Output >>>> Temitope print(b) # Output >>>> Temitope print(id(a)) print(id(b)) # Output >>>> Both will print out different memory location identification number and even if one gets modified,it won't affect the other ``` #### Passing values by reference This is a way assigning a value of one variable to another while sharing the same memory location identification number hereby referencing the same value and an example is: ```python a = ["Temitope","Akinlade"] b = a print(a) print(b) print (id(a)) print(id(b)) # Output >>>> Same value and same memory location identification number and any modificaton here reflects on both address. ``` ### CONCLUSION It was thrilling working with these data types and also having a glimpse to what happens behind the scene in regards to how collected dynamic and automatic data are stored in computer memory. Thanks for sticking around this far and see you soon!.