# Chapter6.py # Data Sets / Collections # Single Element or Item / unstructured / Dictionary key:value # Some of the properties when choosing a collection ## Does the data need to change? immutable no mutatable yes ## Do I need the data elements to be indexed? index 0-length ## Does the data need to be pre sorted? sort order? ## Do I need duplicates? # Lists [el1, el2,...] ## mutatable -> add, append, delete, update, ... ## indexed positional index[0] 3 elements O-2 length:3 Bob,Joe,Mary ## The base sort order is by index id ## duplicate values are ok ["Bob","Joe","Bob","Mary"] len<4> 0:3 # Sets {item1, item2, ...} ## read objects ## not indexed {"Bob","Mary","Frank"} positional id{1} length 3 ## No sort order at all {"Bob","Mary","Frank"} {"Mary","Bob","Frank"} ... ## duplicates are not allowed Bob,Joe,Beth,Bob -> {} Bob,Joe,Beth # Tuples (item1, item2, ...) ## immutable -> no append(), no update(), ... ## tuple -> list [do changes] -> tuple ## positional ids param1, param2 -> pass args in order of params ## duplicates ok # Dictionary / Map / Hash Map ## key: value pairs ## key can not be duplicated ## values can be duplicated ## { firstname: Bob, lastname: Smith }, { firstname: Jane, lastname: Smith } # Structured Data / Dimensional Data ## Python -> Array (pandas) Level 3 Class "dataframe" ## 1 Dimensional Array (col) Series of Data ## 2 Dimensional Array (row,col) dataframe ## 3 Dimensional Array p1 -> (row,col) Excel pivot tables ## 4 Dimensional Array p2 -> p1 -> (row,col) ## ... ## 7 Dimensional Array p5 -> p4 -> p3 -> p2 -> p1 -> (row,col) # Chapter 6 Lists page 164 and Tuples page 194 # Lists product1 = ["widget1", 10.50, "widget 1 desc", True] product2 = ["widget2", 15.50, "widget 2 desc", False] product3 = ["widget3", 9.50, "widget 3 desc", True] # Products List products = [ ["widget4" , 10.50 , "widget 4 desc" , True], ["widget5" , 15.50, "widget 5 desc" , False], ["widget6" , 9.50 , "widget 6 desc" , True] ] # products # Print the exiting list of products ## for x in ys ## x is the local variable in the scope "sub operation" ## ys is the dataset def printProducts(): for product in products: print("=== Product ===") print(product) print("===============") # Ask the user to add a new product def addProduct(): # Step 1 Ask the user to add the product details sku = input("Enter the product sku: ") # string price = float(input("Enter the product price: ")) # float description = input("Enter the product description: ") # string inStock_input = input("Is the product in stock (yes/no): ").strip().lower()# YES, YEs, Yes inStock = inStock_input in ['yes','y'] # True for boolean # Step 2 Append the new product to the products list products.append([sku,price,description,inStock]) # Ask the user to add new products def userAddProduct(): printProducts() while True: addProduct() # call the addProduct() addAnotherProduct = input("Do you want to add a new product (yes,no): ").strip().lower() # set the variable addAnotherProduct if addAnotherProduct not in ['yes','y']: break printProducts() # Tuple def creditCardProc(): cc_num = input("Enter your credit card number: ") cc_exp = input("Enter your credit card exp date: ") cc_cvc = input("Enter your credit card security code: ") cc_zip = input("Enter your credit card zip: ") # 4 objects in memory # Create a tuple from the objects in memory cc_info = (cc_num, cc_exp, cc_cvc, cc_zip) # Use the zip() function to read the tuple # for x in zip() print("Credit Card Information") field_headers = ["Credit Card Number","Exp Date","CVV Code","Zip Code"] # for var1,var2 in zip(LIST,TUPLE) for field_headers, cc_info in zip(field_headers, cc_info): print(f"{field_headers} {cc_info}") # list + tuple different types # f"{templatevar}" ignores the type, only cares about value