• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 1 out of 4 pages
Exam (elaborations)

D335 Python Endgame Fun-lets Questions and Complete Solutions Graded A+

Document preview thumbnail
Preview 1 out of 4 pages

D335 Python Endgame Fun-lets Questions and Complete Solutions Graded A+ Convert pairs of values in a csv row into a dictionary - 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. - Answer: with open(file, 'r') as f: dic = {} lines = ines() for line in lines: dic[line[0]] = () Given a ten-digit integer, isolate the first three, next three and last four digits using floor and modulo. - Answer: #shear right 7 digits first_three = ten_digits // #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 - Answer: original_string = '' formatted_string = original_string[:3] + '-' + original_string[3:5] + '-' + original_string[5:] print(formatted_string) Load the contents of a csv file into memory - Answer: import csv with open('', 'r') as f: csv_reader = r(f) Manipulate text from type() method - Answer: my_string = '' type(my_string).__name__ type_object = type(my_string) type_string = str(type_object) type_name = type_("'")[1] Nest try-except blocks to print errors in a script that divides two integers such that the input of a zero as the divisor or a decimal number prints an error, the latter of which includes the error returned by the interpreter. - Answer: try: inp1 = int(input()) inp2 = int(input()) try: print(int(inp1 / inp2)) except ZeroDivisionError: print(0 error message) except ValueError as v: print(float error message, v) Open my_ and create a list of lines with n removed - Answer: with open('my_') as f: lines = [() for line in ines()] Print a Fibonacci number from the Fibonacci sequence up to the input integer. Assume all inputs 0 - 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. - Answer: if k in dic: return dic[k] else: raise EntryNotFoundError(f'Value not found for {k}') for k, v in (): if v == value: return k else: raise EntryNotFoundError(f'Key not found for {v}') try: except EntryNotFoundError as excpt: print('optional message: ', ge) Replace section of text in my_ - Answer: file_name = 'my_' with open(file_name, 'r+') as f: content = () content = ce('_','_') (0) (content) ate() Sort/clean dictionary - Answer: sorted_dic = {key: dic[key] for key in sorted(dic)} cleaned_dict = {(): () for key, value in my_()}

Content preview

D335 Python Endgame Fun-lets
Questions and Complete Solutions
Graded A+
Convert pairs of values in a csv row into a dictionary - 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. - Answer:
with open(<file>, 'r') as f:

dic = {}

lines = f.readlines()

for line in lines:

dic[line[0]] = line.split()



Given a ten-digit integer, isolate the first three, next three and last four digits using floor and modulo. -
Answer: #shear right 7 digits

first_three = ten_digits // 10000000



#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 - Answer: original_string = '123456789'



formatted_string = original_string[:3] + '-' + original_string[3:5] + '-' + original_string[5:]

Document information

Uploaded on
March 26, 2025
Number of pages
4
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$14.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
YourExamplug
4.1
(74)
Sold
228
Followers
27
Items
18118
Last sold
4 days ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions