Simeon Franklin

Sections 1-4: basic types and truth values

  1. How do you run a python script called myscript.py?
  2. How do you enter interactive python mode?
  3. What does:
    x = 1
    do?
  4. At the interactive console create a variable "message" containing the string "This is section ". Create a variable "section" with the value 1. Print out the string "This is section 1" by joining the two variables together.
  5. What string values are truthy? Which are falseish? How about numbers?
  6. 
        x = 1
        y = "a string"
        z = 0
        x and z # what will this evaluate to? Why?
        z or y # ditto
        if z:
            print y # will this print? Why not?
        if z == False:
            print y # How about this? Try it - and explain.
        if z is False:
            print y
          
        

Section 5: containers and slicing

  1. How do you tell how many items are in a list? How can I add an item to a list?
  2. Given
    
        lst = ['a','b','c','d','e','f','g']
        
    How do I retrieve the first item? The last item? A slice of all items but the first? Every other item?
  3. Tuples are a lot like lists. What is the most important difference?
  4. Given a tuple like
    
        tup = (100, 200)
       
    How can I assign the first element to the variable 'x' and the second to the variable 'y' in a single line? What is this operation called?
  5. Given a dictionary
    
      x = {'key1': 100, 'key2': 9}
      
    How do I check if the key "key3" exists in the dict? What about another way of checking? How do I retrieve the value of "key1"? How can I set a new key "python".
  6. What is most distinctive about sets?

Section 7 - functions

  1. Write the simplest possible function that does nothing.
  2. Write a function that takes two numeric arguments and returns their difference.
  3. Write a function that takes two numeric arguments and returns their difference. The second argument should have a default value of 0. What two words can we use to describe the first parameter? What two words describe the second?
  4. How can I write a function that takes an unlimited number of positional parameters? 1 mandatory positional parameters and optionally as many more as you want? Any number of named parameters?
  5. Given a list with three things in it, how can I call a function passing each item as a parameter to the function?
  6. Given a dictionary with two key/value pairs, how can I call a function passing each item as a named parameter to the function?
  7. What is the most general and generic function definition signature?
  8. What initial values for named parameters may cause problems? Why?

Section 8 - Exceptions

  1. How do you handle exceptions?
  2. How do you raise an exception?
  3. How do re-raise an exception in an except: block?
  4. What is the purpose of finally?

Section 9 - Namespaces and modules

  1. (so far) What creates a new namespace?
  2. What are the 5 different ways to use the import statement?
  3. What does and import statement like
    import pprint
    actually do? What has changed in your current namespace?
  4. What is a module?
  5. What is a package? How do you make one?

Section 13 - Higher Order Functions


>>> import random
>>> data = []
>>> data.append(random.sample(range(0,20), 5))
>>> data.append(random.sample(range(0,20), 5))
>>> data.append(random.sample(range(0,20), 5))
>>> data.append(random.sample(range(0,20), 5))
>>> data
[[5, 15, 9, 0, 13], [7, 16, 3, 8, 10], [4, 8, 5, 7, 11], [4, 13, 6, 7, 17]]

Solve the following in the interactive console, one line only please :)

  1. How can I sort this list of rows? How is it sorted?
  2. How can I get a new data where each row is sorted?
  3. How can I sort by the second item in each row? Reverse order?
  4. How can I limit data to only contain rows with no '0' terms?
  5. How can I get a list of the sums of the rows? A sum of the sums?

Section 14: List comprehensions

Repeat the functional exercises using list comprehensions to solve. Which method is easier for you? Which method is easier to read?

  1. What is a generator? What can you do with a generator? What advantages does an generator possess over a list? What are the two ways in Python of creating generators?
  2. Where have we already used an iterator?