python_fundamentals Flashcards
UTF
- unicode transformation format
- used to represent over a million characters, including:
- foreign languages
- mathematical symbols
- emojis
- ASCII was only developed to hold 256 characters, including:
- Upper case and lower case latin
- numbers
- UTF overcomes the limitations of ASCII
Given the string (x = ‘This is a string’), how would you use slicing to return the following?
- first character
- first two characters
- last character
- fourth to last and third to last
- the string backwards
- first character
- x[0]
- first two characters
- x[0:2]
- last character
- x[-1]
- fourth to last and third to last
- x[-4:-2]
- the string backwards
- x[::-1]
Based on the following dictionary, how would you print the key value pairs?
- x = {‘Christopher Brooks’: ‘brooksc@umich.edu’, ‘Bill Gates’: ‘billg@microsoft.com’}
- for key,value in x.items():
print(key)
print(value)
Given a .csv file named ‘mpg.csv’, how would you read in the contents of the file and store them in a variable named ‘mpg’ and print the first 3 records? How would you print every record stored in ‘mpg’?
import csv
%precision 2
with open(mpg.csv) as csvfile: mpg = list(csv.DictReader(csvfile))
print(mpg[:3])
print(len(mpg))
After storing the .csv file in ‘mpg’, how would print out all of the column headers?
Hint: the column headers are keys in each dictionary record
- mpg[0].keys()
Given all the dictionaries of cars stored in mpg, how would you compute the average ‘cty’ mpg for all cars?
Hint: The mpg for city travel is stored within the value for the key ‘cty’
- Because the all the values in the list are strings, you need to convert to ‘float’ in order to perform mathematical calculations
- sum(float(d[‘cty’] for d in mpg) / len(mpg)
How would you create a set of all the cylinders in the dataset?
How would you loop through each record and compute the city mpg based on cylinder of the car?
Hint: the cylinders are within the heading ‘cyl’
- cylinders = set(d[‘cyl’] for d in mpg)
- print(cylinders)
- {‘4’, ‘5’, ‘6’, ‘8’}
- attached is the logic for computing the city mpg based on cylinder of the car
Given dataset ‘mpg’ that contains the mpg highway values stored with key ‘hwy’ and vehicle class values stored with key ‘class’, find the average highway mpg according to class of vehicle.
- see attached
How do you create as datetime stamp for ‘dtnow’ that represents the current date and time?
Hint: Need to convert since the epoch
- See attached
How do you create a time delta of 100 days?
How would you get 100 days ago from today?
- delta = dt.timedelta(days = 100)
- today = dt.date.today()
- print(today - delta)
How do you create a lamdba expression?
- Lambda syntax:
- Declare a lambda function with the word lambda (lamda)
- provide list of arguments (a, b, c)
- followed by colon ( : )
- ended with a single expression (a + b)
- You can then call the lambda function with parameters
- Example:
- my_function = lambda a,b,c : a + b
- my_function(1, 2, 3)
- returns 3
How would you achieve the following by instead using list comprehension?
def times_tables():
lst = []
for i in range(10):
for j in range(10):
lst.append(i*j)
return lst
times_tables() == [j*i for i in range(10) for j in range(10)]