Python Collections by D@CC on 2024HAug20 Python has four types of Collections For any questions refer to W3 School Pyton on the web. Tuple A Tuple is ORDERED and UNCHANGEABLE and allow duplicates Items can be any data type Multiple items in a single tuple can be many different data types. A Tuple is written with round brackets "(" and ")" eg fruits fruits = ("apple", "banana", "cherry") Tuple items are indexed. The first item has index [0], next has index [1] etc A negative index number starts from the end -1 is the last List A List is a Tuple that can be changed. A List is written with square brackets ("[" and "]") Items added to a list are placed at the end of the list. Set is a collection which is UNORDERED, UNCHANGEABLE*, and unindexed. No duplicate members. *Set items are unchangeable, but you can remove and/or add items whenever you like. Set items cannot be identified by an index number Sets are written with curly brackets Sets cannot be referred to by index or key Rules for Sets are the same as in set theory True and 1 are considered the SAME value in a set. If a Set is defined to include duplicate values, the duplicate values are not retained. Dictionary is a collection which is ordered** and changeable. No duplicate members. A Dictionary is written with curly brackets Each dictionary contains key:value pairs Duplicate values will overwrite existing values. **As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print thisdict["brand"] will print "Ford" thisdictionaryIndex = { "000":"Cole", "001":"Marcoux", "003":"Smith" "001":"Anderson", } print thisDictionaryIndex["000"] will print "Cole" What will be printed by print thisDictionaryIndex["001"] or print thisDictionaryIndex or print thisDictionaryIndex[1] Dictionaries are often used to house a complete index. Two duplicate pairs are NOT retained, but two duplicate keys can occur. EXERCISES Exercise 01 Read in a serial data file containing records with an index. Each record is of the format 00001,string\n the two fields are separated by a comma the first field is a 5 digit decimal number padded with zeroes in front store the file in a Python dictionary name Dict store the first field as a 5 digit string store the second field as a string of characters, that may be null The last record is empty containing only a "\n" line feed Once the file has been read, print out the Python dictionary: Dict Then print out the indexed entried in the Python dictionary for i in len(Dict) print(Dict[i]) #for end Store each record of the data file as a pair in a Python dictionary. Exercise 02 Try with a file with 2 duplicate records Exercise 03 Try with a file with records that are not ordered by the key Exercise 04 Try with a file with duplicate keys and different strings Exercise 05 Try with a file with different keys and duplicate strings. Exercise 06 Load the preceding file into a Set (by ignoring the "key" field) Exercise 07 Load the precedinf file into a List (by ignoring the "key" field) Exercise 08 Add error checking to ensure that the serial file: is not devoid of any records (ie empty) has at least one comma in every record every key is 5 digits long a comma exists in every column 6 does not contain a null record (just a "\n") the file exists in the current directory does not contain a record longer than 87 characters (not including the line feed) file does not contain more than 5 records. has no keys that are not in ascending order has no strings that are not in ascending order has no non-digits in a key has no strings that are not 80 characters long Exercise 09 For each type of collection load items "apple", "orange", "banana" for i in len(Coll) print(Coll[i]) #for end Exercise 10 Load a Tuple into a List then print it out Load a Tuple into a Set, then print it out Load a Tuple into a Dictionary with keys starting with 0 etc, then print it out Exercis 11 Add an item to an existing List, then print Add an item to an existing Set, then print Add an item pair to an existing Dictionary, then print Try to add an item to an existing Tuple, then print /Python_Collections.txt