Skip to content

Instantly share code, notes, and snippets.

View ardenn's full-sized avatar
🙉
Remote

Rodgers Ouma ardenn

🙉
Remote
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ardenn
ardenn / dictionary_1.py
Created October 1, 2018 17:43
Python Dictionaries Tutorial - Create a dictionary
# create an empty dictionary
empty_property = dict()
# create dictionary using a list of key-value tuples
job2 = dict([
("title","Marketing & Business Development Manager"),("location","Mombasa"),
("job_type","Full Time"),
("employer","KUSCCO Limited (Kenya Union of Savings & Credit Co-operatives Limited)"),
("category","Marketing & Communications")
])
@ardenn
ardenn / dictionary_2.py
Created October 1, 2018 17:46
Python Dictionaries Tutorial - Create a dictionary
# Using keyword arguments
dict(
title="Marketing & Business Development Manager",
location="Mombasa",job_type="Full Time",
employer="KUSCCO Limited (Kenya Union of Savings & Credit Co-operatives Limited)",
category="Marketing & Communications"
)
@ardenn
ardenn / dictionary_3.py
Created October 1, 2018 17:50
Python Dictionaries Tutorial - Accessing items
# Check existence of title
"title" in job2 # returns True
"salary" in job2 # returns False
# Using key indexing
job2["title"] # return 'Marketing & Business Development Manager'
@ardenn
ardenn / dictionary_4.py
Created October 1, 2018 17:54
Python Dictionaries Tutorial - Accessing items
# Using get() method
job2.get("title") # return 'Marketing & Business Development Manager'
job2.get("salary") # return None
# Passing a second argument to get()
job2.get("salary", 5000) # return 5000
@ardenn
ardenn / dictionary_5.py
Created October 1, 2018 17:57
Python Dictionaries Tutorial - Modifying items
# Adding a new entry for salary using the index
job2["salary"] = 10000
# Modifying the entry for job_type using the index
job2["job_type"] = "Part time"
# Modifying the salary entry using update
job2.update({"salary":20000})
# Adding the available entry using update
@ardenn
ardenn / dictionary_6.py
Created October 1, 2018 17:59
Python Dictionaries Tutorial - Merging dictionaries
extra_info = {
"verified":True,
"qualification":"Undergraduate Degree",
"taxable":True}
# Merge extra_info with job2
job2.update(extra_info)
@ardenn
ardenn / dictionary_7.py
Created October 1, 2018 18:01
Python Dictionaries Tutorial - Deletion
del job2["salary"]
del job2["available"]
print(job2) #return a dictionary without 'salary' and 'available' entries
job1.clear()
print(job1) #return an empty dictionary
del job1
print(job1) # return NameError
@ardenn
ardenn / dictionary_8.py
Created October 1, 2018 18:03
Python Dictionaries Tutorial - Iteration
# Iterating through the dictionary itself
for x in job2:
print(x) # prints the keys of job2
# Using keys()
for key in job2.keys():
print(key) # prints the keys of job2
# Using values()
for val in job2.values():
@ardenn
ardenn / dictionary_9.py
Created October 1, 2018 18:05
Python Dictionaries Tutorial - sorting
with open('jobs.csv','r') as csv_file:
reader = csv.DictReader(csv_file)
for job in reader:
# Using sorted() to sort a dictionary's items on the keys
for key,val in sorted(job.items(),key=lambda item:item[0]):
# Apply any additional processing
print(key, val) #print the keys and values of each job