
Python - Write Python code to Perform
- 26th Aug, 2021
- 17:58 PM
# -*- coding: utf-8 -*- """Kon Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1Fe88oyprBo7upG_lvspyq9t1S5Rz_fjp """ # Question 1: supplier_102={ "city": "Lake Peterfurt", "contact_firstname": "Lucas", "contact_lastname": "Reyes", "contact_title": "Mr", "country": "Australia", "email": "lucas7404.reyes@gmail.com", "notes": "", "phone": "(02) 2310 6339", "postcode": 2634, "state": "Reyes", "street_address": "09 /n 0 Tara Laneway", "supplier_id": 102, "supplier_name": "Mata, Townsend and Black" } # 1. Print all the keys from the dictionary print("All the keys from the dictionary are: ",supplier_102.keys()) print() # 2. Print all the values from the dictionary print("All the values from the dictionary are: ", supplier_102.values()) print() # 3. Check if item ‘mobile’ exists and print an appropriate message if "mobile" in supplier_102: print("Yes, mobile exists in the supplier_102 ") else: print("No, mobile does not exists in the supplier_102 ") print() # 4. Delete all items from the dictionary and print supplier_102.clear() print("Updated Supplier_102 is as follows: ",supplier_102) # Question 2: suppliers = [ { "city":"St. Kellyton", "contact_firstname":"Michelle", "contact_lastname":"Mitchell", "contact_title":"Mrs", "country":"Australia", "email":"michelle3113.mitchell@gmail.com", "notes":"", "phone":"(03) 9269 4800", "postcode":2693, "state":"Mitchell", "street_address":"2 Alexander Grove", "supplier_id":101, "supplier_name":"Ferguson Inc"}, { "city":"Lake Peterfurt", "contact_firstname":"Lucas", "contact_lastname":"Reyes", "contact_title":"Mr", "country":"Australia", "email":"lucas7404.reyes@gmail.com", "notes":"", "phone":"(02) 2310 6339", "postcode":2634, "state":"Reyes", "street_address":"09 /n 0 Tara Laneway", "supplier_id":102, "supplier_name":"Mata, Townsend and Black"}, { "city":"Parkerborough", "contact_firstname":"Marcia", "contact_lastname":"Stanley", "contact_title":"Mrs", "country":"Australia", "email":"marcia2459.stanley@gmail.com", "notes":"", "phone":"(07) 2644 5735", "postcode":2619, "state":"Stanley", "street_address":"Suite 071n 3 Cody Upper", "supplier_id":103, "supplier_name":"White, Vargas and Ballard"} ] # 1. Find the length of list print("The length of the list suppliers is: ", len(suppliers)) print() # 2. Change “supplier_name” of supplier with “supplier_id”=102 from “Mata, Townsend and Black” to “Mata, Townsend and Black & Co" for dicti in suppliers: if (dicti["supplier_id"] == 102): dicti["supplier_name"]= "Mata, Townsend and Black & Co" print("Modified Dictionary:",dicti) print() # 3. Add a new supplier to the list new_supplier= {"city":"", "contact_firstname":"", "contact_lastname":"", "contact_title":"", "country":"", "email":"", "notes":"", "phone":"", "postcode":"", "state":"", "street_address":"", "supplier_id":suppliers[-1]["supplier_id"]+1, "supplier_name":""} suppliers.append(new_supplier) print("New Supplier_id added in supplier list:") for dicti in suppliers: print(dicti) print() # 4. Remove supplier with “supplier_id” =103 for dicti in suppliers: if (dicti["supplier_id"] == 103): suppliers.remove(dicti) print("The updated suppleir list is as follows (deleted suplier having id 103):") for dicti in suppliers: print(dicti) # Question 3 lst_num=[1, 25, 2, 30, 8] def max_items(items): maxm = 0 for i in items: if i > maxm: maxm= i return maxm max_items(lst_num) # Question 4: suppliers= [ { "city":"St. Kellyton", "contact_firstname":"Michelle", "contact_lastname":"Mitchell", "contact_title":"Mrs", "country":"Australia", "email":"michelle3113.mitchell@gmail.com", "notes":"", "phone":"(03) 9269 4800", "postcode":2693, "state":"Mitchell", "street_address":"2 Alexander Grove", "supplier_id":101, "supplier_name":"Ferguson Inc"}, { "city":"Lake Peterfurt", "contact_firstname":"Lucas", "contact_lastname":"Reyes", "contact_title":"Mr", "country":"Australia", "email":"lucas7404.reyes@gmail.com", "notes":"", "phone":"(02) 2310 6339", "postcode":2634, "state":"Reyes", "street_address":"09 /n 0 Tara Laneway", "supplier_id":102, "supplier_name":"Mata, Townsend and Black"}, { "city":"Parkerborough", "contact_firstname":"Marcia", "contact_lastname":"Stanley", "contact_title":"Mrs", "country":"Australia", "email":"marcia2459.stanley@gmail.com", "notes":"", "phone":"(07) 2644 5735", "postcode":2619, "state":"Stanley", "street_address":"Suite 071n 3 Cody Upper", "supplier_id":103, "supplier_name":"White, Vargas and Ballard"} ] name_lst=[] for dicti in suppliers: name_lst.append(dicti.get('supplier_name')) print("List of Supplier Names are as follows: ",name_lst) print() print("List converted into a Single String is as follows:") print("; ".join(name_lst)) # Question 5: prod_104 = {"discontinued":0, "lead_time_days":4, "product_category":"Personal Computers", "product_description":"8 inch Display (1920×1200)", "product_id":104, "product_name":"NVIDIA SHIELD Tablet (WiFi)", "reorder_level":10, "unit_price":299.0 } print("The Dictionary prod_104 is as follow: ") print(prod_104 ) # Question 6: suppliers= [ { "city":"St. Kellyton", "contact_firstname":"Michelle", "contact_lastname":"Mitchell", "contact_title":"Mrs", "country":"Australia", "email":"michelle3113.mitchell@gmail.com", "notes":"", "phone": "(03) 9269 4800", "postcode":2693, "state":"Mitchell", "street_address":"2 Alexander Grove", "supplier_id":101, "supplier_name":"Ferguson Inc" }, { "city":"Lake Peterfurt", "contact_firstname":"Lucas", "contact_lastname":"Reyes", "contact_title":"Mr", "country": "Australia", "email":"lucas7404.reyes@gmail.com", "notes":"xyz", "phone":"(02) 2310 6339", "postcode":2634, "state":"Reyes", "street_address":"09 /n 0 Tara Laneway", "supplier_id":102, "supplier_name":"Mata, Townsend and Black" }, { "city":"Parkerborough", "contact_firstname":"Marcia", "contact_lastname": "Stanley", "contact_title":"Mrs", "country":"Australia", "email": "marcia2459.stanley@gmail.com", "notes":"", "phone":"(07) 2644 5735", "postcode":2619, "state":"Stanley", "street_address":"Suite 071n 3 Cody Upper", "supplier_id":103, "supplier_name":"White, Vargas and Ballard"} ] resulted_list = list(filter(lambda x: x["city"] in ["Parkerborough", "Lake Peterfurt"], suppliers)) print("Length of resulted_list: ",len(resulted_list)) print() print("The resulted list is as follows: ") resulted_list # Question 7: suppliers = [ { "city":"St. Kellyton", "contact_firstname":"Michelle", "contact_lastname":"Mitchell", "contact_title":"Mrs", "country":"Australia", "email":"michelle3113.mitchell@gmail.com", "notes":"", "phone":"(03) 9269 4800", "postcode":2693, "state":"Mitchell", "street_address":"2 Alexander Grove", "supplier_id":101, "supplier_name":"Ferguson Inc" }, { "city":"Lake Peterfurt", "contact_firstname":"Lucas", "contact_lastname":"Reyes", "contact_title":"Mr", "country":"Australia", "email":"lucas7404.reyes@gmail.com", "notes":"xyz", "phone":"(02) 2310 6339", "postcode":2634, "state":"Reyes", "street_address":"09 /n 0 Tara Laneway", "supplier_id":102, "supplier_name":"Mata, Townsend and Black"}, { "city":"Parkerborough", "contact_firstname":"Marcia", "contact_lastname":"Stanley", "contact_title":"Mrs", "country":"Australia", "email":"marcia2459.stanley@gmail.com", "notes":"", "phone":"(07) 2644 5735", "postcode":2619, "state":"Stanley", "street_address":"Suite 071n 3 Cody Upper", "supplier_id":103, "supplier_name":"White, Vargas and Ballard"} ] result = list(map(lambda x: str(x["supplier_id"])+"-"+x["supplier_name"] , suppliers)) result # Question 8: from datetime import datetime # Convert a date string in the format “dd/mm/yyyy” to a date object and print it date_string = '21/02/2020' datetime_object = datetime.strptime(date_string, '%d/%m/%Y').strftime('%m/%d/%y') print("Converted Date-Time object in python: ",datetime_object) print() #Create a date object using the values (2020, 3, 20) x = datetime(2020, 3, 20) print("A date object using the values (2020, 3, 20):",x.strftime("%d/%m/%y")) # Question 9 import re phone_num = "(03) 9269 4800" start = re.escape("(") end = re.escape(")") code = re.search('%s(.*)%s' % (start, end), phone_num).group(1) print(code) supplier2={ "city":"St. Kellyton", "contact_firstname":"Michelle", "contact_lastname":"Mitchell", "contact_title":"Mrs", "country":"Australia", "email":"michelle3113.mitchell@gmail.com", "notes":"", "phone":"(03) 9269 4800", "postcode":2693, "state":"Mitchell", "street_address":"2 Alexander Grove", "supplier_id":101, "supplier_name":"Ferguson Inc" } class Supplier: # The init method or constructor def __init__(self, supplier): # Instance Variable self.city = supplier["city"] self.contact_firstname= supplier["contact_firstname"] self.contact_lastname = supplier["contact_lastname"] self.contact_title = supplier["contact_title"] self.country= supplier["country"] self.email= supplier["email"] self.notes= supplier["notes"] self.phone= supplier["phone"] self.postcode= supplier["postcode"] self.state= supplier["state"] self.street_address= supplier["street_address"] self.supplier_id= supplier["supplier_id"] self.supplier_name= supplier["supplier_name"] # Adds an instance variable def set_supplier_name(self, supplier_name): self.supplier_name = supplier_name # Retrieves instance variable def get_supplier_details(self): return str(self.supplier_id) + "-" + self.supplier_name supp= Supplier(supplier2) supp.get_supplier_details() supp2 = Supplier(supplier2) supp2.set_supplier_name("Cartan") supp2.get_supplier_details()