Simeon Franklin
Sections 1-4: basic types and truth values
- How do you run a python script called myscript.py?
- How do you enter interactive python mode?
- What does:
do?x = 1 - 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.
- What string values are truthy? Which are falseish? How about numbers?
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
- How do you tell how many items are in a list? How can I add an item to a list?
- Given
How do I retrieve the first item? The last item? A slice of all items but the first? Every other item?lst = ['a','b','c','d','e','f','g'] - Tuples are a lot like lists. What is the most important difference?
- Given a tuple like
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?tup = (100, 200) - Given a dictionary
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".x = {'key1': 100, 'key2': 9} - What is most distinctive about sets?
Section 7 - functions
- Write the simplest possible function that does nothing.
- Write a function that takes two numeric arguments and returns their difference.
- 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?
- 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?
- Given a list with three things in it, how can I call a function passing each item as a parameter to the function?
- Given a dictionary with two key/value pairs, how can I call a function passing each item as a named parameter to the function?
- What is the most general and generic function definition signature?
- What initial values for named parameters may cause problems? Why?
Section 8 - Exceptions
- How do you handle exceptions?
- How do you raise an exception?
- How do re-raise an exception in an except: block?
- What is the purpose of finally?
Section 9 - Namespaces and modules
- (so far) What creates a new namespace?
- What are the 5 different ways to use the import statement?
- What does and import statement like
actually do? What has changed in your current namespace?import pprint - What is a module?
- 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 :)
- How can I sort this list of rows? How is it sorted?
- How can I get a new data where each row is sorted?
- How can I sort by the second item in each row? Reverse order?
- How can I limit data to only contain rows with no '0' terms?
- 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?
- 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?
- Where have we already used an iterator?
