Skip to content

Instantly share code, notes, and snippets.

@Mikelew88
Created October 23, 2017 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mikelew88/307209273f83060aae2f5b02d4d73787 to your computer and use it in GitHub Desktop.
Save Mikelew88/307209273f83060aae2f5b02d4d73787 to your computer and use it in GitHub Desktop.
Python 101 Blog Example
# variable
var1 = 10
# list
anything = [10, 10, 3, 2]
# dict
dict1 = {'apple': 'green', 'grape':'red', 'other grape':'green', 'apple':'red'}
# loops through lists
for index, element in enumerate(anything):
# My favorite method for inserting variables into strings
print("Element {0}: {1}".format(index, element))
# another method for inserting into strings.
print("Element %i: %i" % (index, element))
# through dicts
for key, val in dict1.iteritems():
if val == 'green':
print(key + ' is ' + val)
# ex function 1
def key_in_value(input_to_function):
"""
INPUT: dict
OUTPUT: list
Return the keys from the dictionary where the key is a member in the
associated value.
example:
INPUT: {"a": ["b", "c", "a"], "b": ["a", "c"], "c": ["c"]}
OUTPUT: ["a", "c"]
Hint: Use iteritems
(Can be done on one line with a list comprehension)
"""
pass
# call function
output = key_in_value({"a": ["b", "c", "a"], "b": ["a", "c"], "c": ["c"]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment