























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")
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
myFile = open("new.txt", "w")
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.
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
(also of course print() goes to the screen and
if (seq.startswith("C")): ... print "Starts with C" ... Starts with C
if (
if (seq.startswith("C")):
... print "Starts with C"
File "
IndentationError: expected an indented block
if (seq.startswith("C")): ... print "Starts with C" ... print "All right by me!" File "
", line 4 print "All right by me!" ^ SyntaxError: invalid syntax
This is why I prefer to use the
< 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
Beware!
= versus ==
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
if (((x <= y) and (x < z)) or ((x == y) and not (x == z)))
Can be read this way:
if test1 is true then run block1, else if test2 is true run block2, else run block
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