Hack My Planet

Day 17

1 def generateFibonacci():
2     count = int(input("How many fibonacci numbers would you like to generate? "))
3     i = 1
4     if count == 0:
5         fib = []
6     elif count == 1:
7         fib = [1]
8     elif count == 2:
9         fib = [1,1]
10    elif count > 2:
11        fib = [1,1]
12        while i < (count - 1):
13           fib.append(fib[i] + fib[i-1])
14           i += 1
15    return fib

Write out what the code does line-by-line

Example:

Line 1: Define a function named generateFibonacci() that takes in no parameters.

Line 2: blah blah blah

Modules & Libraries

  • Modules are imported Python files containing definitions and statements, such as functions and objects.
    • network.py
  • Libraries are collections of modules

Dictionaries

  • Dictionaries are collections of key-value pairs
  • Store information about anything
    • dog = {"color": "brown", "age": 6}
    • print(dog["color"])
    • print(dog["age"])
  • Define dictionaries in curly braces {}, with the key in quotes, and its value after a colon
    • The value can be any data type
  • Access dictionary values by calling the key in brackets []

Modifying Dictionaries

  • Modify values in a dictionary by giving the name of the key in brackets and then the new value
    • dog["color"] = "black"
  • Add keys the same way, except with a new key name
    • dog["size"] = "medium"
    • print(dog)
  • Remove key-value pairs with the del statement
    • del dog["color"]
    • print(dog)

A Dictionary of Similar Objects

  • The previous examples involved storing different kinds of information about one object (a dog)
  • You can use a dictionary to store one kind of information about many objects
favorite_languages = {
    'john': 'Python',
    'irene': 'C',
    'yukio': 'Python',
    'charles': 'Java',
    }
    

Dictionary Exercises

  • Use a dictionary to store information about a person you know. Store their first name, last name, age, and the city in which they live. You should have keys such as first_name, last_name, age, and city. Print each piece of information stored in your dictionary

 

  • Think of five programming or networking terms you've learned in this class. Use them as keys, and their definitions as values in a dictionary. Print each word and its meaning as neatly formatted output.
    • You might print the word followed by a colon and then its meaning, or print the word on one line and its meaning indented on a second line (use the newline character \n to insert a blank line)
Made with Slides.com