diff --git a/4b-dictionary.py b/4b-dictionary.py new file mode 100644 index 0000000..bb94f2a --- /dev/null +++ b/4b-dictionary.py @@ -0,0 +1,137 @@ +#!/usr/bin/python3 + +# Added for testing +# Added for testing line 2 +# Examples for dictionary +# Empty Dictionary +emp_dict = {} + +# dictionary with integer keys +emp_dict = {100: 'Sanjeev', 101: 'Jassi'} +print(emp_dict) + +# dictionary with mixed keys +emp_dict = {100: 'Sanjeev', 'skills': ['Python', 'AWS']} +print(emp_dict) + +# using dict() function +emp_dict = dict({100: 'Sanjeev', 101: 'Jassi'}) +print(emp_dict) + +# Let's create emp_dict of this format +''' +{ + 'Employee ID': + { Name: 'string' + Joined: 'yyyy-mm-dd' + Title: 'string' + Skills: [‘list’, ‘of’, ‘skills’] + Project: {‘project_name’: ‘project description’} + } +} +''' + +# Initializing Employee +emp_dict = { + 100: + { + 'name': "Sanjeev", + 'joined': "2017-08-14", + 'title': "Cloud Security Engineer", + 'skills': ['Python', 'AWS', 'AppSec'], + 'projects': { + 'CSPM implementation': 'Implement Cloud Security Posture for AWS' + } + }, + 101: + { + 'name': "Jassi", + 'joined': "2017-10-27", + 'title': "Cloud Security Manager", + 'skills': ['Python', 'AWS', 'AWS Security'], + 'projects': { + 'CSPM implementation': 'Implement Cloud Security Posture for AWS and Azure' + } + } +} + +print(emp_dict) + +# Get the type of emp_dict +print(type(emp_dict)) + +# get keys of a dictionary using keys() +emp_ids = emp_dict.keys() +print(emp_ids) + +# Get values of a dictionary using values() +emp_details = emp_dict.values() +print(emp_details) + +# get key and value both using items() +emps = emp_dict.items() +print(emps) + +# Length of a dictionary (number of items) using len() +print(len(emp_dict)) + +# Iterate through a dictionary +for id in emp_dict.keys(): + print(f"Employee ID: {id}") + print(f"\tEmployee Details: {emp_dict[id]}") + +# Accessing elements from dictionary +# get vs [] for retrieving elements +# Sanjeev +print(emp_dict[100]['name']) + +# ['Python', 'AWS', 'AppSec'] +print(emp_dict[101].get('skills')) + +# Try to access keys which doesn't exist +# None +print(emp_dict[100].get('mailid')) + +try: + print(emp_dict[101]['location']) +except KeyError: + print("Location not found for employee 101") + +# Add one more employee to emp_dict using update() +new_emp = [{ + 102: + { + 'name': "Rakesh", + 'joined': "2018-01-07", + 'title': "Business Analyst", + 'skills': ['Power BI', 'MBA', 'Marketing Expert'], + 'projects': { + 'Flexmind Marketing': 'Increase the membership my targeted marketing' + } + } +}] + +emp_dict.update(new_emp) +print(emp_dict) + +# Update the existing value. Update title of emp_id: 100 as "Sr. Cloud Security Engineer" +emp_dict[100]['title'] = "Sr. Cloud Security Engineer" +print(emp_dict[100]) + +# Learn to delete +# pop, clear, del +# Pop employee id 101 +emp_dict.pop(101, "Not found") +print(len(emp_dict)) + +# Delete the employee 102 +del emp_dict[102] +print(len(emp_dict)) + +# Clear the dictionary +emp_dict.clear() +print(emp_dict) + +# Delete the dictionary itself and printing it would throw an error +del emp_dict +# print(emp_dict) diff --git a/7-regex-examples.py b/7-regex-examples.py new file mode 100644 index 0000000..49e5af1 --- /dev/null +++ b/7-regex-examples.py @@ -0,0 +1,24 @@ +import re + +urls = ["https://www.facebook.com","https://www.google.com","https://www.amazon.in"] + +def checkValidURL(url): + url_reg_ex = r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?" + data = re.search(url_reg_ex,url) + if data is not None: + return True + return False + +def parseDomain(url): + domain = url.split("//")[1].split("www")[1].split(".")[1] + print(domain) + + +if __name__ == "__main__": + for url in urls: + url_status = checkValidURL(url) + if url_status: + try: + parseDomain(url) + except (ValueError, IndexError) as e: + print(f"Error parsing domain for {url}: {e}") diff --git a/README.md b/README.md index 975ccf5..b18da26 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# Python-examples \ No newline at end of file +# Python-examples +Utility functions and scripts