Skip to main content

Statistics Python

1.

Question 1

A student wants to write into a file called myfile, without deleting its existing content. Which one of the following functions should he or she use?

  • f = open('myfile', '+')
  • f = open('myfile', 'rw')
  • f = open('myfile', 'a')
  • f = open('myfile', 'r')

2.

Question 2

Which of the following statements are true?

A) When you open a file for reading, if the file does not exist, an error occurs.

B) When you open a file for writing, if the file does not exist, a new file is created. 

C) When you open a file for reading, if the file does not exist, the program will open an empty file.

D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

E) When you open a file for writing, if the file does not exist, an error occurs.


  1. B, C, and D only
  2. A and B only
  3. A, B, and D only
  4. All of them are correct


Question 3

Examine the following three functions that take as argument a file name and return the extension of that file. For instance, if the file name is 'myfile.tar.gz' the returned value of the function should be 'gz'. If the file name has no extention, i.e. when the file name is just 'myfile', the function should return an empty string.


  • def get_extension1(filename):
  •     return(filename.split(".")[-1])

  • def get_extension2(filename):
  •     import os.path

  •     return(os.path.splitext(filename)[1]
  • def get_extension3(filename):
  •     return filename[filename.rfind('.'):][1:]

Which of the these functions are doing exactly what they are supposed to do according to the description above?

All of them.

get_extension3 only

get_extension2 and get_extension3

get_extension1 and get_extension3

Question 4

A student is running a Python program like this:

>python mergefasta.py myfile1.fa myfile2.fa

In the mergefasta.py program the following lines are present:

import sys

tocheck=sys.argv[1]

What is the value of the variable tocheck?

myfile2.fa

mergefasta.py

'myfile1.fa'

'myfile2.fa'


Question 5

A student launches the Python interpreter from his home directory. His home directory contains another directory called 'mydir',  and 'mydir' contains two files called 'foo' and 'bar'. The home directory does not contain any files, only other directories.  What will happen when he writes the following code at the Python prompt:>>> import os

>>> filenames = os.listdir('mydir')

>>> f= open(filenames[0])


1 / 1 point


A variable f representing a file object will be created, and the first file in the directory 'mydir' will be opened.

An error will be produced stating that the file to be opened does not exist.

A variable f representing a file object will be created, and the first file in the directory 'mydir' will be opened for reading in text mode.

An error will be produced stating that filename is not subscriptable.


 Solutions week 4 quiz 
Here are the answers to your questions:

  • The correct function to use when a student wants to write into a file called ‘myfile’, without deleting its existing content is f = open('myfile', 'a'). This opens the file for appending, which means new data will be added to the end of the file without deleting its existing content.\
  • The true statements are:
A) When you open a file for reading, if the file does not exist, an error occurs.
B) When you open a file for writing, if the file does not exist, a new file is created.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
So, the correct option is A, B, and D only.
  • The functions get_extension1 and get_extension3 are doing exactly what they are supposed to do according to the description above. The function get_extension2 returns the extension with a dot (e.g., ‘.gz’), which is not required according to the problem statement.
  • The value of the variable tocheck will be 'myfile1.fa'. In Python, sys.argv is a list in Python, which contains the command-line arguments passed to the script. With the help of sys.argv, the Python script can be made to act like a command line program. sys.argv[0] is the name of the script itself, sys.argv[1] will be 'myfile1.fa' and so on.
  1. An error will be produced stating that the file to be opened does not exist. This is because os.listdir('mydir') will return a list of filenames in the directory ‘mydir’, but when trying to open the file with open(filenames[0]), Python will look for the file in the current working directory (which is the home directory), not in ‘mydir’. To open the file correctly, the code should be f = open('mydir/' + filenames[0]).

Comments

Popular posts from this blog

 Genomics_command_line_quiz1 For all projects, you may use your own Unix-based system and, where applicable, ensure that you are running the version of the software specified in the assignments. Alternatively, you may use the VMBox virtual machine environment provided with the course materials. Instructions on how to download and use the environment can be found on the course web site. For the following questions, refer to the class workflow and use the data in the Online materials (‘gencommand_proj1_data.tar.gz’) to answer the questions. Assume you sequenced and assembled the genome of Malus domestica (apple), and performed gene annotation. You then collected samples and ran RNA-seq experiments to determine sets of genes that are expressed in the various tissues. This information was stored, respectively, in the following files: “apple.genome”, “apple.genes”, “apple.condition{A,B,C}”. NOTE: The apple genome and the apple gene annotations for this project were extracted from the Rosace

Immunotherapy

 

Introduction to Molecular Biology

 Introduction to Molecular Biology Cells are fundamental building blocks of living organisms. Cells contain a nucleus, mitochondria and chloroplasts, endoplasmic reticulum, ribosomes, vacuoles, etc.  The nucleus is important organelle because it houses chromosomes which include the DNA.  The DNA is in essence a blueprint of the organism as it encodes information needed to synthesize proteins . Molecular biologist s would like to understand how human biology works with the hope to treat diseases like cancer. One can look at simpler organisms such as yeasts to understand how human biology works.  Admittedly, unicellular yeasts are very different from humans who have approximately 1014 cells. However, the DNA is similar across all living organisms. For example, humans share 99% of DNA with chimps. Naturally, we would like to know what information contained in that 1% of DNA is so critical to determine all the distinguishing features of humans,  DNA            DNA stands for deoxyribonucle