<jupyter_start><jupyter_text>Exercise 2  Part 1 - Lists Use a loop to make a list of the integers from 5 to 10<jupyter_code>int_list = []
for i in range(5,11):
    int_list.append(i)
int_list<jupyter_output><empty_output><jupyter_text>Use a list comprehension to make a list of the integers from 20 to 30<jupyter_code>int_list_2 = [i for i in range(20,31)]
int_list_2<jupyter_output><empty_output><jupyter_text>Print just the 3rd, 4th, and 5th integers in that array<jupyter_code>print(int_list_2[2:5])<jupyter_output>[22, 23, 24]<jupyter_text>Part 2 - Dictionaries and functions Make a dictionary that contains the number of characters for each word in a listE.g.               ['hello', 'world!'] -> {'hello': 5, 'world!': 6}<jupyter_code>word_list = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
num_chars = {}
for w in word_list:
    num_chars[w] = len(w)
print(num_chars)<jupyter_output>{'Mercury': 7, 'Mars': 4, 'Neptune': 7, 'Saturn': 6, 'Uranus': 6, 'Earth': 5, 'Jupiter': 7, 'Venus': 5}<jupyter_text>Add poor Pluto back to the list<jupyter_code>word_list.append('Pluto')
word_list<jupyter_output><empty_output><jupyter_text>Now try to make up the lookup for the number of characters using a comprehension instead of a loop<jupyter_code>dict_comprehension = {w: len(w) for w in word_list}
dict_comprehension<jupyter_output><empty_output><jupyter_text>Count the total number of characters in the list using a loop<jupyter_code>total = 0
for w in word_list:
    total += dict_comprehension[w]
print(total)<jupyter_output>52<jupyter_text>Can you count the total number of characters using just `dict_comprehension` and no loops? _Hint_: Inspect the `dict_comprehension` object - does it contain any methods or properties that might help you?<jupyter_code>dir(dict_comprehension)
sum(dict_comprehension.values())<jupyter_output><empty_output><jupyter_text>Part 3 - FactorsDesign a function which returns a list containing all of the [factors](https://en.wikipedia.org/wiki/Integer_factorization) of an integerE.g.:         10 -> [1, 2, 5, 10]        50 -> [1, 2, 5, 10, 25, 50]<jupyter_code>def factors(n):
    ''' Return a list containing the factors for integer n '''
    fac = []
    for i in range(1,n+1):
        if n % i == 0:
            fac.append(i)
    return fac
factors(10)<jupyter_output><empty_output><jupyter_text>**Bonus**: Can you do it in one line?<jupyter_code>def one_line_factors(n):
    ''' Return a list containing the factors for integer n '''
    return [i for i in range(1,n+1) if n % i == 0]
one_line_factors(10)<jupyter_output><empty_output><jupyter_text>Part 4 - Fibonacci SequenceCreate a function which will return the first `n` numbers in the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number) starting at 1 (1, 1, 2, 3, 5, 8, 13, ...)<jupyter_code>def fib(n):
    ''' Return the first n digits of the Fibonacci sequence '''
    seq = [1,1]
    while len(seq) < n:
        seq.append(seq[-1] + seq[-2])
    return seq
fib(10)
assert fib(10)[-1] == 55<jupyter_output><empty_output><jupyter_text>**Bonus**: Design a function that creates a simple "graph" of the Fibonacci sequencee.g.``````_Hint_: What happens if you multiply a `string`?<jupyter_code>def fib_graph(n):
    ''' Draw a graph of the first n digits of the Fibonacci sequence '''
    seq = fib(n)
    for i in seq:
        print('#' * i)
fib_graph(10)
def alt_fib_graph(n):
    ''' Draw a graph of the first n digits of the Fibonacci sequence '''
    seq = fib(n)
    return '\n'.join([''.join(['#' for i in range(x)]) for x in seq])
print(alt_fib_graph(10))<jupyter_output>#
#
##
###
#####
########
#############
#####################
##################################
#######################################################<jupyter_text>__Super Bonus__: Can you create a Fibonacci [`class`](https://docs.python.org/3/tutorial/classes.html) which calculates the sequence of order `n`, stores it, and can print it as a graph?<jupyter_code>class Fibonacci():
    ''' Creates the Fibonacci sequence of order n, both storing it and making graphs of it '''

    def __init__(self, n, graph_it=False):
        ''' Initialize the sequence by storing n, and then calculating the sequence '''
        self.n = n
        self.calculate()
        if graph_it:
            self.graph()
        
    def calculate(self):
        ''' Calculate the Fibonacci sequence of order n and store it in self.seq '''
        seq = [1,1]
        while len(seq) < self.n:
            seq.append(seq[-1] + seq[-2])
        self.seq = seq
        
    def graph(self):
        ''' Print a graph of the Fibonacci sequence '''
        for i in self.seq:
            print('#' * i)
fib11 = Fibonacci(11)
fib11.seq
fib11.graph()
fib8 = Fibonacci(8, graph_it=True)<jupyter_output>#
#
##
###
#####
########
#############
#####################
StarcoderdataJupyterStructuredCleanDedup