
























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!

























myFile = open('hello.txt', 'r') (NB – this opens a file in the present working directory, you can also provide a full path)
myFile = open('hello.txt', 'r') myString = myFile.read() print myString Hello, world! How ya doin'?
myFile = open('hello.txt', 'r') myStringList = myFile.readlines() print myStringList ['Hello, world!\n', 'How ya doin'?\n'] print myStringList[1] How ya doin'? this file method returns a list of strings, one for each line in the file notice that each line has the newline character at the end
myFile = open('new.txt', 'w') # (or 'a')
myFile.write('This is a new file\n') myFile.close() Ctl-D (exit the python interpreter) cat new.txt This is a new file always close a file after you are finished reading from or writing to it. open('new.txt', 'w') will overwrite an existing file (or create a new one) open('new.txt', 'a') will append to an existing file
newFile.write('foo') newFile.write(1) Traceback (most recent call last): File "
", line 1, in? TypeError: argument 1 must be string or read-only character buffer, not int newFile.write(str(1)) # str converts to string (also of course print goes to the screen and .write() goes to a file)
if (seq.startswith("C")): ... print "Starts with C" ... Starts with C
if (seq.startswith("C")): ... print "Starts with C" File "
", line 2 print "Starts with C" ^ IndentationError: expected an indented block the interpreter expects you to be inside a code block (…) .write() does not automatically ### append a new-line character. - .write() requires a string as input. >>> newFile.write('foo') >>> newFile.write(1) Traceback (most recent call last): File " ", line 1, in? TypeError: argument 1 must be string or read-only character buffer, not int >>> newFile.write(str(1)) # str converts to string (also of course print goes to the screen and .write() goes to a file) ## The if statement >>> if (seq.startswith("C")): ... print "Starts with C" ... Starts with C >>> - A block is a group of lines of code that belong together. if ( ): - In the Python interpreter, the ellipsis … indicates that you are inside a block (on my Win machine it is just a blank indentation). - Python uses indentation to keep track of code blocks. - You can use any number of spaces to indent a block, but you must be consistent. Using one is simplest. - An unindented or blank line indicates the end of a block. ## The if statement - Try doing an if statement without indentation: >>> if (seq.startswith("C")): ... print "Starts with C" File " ", line 2 print "Starts with C" ^ IndentationError: expected an indented block the interpreter expects you to be inside a code block (…) but it is not indented properly
Comparison and logic operators
is greater than == is equal to != is NOT equal to <= is less than or equal to = is greater than or equal to
seq = 'CAGGT'
if ('C' == seq[0]): ... print 'C is first in', seq ... C is first in CAGGT if ('CA' in seq): ... print 'CA is found in', seq if (seq.startswith("C")): ... print "Starts with C" ... Starts with C >>> - A block is a group of lines of code that belong together. if (
): - In the Python interpreter, the ellipsis … indicates that you are inside a block (on my Win machine it is just a blank indentation). - Python uses indentation to keep track of code blocks. - You can use any number of spaces to indent a block, but you must be consistent. Using one is simplest. - An unindented or blank line indicates the end of a block. ## The if statement - Try doing an if statement without indentation: >>> if (seq.startswith("C")): ... print "Starts with C" File " ", line 2 print "Starts with C" ^ IndentationError: expected an indented block the interpreter expects you to be inside a code block (…) but it is not indented properly Comparison and logic operators - Boolean: and, or, not - Numeric: < , > , ==, !=, >=, <= - String: in, not in < is less than > is greater than == is equal to != is NOT equal to <= is less than or equal to >= is greater than or equal to ## Examples seq = 'CAGGT' >>> if ('C' == seq[0]): ... print 'C is first in', seq ... C is first in CAGGT >>> if ('CA' in seq): ... print 'CA is found in', seq ... CA is found in CAGGT if (('CA' in seq) and ('CG' in seq)): ... print "Both there!" ...
comparison operators the outer parenthesis pair is optional
x = 1 y = 2 z = 3 if ((x < y) and (y != z)): do something if ((x > y) or (y == z)): do something else Evaluation starts with the innermost parentheses and works out. When there are multiple parentheses at the same level, evaluation starts at the left and moves right. The statements can be arbitrarily complex. if (((x <= y) and (x < z)) or ((x == y) and not (x == z)))
if
if (seq.startswith('T')): ... print 'T start' ... else: ... print 'starts with', seq[0] ... starts with C
evaluates to FALSE base = 'C' if (base == 'A'): ... print "adenine" ... elif (base == 'C'): ... print "cytosine" ... elif (base == 'G'): ... print "guanine" ... elif (base == 'T'): ... print "thymine" ... else: ... print "Invalid base!" ... cytosine
if