



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Convert |! pairs |! of |! values |! in |! a |! csv |! row |! into |! a |! dictionary |! - |! Correct |! answer |! ✔for |! row |! in |! csv_reader: |! |! row_dict |! = |! dict(zip(row[::2], |! row[1::2])) |! print(row_dict) Extract |! words |! listed |! alphabetically |! by |! line |! from |! a |! text |! file |! and |! create |! a |! dictionary |! with |! the |! first |! letter |! of |! the |! words |! comprising |! the |! key, |! and |! the |! words |! themselves |! in |! a |! list |! as |! the |! corresponding |! value. |! - |! Correct |! answer |! ✔with |! open(
#shear |! right |! 4 |! digits |! then |! shear |! left |! 3 |! digits |! next_three |! = |! (ten_digits |! // |! 10000) |! % |! 1000 #shear |! left |! 7 |! digits last_four |! = |! ten_digits |! % |! 10000 Insert |! SSN-style |! dashes |! - |! Correct |! answer |! ✔original_string |! = |! '123456789' formatted_string |! = |! original_string[:3] |! + |! '-' |! + |! original_string[3:5] |! + |! '-' |! + |! original_string[5:] print(formatted_string) Load |! the |! contents |! of |! a |! csv |! file |! into |! memory |! - |! Correct |! answer |! ✔import |! csv with |! open('data.csv', |! 'r') |! as |! f: |! csv_reader |! = |! csv.reader(f) Manipulate |! text |! from |! type() |! method |! - |! Correct |! answer |! ✔my_string |! = |! '' type(my_string).name type_object |! = |! type(my_string)
Print |! a |! Fibonacci |! number |! from |! the |! Fibonacci |! sequence |! up |! to |! the |! input |! integer. |! Assume |! all |! inputs |! > |! 0 |! - |! Correct |! answer |! ✔a, |! b |! = |! 0, |! 1 for |! r |! in |! range(2, |! int(input())): |! a, |! b |! = |! b, |! a |! + |! b print(a |! + |! b) Raise |! exceptions |! for |! searching |! a |! dictionary |! and |! output |! the |! exception |! message. |! Assume |! the |! dictionary |! and |! a |! class |! to |! create |! the |! exception |! message |! object |! are |! already |! provided. |! - |! Correct |! answer |! ✔if |! k |! in |! dic: |! return |! dic[k] else: |! raise |! EntryNotFoundError(f'Value |! not |! found |! for |! {k}') for |! k, |! v |! in |! dic.items(): |! if |! v |! == |! value: |! return |! k |! else: |! raise |! EntryNotFoundError(f'Key |! not |! found |! for |! {v}') try: |! except |! EntryNotFoundError |! as |! excpt: |! print('<optional |! message>: |! ', |! excpt.message)
Replace |! section |! of |! text |! in |! my_file.txt |! - |! Correct |! answer |! ✔file_name |! = |! 'my_file.txt' with |! open(file_name, |! 'r+') |! as |! f: |! content |! = |! f.read() |! content |! = |! content.replace('_photo.jpg','_info.txt') |! f.seek(0) |! f.write(content) |! f.truncate() Sort/clean |! dictionary |! - |! Correct |! answer |! ✔sorted_dic |! = |! {key: |! dic[key] |! for |! key |! in |! sorted(dic)} cleaned_dict |! = |! {key.strip(): |! value.strip() |! for |! key, |! value |! in |! my_dict.items()}