Read Allen Downey's Think Python for getting up to speed with Python 2.7 and computer science topics. It's completely available online, or you can buy a physical copy if you would like.
For quick and easy interactive practice with Python, many people enjoy Codecademy's Python track. There's also Learn Python The Hard Way and The Python Tutorial.
How are Python lists and tuples similar and different? Which will work as keys in dictionaries? Why?
Lists and tuples are both ways to store data. Lists are mutable, meaning you can edit them after creation. Tuples are immutable, and as such, can work as keys in a dictionary.
How are Python lists and sets similar and different? Give examples of using both. How does performance compare between lists and sets for finding an element. Why?
Both can be used to store data. Lists are ordered and can contain duplicate values. Sets are unordered and only store unique values. Sets would be more efficient at finding an element because they don't have to search sequentially. A list of numbers can contain duplicate values in a specified order: nums=[1,2,2,2,1,1] If you tried to store the same values as a set, it would discard the duplicates: nums={1,2}
Describe Python's lambda
. What is it, and what is it used for? Give at least one example, including an example of using a lambda
in the key
argument to sorted
.
Lambda allows you to write anonymous functions (aka functions that are unnamed).
Example: lst=[-4,-2,0,1,5] sorted(lst,key=lambda x:x**2)
Explain list comprehensions. Give examples and show equivalents with map
and filter
. How do their capabilities compare? Also demonstrate set comprehensions and dictionary comprehensions.
List comprehensions allow for construction of lists using specified definitions. For example: myList = [x**2 for x in range(10)]. You can accomplish similar funcionality using the 'map' and 'filter' functionalities. 'map' can be used to apply a function to each item in a list. 'filter' can be used to remove items in a list which don't match a predicate.
Map example:
nums=[1,2,3,4]
result=list(map(lambda x: x**2,nums))
Filter example: nums=[1,2,3,4]
result=list(filter(lambda x: x%2==0,nums))
Set comprehension example:
a = {x for x in 'abracadabra' if x not in 'abc'}
Dictionary comprehension example:
d = {n: n**2 for n in range(5)}
Use Python to compute days between start and stop date.
a.
date_start = '01-02-2013'
date_stop = '07-28-2015'
937
b.
date_start = '12312013'
date_stop = '05282015'
513
c.
date_start = '15-Jan-1994'
date_stop = '14-Jul-2015'
7850
Place code in this file: q5_datetime.py
Edit the 7 functions in q6_strings.py
Edit the 5 functions in q7_lists.py
Write a script as indicated (using the football data) in q8_parsing.py