This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Create three dictionaries: lloyd, alice, and tyler. | |
| Give each dictionary the keys "name", "homework", "quizzes", and "tests". | |
| Have the "name" key be the name of the student (that is, lloyd's name should be "Lloyd") | |
| and the other keys should be an empty list. | |
| ''' | |
| lloyd = { | |
| "name":"Lloyd", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Now you need your compute_bill function to take the stock/inventory of a particular item into account when computing the cost. | |
| Ultimately, if an item isn't in stock, then it shouldn't be included in the total. You can't buy or sell what you don't have! | |
| shopping_list = ["banana", "orange", "apple"] | |
| stock = { | |
| "banana": 6, | |
| "apple": 0, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create a new dictionary called prices using {} format like the example above. | |
| # Put these values in your prices dictionary, in between the {}: | |
| prices = { | |
| "banana": 4, | |
| "apple": 2, | |
| "orange": 1.5, | |
| "pear": 3 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Strings are like lists with characters as elements. You can loop through strings the same way | |
| for letter in "Codecademy": | |
| print letter | |
| # Empty lines to make the output pretty | |
| word = "Programming is fun!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Functions can also take lists as inputs and perform various operations on those lists. | |
| def count_small(numbers): | |
| total = 0 | |
| for n in numbers: | |
| if n < 10: | |
| total = total + 1 | |
| return total | |
| lost = [4, 8, 15, 16, 23, 42] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The blocks of code in a for loop can be as big or as small as they need to be. | |
| # While looping, you may want to perform different actions depending on the particular item in the list. | |
| # example | |
| numbers = [1, 3, 4, 7] | |
| for number in numbers: | |
| if number > 6: | |
| print number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # You can also use a for loop on a dictionary to loop through its keys with the following: | |
| # A simple dictionary | |
| d = {"foo" : "bar"} | |
| for key in d: | |
| print d[key] # prints "bar" | |
| # Note that dictionaries are unordered, meaning that any time you loop through a dictionary, | |
| # you will go through every key, but you are not guaranteed to get them in any particular order. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # for loops allow us to iterate through all of the elements in a list from the left-most (or zeroth element) | |
| # to the right-most element. A sample loop would be structured as follows: | |
| a = ["List of some sort”] | |
| for x in a: | |
| # Do something for every x | |
| # This loop will run all of the code in the indented block under the for x in a: statement. | |
| # The item in the list that is currently being evaluated will be x. So running the following: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Let's go over a few last notes about dictionaries | |
| my_dict = { | |
| "fish": ["c", "a", "r", "p"], | |
| "cash": -4483, | |
| "luck": "good" | |
| } | |
| print my_dict["fish"][0] | |
| # 1. In the example above, we created a dictionary that holds many types of values. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Sometimes you need to remove something from a list. | |
| # example | |
| beatles = ["john","paul","george","ringo","stuart"] | |
| beatles.remove("stuart") | |
| print beatles | |
| backpack = ['xylophone', 'dagger', 'tent', 'bread loaf'] |