python_fundamentals Flashcards

1
Q

UTF

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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
A
  • 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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Based on the following dictionary, how would you print the key value pairs?

  • x = {‘Christopher Brooks’: ‘brooksc@umich.edu’, ‘Bill Gates’: ‘billg@microsoft.com’}
A
  • for key,value in x.items():
    print(key)
    print(value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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’?

A

import csv

%precision 2

with open(mpg.csv) as csvfile:
 mpg = list(csv.DictReader(csvfile))

print(mpg[:3])

print(len(mpg))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
  • mpg[0].keys()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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’

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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’

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.

A
  • see attached
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you create as datetime stamp for ‘dtnow’ that represents the current date and time?

Hint: Need to convert since the epoch

A
  • See attached
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a time delta of 100 days?

How would you get 100 days ago from today?

A
  • delta = dt.timedelta(days = 100)
  • today = dt.date.today()
  • print(today - delta)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create a lamdba expression?

A
  • Lambda syntax:
    1. Declare a lambda function with the word lambda (lamda)
    2. provide list of arguments (a, b, c)
    3. followed by colon ( : )
    4. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

times_tables() == [j*i for i in range(10) for j in range(10)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly