[CS] CS50 Week 6: Python

Penny Ng
3 min readNov 13, 2020

Key Points

  • Python basics
  • Demonstration of converting previous assignments from C to Python
  • Files
  • Features that C does not have

Assignment: DNA

I broke down this programme into a few small functions, dealing with the DNA database, the input DNA sequences, and the computation of DNA results respectively.

Main

At the beginning of this programme, it checks the number of command-line arguments first. If the user does not pass 3 arguments on the command line, this programme exits.

If 3 command-line arguments are passed, this programme reads the database by calling function read_db(). The second command-line argument, supposed to be the file name of a database, is passed to this function. read_db() reads the database and save all data to a global variable db_dict.

Then, it gets a list of DNA result by calling function find_dna(). The third command-line argument, supposed to be the file name of a DNA sequence, is passed to the function along with a list of STRs.

With the DNA result, it identifies if any value from DNA database matches the DNA result.

Database

functionread_db() takes an argument. The filename of the database file got back from the command line should be passed into this function.

After opening the CSV file, it reads data from it into a global variable db_dict.

db_dict is a dictionary containing people’s name as keys and their corresponding DNA data list as values except for the first key-value pair, which are column titles of the raw data.

Penny Ng

A passionate self-taught  iOS developer. Write to record my learning and share the knowledge. Twitter @pennyngpw