Conditionals (if-then-else)

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
CS0007: Introduction to Computer Programming
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Conditional Statements Introduction to Computing Science and Programming I.
Genome Sciences 373 Genome Informatics Quiz Section 2 April 7, 2015.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Lists and the ‘ for ’ loop. Lists Lists are an ordered collection of objects >>> data = [] >>> print data [] >>> data.append("Hello!") >>> print data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Python Conditionals chapter 5
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
 Type Called bool  Bool has only two possible values: True and False.
Introduction to Python
CSc 120 Introduction to Computer Programing II Adapted from slides by
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
Introduction to Python
Topics The if Statement The if-else Statement Comparing Strings
Introduction to Python
Conditional Execution
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Numbers, lists and tuples
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Imperative Programming
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Introduction to Programming
Python - Conditional Execution
Conditional Execution
An Introduction to Python
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Introduction to Programming
Conditional Execution
ERRORS AND EXCEPTIONS Errors:
Control flow : if statements
Introduction to Python
Selection Statements.
More for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
If Statements.
CS 1111 Introduction to Programming Spring 2019
Advanced Python Concepts: Exceptions
Boolean Expressions to Make Comparisons
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Python
Advanced Python Concepts: Exceptions
CHAPTER 5: Control Flow Tools (if statement)
Lists and the ‘for’ loop
“Everything Else”.
The Selection Structure
Types, Truth, and Expressions
Introduction to Programming
Imperative Programming
Types, Truth, and Expressions (Part 2)
Control 9 / 25 / 19.
Presentation transcript:

Conditionals (if-then-else) Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble

The if statement >>> seq = "Class" >>> 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.

The if statement Try doing an if statement without indentation. >>> if (seq.startswith("C")): ... print "Starts with C" File "<stdin>", line 2 print "Starts with C" ^ IndentationError: expected an indented block

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!

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

Comparison operators Boolean: and, or, not Numeric: < , > , ==, !=, <>, >=, <= String: in

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!" >>>

Beware! = versus == Single equal assigns a variable name. >>> 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!

Evaluates to FALSE: no print. 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.

if-elif-else if <test1>: <statement> elif <test2>: elif block executes if <test1> is false and then performs a second <test2>

Example >>> 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

<file> = open(<filename>, r|w|a> <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:

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.

Solution #1 import sys if (sys.argv[1] == sys.argv[2]): print "Yes” else: print "No"

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.

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.

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.

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

Reading Chapter 5 of Think Python (1st edition) by Allen B. Downey.