# page 25 # Python Standard Data Structures # List Set Tuple Dictionary(Dict) # Syntax ## List [] ## Set {} ## Tuple () ## Dict {key:value} # Values ## List single values ## Set single values ## Tuples READ ONLY single values ## Dict Key READ ONLY Value READ/WRITE key: value # Duplicates Allowed ## List Yes ["Bob","Joe","Bob"] > len(3) Bob,Joe,Bob ## Set No Last In {"Bob","Joe","Bob"} > len(2) Bob,Joe ## Tuple Yes ("Bob","Joe","Bob") > len(3) Bob,Joe,Bob READ ONLY ## Dict Key No duplicates Value Yes duplicates ## { "fname": "Joe", "lname": "Joe" } # Lists page 25 firstNames = [] # empty list in memory firstNames = ["Bob", "Joe", "Bob"] # do this first ok, but no preferred # print(firstNames) newFirstName = input("Enter a new contact first name: ") firstNames.append(newFirstName) # print(firstNames) # prints the ['',''] object # for x in ys ## x local variable in the scope ## ys is the data set print() # show the list of contact first names with an id contactId = 0 print("Contact Information") print("-------------------") for first_name in firstNames: # scope print("Contact ID: " , contactId , "Contact First Name: ", first_name) # parameterized function contactId += 1 print("----- EOC ---------") print() # empty parameter # Use a while loop to ask the user if they want to update a contact, then show the new contact menu while True: # Ask the user if they want to update a contact result = input("Do you want to update a contact (yes/no)?") if result.lower() == "no": break # Ask the user what contact to update contactIdToUpdate = int( input("What contact do you want to update? ") ) # Ask the user what to update the contact to contactNewName = input("Enter the updated name: ") firstNames[contactIdToUpdate] = contactNewName # show the list of contact first names with an id contactId = 0 print("Contact Information") print("-------------------") for first_name in firstNames: # scope print("Contact ID: " , contactId , "Contact First Name: ", first_name) # parameterized function contactId += 1 print("----- EOC ---------") print() # empty parameter # page 28 Dictionary or Dict # Create an empty list of contacts contacts = [] # empty list object in memory # Ask the user for the contact first name, last name and age and assign the values in memory as varibles, use a while loop to add additional contacts while True: # you need to create a break point FIRSTNAME = input("Enter the contact first name or type 'quit' to exit: ") # Check to see if the entry was quit, and break out of loop if FIRSTNAME.lower() == "quit": break LASTNAME = input("Enter the contact last name: ") AGE = int( input("Enter the contact age: ") ) contact = { "fName" : FIRSTNAME, "lName" : LASTNAME, "age": AGE } # ends contact{} # add the new contact DICT type to the contacts list contacts.append(contact) print("----BOC----") print(contacts) # [ {k:v}, {k:v}, ... ] print("----EOC----") # Create a contacts list report for contact in contacts: fullName = contact['fName'] + " " + contact['lName'] age = contact['age'] print("Contact information:", fullName , age) # page 30 Tuples # Create a tuple that accepts credit card information from user inputs, then prints the result # Create an empty Tuple credit_card_info = () # storing an empty Tuple in memory # User inputs for credit card information cc_num = input("Enter your credit card number: ") cc_exp = input("Enter your credit card exp date: ") cc_cvc = input("Enter your credit card CVC code: ") # Create the tuple with the user-supplied values credit_card_info = (cc_num, cc_exp, cc_cvc) #len(3) range(0,2) # credit_card_info[2] = "521" # ERROR can not assign a value to a tuple # Print the Tuple print("----Credit Card Information----") print("Credit Card Number:", credit_card_info[0]) print("Credit Card Exp Date:", credit_card_info[1]) print("Credit Card Security Code:", credit_card_info[2]) print("----EO CCI----") print("----Credit Card Information using Line Breaks----\n\n Credit Card Number:", credit_card_info[0], "\n", "Credit Card Exp Date:", credit_card_info[1], "\n", "Credit Card Security Code:", credit_card_info[2], "\n\n", "----End of Credit Card Information using Line Breaks---") # ends print() # Sets page 31 # Create a set of product information using a tuple # (int productSKU, str productName, int productPrice) products = { (1111, "Product 1", 25.24), (2222, "Product 2", 52), (3333, "Product 3", 14.8) } # closes set # Print the set print(products) print("------") # Print the product information in the tuple for product_tuple in products: productSKU, productName, productPrice = product_tuple # tuple model left to right # convert to string data type ## create a list R/W from the tuple R list_productTuple = list(product_tuple) # list(tuple) # formatted an int as string view productSKU_toString = "{}".format(productSKU) # view template variable print("Product SKU:", productSKU_toString) # view print("Product Name:", productName) print("Product Price:", productPrice) # locale formatting en/US