While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.

Slides:



Advertisements
Similar presentations
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Advertisements

While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
16-May-15 Sudden Python Drinking from the Fire Hose.
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Pseudocode and Algorithms
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Loops – While, Do, For Repetition Statements Introduction to Arrays
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Basic Elements of C++ Chapter 2.
Fundamentals of Python: From First Programs Through Data Structures
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
An Introduction to Textual Programming
General Programming Introduction to Computing Science and Programming I.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
For loops in programming Assumes you have seen assignment statements and print statements.
Course A201: Introduction to Programming 09/16/2010.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Introduction to Computing Science and Programming I
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction To Repetition The for loop
Repetition Structures Chapter 9
The switch Statement, and Introduction to Looping
(optional - but then again, all of these are optional)
Basic Elements of C++.
(optional - but then again, all of these are optional)‏
ECS10 10/10
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
The Selection Structure
Programming for Engineers in Python
Web Programming– UFCFB Lecture 16
Python - Loops and Iteration
Engineering Innovation Center
Basic Elements of C++ Chapter 2.
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Conditionals (if-then-else)
Arrays, For loop While loop Do while loop
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
TIPS: How to Be Successful
T. Jumana Abu Shmais – AOU - Riyadh
Loops CIS 40 – Introduction to Programming in Python
CS 100: Roadmap to Computing
String and Lists Dr. José M. Reyes Álamo.
CMPT 102 Introduction to Scientific Computer Programming
Fundamentals of visual basic
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.
Sequence comparison: Local alignment
CHAPTER 6: Control Flow Tools (for and while loops)
Functions continued.
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Computer Science
Topics Introduction to File Input and Output
Class code for pythonroom.com cchsp2cs
Presentation transcript:

while loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble

for loop review for <element> in <object>: don’t forget the colon! for <element> in <object>: <statement> . . . <last statement> block of code <element> is a newly created variable name. You can access the variable after the loop completes. <object> is a container of one or more <element>s. It must already exist. range() will make a list of integers “on the fly” for index in range(0,100): <statement>

Choosing good variable names Pick names that are descriptive. Change a name if you decide there’s a better choice. (Use search and replace to be sure you don't miss any.) Very locally used names can be short and arbitrary The Python standard recommends using internal underscores rather than camelCase. list_of_lines = my_file.readlines() sequence = "GATCTCTATCT" my_DP_matrix = [[0,0,0],[0,0,0],[0,0,0]] sum = 0 for i in range(len(list_of_ints)): sum = sum + list_of_ints[i] (more code)

Comment liberally Any place a # sign appears, the rest of the line is a comment (ignored by program). Blank lines are also ignored – use them to visually group code. import sys query = sys.argv[1] my_file = open(sys.argv[2], "r") lines = my_file.readlines() # put all the lines from a file into a list # now process each file line to remove the \n character, then # search the line for query and record each result in a list of ints int_list = [] for line in lines: position = line.find(query) int_list.append(position) etc.

while loop statement1 statement2 while (<test>): In English: While some logical test is true, continue executing the block of statements.

What does this program do? sum = 0 count = 1 while (count < 3): sum = sum + count count = count + 1 print count print sum

What does this program do? sum = 0 count = 1 while (count < 3): sum = sum + count count = count + 1 print count print sum sum = 0 count = 1 count < 3? TRUE sum = 0 + 1 = 1 count = 1 + 1 = 2 sum = 1 + 2 = 3 count = 2 + 1 = 3 count < 3? FALSE 3 3

Increment operator (+=) x = x + y is the same as x += y sum = 0 count = 1 while (count < 3): sum = sum + count count = count + 1 print count print sum sum = 0 count = 1 while (count < 3): sum += count count += 1 print count print sum

For versus while loops You will probably use for loops more. For is natural to loop through a list, characters in a string, etc. (anything of determinate size). While is natural to loop an indeterminate number of times until some condition is met.

Examples of for loops for base in sequence: <do something with each base> for sequence in database: <do something with each sequence> for index in range(5,200): <do something with each index>

Examples of while loops while (error > 0.05): <do something that will reduce error> while (score > 0): <traceback through a DP matrix, each time setting the current score> Warning: if you write a while loop and the conditional test stays True, the loop will run forever (infinite loop).

Terminating a loop while loops use continue and break in the same way as for loops: continue : jumps to the top of the enclosing loop break : breaks completely out of the enclosing loop

Sample problem #1 Convert the following program to a while loop: import sys total = 0 for i in range(1, len(sys.argv)): total += int(sys.argv[i]) print total Run both programs with arguments “1 2 3”.

Solution #1 import sys total = 0 i = 1 while (i < len(sys.argv)) : total += int(sys.argv[i]) i += 1 print total

Sample problem #2 Write a python program to print all the square numbers up to a user- specified maximum: > python print-squares.py 20 1 4 9 16 My solution has 6 lines.

Solution #2 import sys max_int = int(sys.argv[1]) index = 1 while (index * index < max_int): print index * index index += 1

Sample problem #3 Write a python program to count the lines in a file using readline() and a while loop. > python count-lines.py myfile.txt 3 My solution has 8 lines.

Solution #3 import sys myfile = open(sys.argv[1], “r”) lineCount = 0 line = myfile.readline() while (line != ""): lineCount += 1 print lineCount

Sample problem #4 Write a program count-fasta.py that counts the number of fasta sequences in a file specified on the command line. Use either a while loop or a for loop. python count-fasta.py small.fasta 72 23516 Run your program on the two files on the course web page: small.fasta and large.fasta. Fasta format: >identifier1 [optional comments] AAOSIUBOASIUETOAISOBUAOSIDUGOAIBUOABOIUAS AOSIUDTOAISUETOIGLKBJLZXCOITLJLBIULEIJLIJ >identifier2 [optional comments] TXDIGSIDJOIJEOITJOSIJOIGJSOIEJTSOE >identifier3 Etc. sequence on any number of lines until next line that starts with “>”

Solution #4 Not required, but a good habit to get into import sys # Make sure we got an argument on the command line. if (len(sys.argv) != 2): print "USAGE: one file argument required" sys.exit() # Open the file for reading. fasta_file = open(sys.argv[1], "r") num_seqs = 0 for line in fasta_file: # Increment if this is the start of a sequence if (line[0] == ">"): # parentheses are optional num_seqs += 1 fasta_file.close() print num_seqs