Skip to content

Instantly share code, notes, and snippets.

@tgb20
Created January 14, 2020 23:53
Show Gist options
  • Save tgb20/9b3cf389fcb24284fdb540bf11183adc to your computer and use it in GitHub Desktop.
Save tgb20/9b3cf389fcb24284fdb540bf11183adc to your computer and use it in GitHub Desktop.
example = ["cow", "pig", "goat", "sheep"]
# What are two ways to add something to a list?
example.append("chicken")
example.insert(1, "horse")
print("Adding to list:", example)
# What are two ways to remove something from a list?
del(example[0])
example.remove("goat")
print("Removing from list:", example)
# What are two ways to get a sorted copy of a list, without changing the original list?
example2 = example[:] # slice notation, gets copy of list
example2.sort()
print("Example", example, "Example 2", example2)
example3 = example.copy()
example3.sort()
print("Example", example, "Example 3", example3)
# How do you find out whether a certain value in a list?
if "goat" not in example:
print("Goat not in example")
if "pig" in example:
print("Pig in example")
# How do you find out the location of a certain value in a list?
print(example)
print(example.index("pig"))
# What's a tuple?
# A tuple is a list you can't change, and its made using () instead of []
test = ("red", "green", "blue")
# How do you make a list of lists?
listOfLists = []
listOfLists.append(example)
listOfLists.append(example2)
listOfLists.append(example3)
print(listOfLists)
# How do you get a single value from a list of lists?
print(listOfLists[0][0]) # First index of list, second index of item in list
# What is a dictionary?
# A dictionary is how you would store variables, with keys
dictionary = {"Monday":"Hello", "Tuesday":"Goodbye"}
# How do you add an item to a dictionary?
dictionary["Wednesday"] = "Hi"
print(dictionary)
# How do you look up an item from its key?
print(dictionary["Tuesday"]) # In brackets put the key, not the value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment