>> if (seq.startswith("C")): print "Starts with C" ... Starts with C >>> A block is a group of lines of code that belong together. if ( Download presentation Presentation is loading. Please wait.
1
Conditionals (if-then-else)
2
The if statement >>> seq = "Class"
3
The if statement Try doing an if statement without indentation.
4
Multiline blocks Try doing an if statement with multiple lines in the block. >>> if (seq.startswith("C")): ... print "Starts with C" ... print "All right by me!" ... Starts with C All right by me!
5
Multiline blocks What happens if you don’t use the same number of spaces to indent the block? >>> if (seq.startswith("C")): ... print "Starts with C" print "All right by me!" File "<stdin>", line 4 print "All right by me!" ^ SyntaxError: invalid syntax
6
Comparison operators Boolean: and, or, not
7
Examples seq = 'CAGGT' >>> if ('C' == seq[0]): ... print 'C is first' ... C is first >>> if ('CA' in seq): ... print 'CA in', seq CA in CAGGT >>> if (('CA' in seq) and ('CG' in seq)): ... print "Both there!" >>>
8
Beware! = versus == Single equal assigns a variable name.
9
Evaluates to FALSE: no print.
10
if-elif-else if <test1>: <statement> elif <test2>:
11
Example >>> base = 'C' >>> if (base == 'A'):
12
<file> = open(<filename>, r|w|a>
13
Sample problem #1 Write a program is-equal.py that checks whether the first two command line arguments are the same. > python is-equal.py foo foo Yes > python is-equal.py foo bar No My solution has 5 lines.
14
Solution #1 import sys if (sys.argv[1] == sys.argv[2]): print "Yes” else: print "No"
15
Sample problem #2 Modify is-equal.py so that it issues an error message if an incorrect command line is given. > python is-equal.py foo foo Yes > python is-equal.py foo Error: Expected 2 values but found 1. > python is-equal.py Error: Expected 2 values but found 0.
16
Solution #2 import sys if (len(sys.argv) != 3): print "Expected 2 values but found %d." % (len(sys.argv) - 1) elif (sys.argv[1] == sys.argv[2]): print "Yes” else: print "No" My solution has 7 lines.
17
Sample problem #3 Write a program find-base.py that takes as input a DNA sequence and a nucleotide. The program should print the number of times the nucleotide occurs in the sequence, or a message saying it’s not there. > python find-base.py A GTAGCTA A occurs at position 3. > python find-base.py A GTGCT A does not occur at all. My solution has 8 lines. Hint: S.find('G') returns -1 if it can't find the requested sequence.
18
Solution #3 import sys base = sys.argv[1] sequence = sys.argv[2] position = sequence.find(base) if (position == -1): print base, "does not occur at all" else: print base, "occurs at position %d." % position
19
Reading Chapter 5 of Think Python (1st edition) by Allen B. Downey.
Similar presentations © 2025 SlidePlayer.com. Inc. Log in
Similar presentations
Presentation on theme: "Conditionals (if-then-else)"— Presentation transcript:
Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble
>>> if (seq.startswith("C")): ... print "Starts with C" ... Starts with C >>> A block is a group of lines of code that belong together. if (<test evaluates to true>): <execute this block of code> In the Python interpreter, the ellipse indicates that you are inside a block. Python uses indentation to keep track of blocks. You can use any number of spaces to indicate blocks, but you must be consistent. An unindented or blank line indicates the end of a block.
>>> if (seq.startswith("C")): ... print "Starts with C" File "<stdin>", line 2 print "Starts with C" ^ IndentationError: expected an indented block
Numeric: < , > , ==, !=, <>, >=, <= String: in
>>> myString == "foo" Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'myString' is not defined >>> myString = "foo" True Double equal tests for equality. >>> if (myString = "foo"): File "<stdin>", line 1 if (myString = "foo"): ^ SyntaxError: invalid syntax >>> if (myString == "foo"): ... print "Yes!" ... Yes!
if-else statements if <test1>: <statement> else: The else block executes only if <test1> is false. >>> if (seq.startswith('T')): ... print 'T start' ... else: ... print 'starts with', seq[0] ... starts with C >>> Evaluates to FALSE: no print.
elif block executes if <test1> is false and then performs a second <test2>
... print "adenine" ... elif (base == 'C'): ... print "cytosine" ... elif (base == 'G'): ... print "guanine" ... elif (base == 'T'): ... print "thymine" ... else: ... print "Invalid base!“ ... cytosine
<string> = <file>.read() <string> = <file>.readline() <string list> = <file>.readlines() <file>.write(<string>) <file>.close() Boolean: and, or, not Numeric: < , > , ==, !=, <>, >=, <= String: in, not in if <test1>: <statement> elif <test2>: else:
Similar presentations
All rights reserved.