Skip to content

Instantly share code, notes, and snippets.

@tgb20
Created January 14, 2020 23:53
Show Gist options
  • Select an option

  • Save tgb20/9b3cf389fcb24284fdb540bf11183adc to your computer and use it in GitHub Desktop.

Select an option

Save tgb20/9b3cf389fcb24284fdb540bf11183adc to your computer and use it in GitHub Desktop.

Revisions

  1. tgb20 created this gist Jan 14, 2020.
    57 changes: 57 additions & 0 deletions ryanchapter12review.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    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