Py APP1 (Py.Ud.10Apps) Flashcards

1
Q

how to load a json file into a dict?

data.json into a1_dict

A

prerequisite: load json module
]] import json
]] a1_dict = json.load( open( ‘files/data.json’) )

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

in case of a key error, how to provide a user-friendly error message instead of a traceback?

A

– use get() to retrieve the value from the dict and an if-else to treat input errors

https://realpython.com/python-keyerror/
ages = {‘Jim’: 30, ‘Pam’: 28, ‘Kevin’: 33}
person = input(‘Get age for: ‘)
age = ages.get(person)

if age:
print( f’{person} is {age} years old.’ )
else:
print( f”{person}’s age is unknown.” )

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

name a module which is useful in comparing similarities between textstrings

A

difflib

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

name two of its modules and describe how they work

A

sequencematcher( ‘None’, word, string ).ratio( )
– will provide the ratio of similarity, over 0.6 is strong
get_close_matches( ‘word’, [ dictwords to compare against ], , ratio threshold )

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