For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.

Slides:



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

I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Genome Sciences 373 Genome Informatics Quiz Section 3 April 14, 2015.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
An introduction to Python and its use in Bioinformatics Csc 487/687 Computing for Bioinformatics Fall 2005.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Course A201: Introduction to Programming 09/30/2010.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Counting Loops.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
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.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Topic: Iterative Statements – Part 1 -> for loop
CS1022 Computer Programming & Principles
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Loops BIS1523 – Lecture 10.
Introduction to Python
(optional - but then again, all of these are optional)
Python: Control Structures
Chapter 5 - Control Structures: Part 2
(optional - but then again, all of these are optional)‏
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.
Programming for Engineers in Python
Agenda Control Flow Statements Purpose test statement
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Conditionals (if-then-else)
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Topics Introduction to File Input and Output
Loop Structures and Booleans Zelle - Chapter 8
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Iteration: Beyond the Basic PERFORM
I210 review.
More Looping Structures
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.
More elements of Python programs
Introduction to Strings
CHAPTER 6: Control Flow Tools (for and while loops)
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Computer Science
Introduction to Strings
“Everything Else”.
More Looping Structures
Topics Introduction to File Input and Output
Introduction to Strings
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Introduction to Computer Science
Introduction to Python
Presentation transcript:

for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is a lot of material for one class. Most everyone was confused and unable to solve the sample problems.

Review Use if - elif - else statements for conditional code blocks. Use the logical operators (==, !=, <=, etc.). Code blocks share the same indentation. Indexing always uses []: myString[0] Functions always use (): len(myString)

for loop Allows you to perform an operation on each element in a list. for <target> in <object>: <statement> ... Variable name available inside loop Must be defined in advance Must be indented Repeated block of code

Try it … >>> for name in ["Andrew", "Teboho", "Xian"]: ... print "Hello", name ... Hello Andrew Hello Teboho Hello Xian >>>

Multiline blocks Each line must have the same indentation. >>> for integer in [0, 1, 2]: ... print integer ... print integer * integer ... 1 2 4

Looping on a string >>> DNA = 'AGTCGA' >>> for base in DNA: ... print "base =", base ... base = A base = G base = T base = C

The increment operation is so common there is a shorthand: index += 1 Use an integer variable to keep track of a numeric index during looping. >>> index = 0 >>> for base in DNA: ... print "base", index, "is", base ... index = index + 1 ... base 0 is A base 1 is G base 2 is T base 3 is C base 4 is G base 5 is A >>> print "The sequence has", index, "bases" The sequence has 6 bases The increment operation is so common there is a shorthand: index += 1

The range() function returns a list of integers covering a specified range range([start,] stop [,step]) [optional arguments], default to 0 and 1 range(5) [0, 1, 2, 3, 4] range(2,8) [2, 3, 4, 5, 6, 7] >>> range(-1, 2) [-1, 0, 1] >>> range(0, 8, 2) [0, 2, 4, 6] >>> range(0, 8, 3) [0, 3, 6] >>> range(6, 0, -1) [6, 5, 4, 3, 2, 1]

Using range() in a for loop >>> for index in range(0,4): ... print index, "squared is", index * index ... 0 squared is 0 1 squared is 1 2 squared is 4 3 squared is 9 range() produces a list of integers, so this is really looping over a list.

Nested loops >>> for first in [1, 2, 3]: ... for second in [4, 5]: ... print first * second ... 4 5 8 10 12 15

Nested loops [[0.5, 1.3], [1.7, -3.4], [2.4, 5.4]] Column one Row zero >>> matrix = [[0.5, 1.3], [1.7, -3.4], [2.4, 5.4]] >>> for row in range(0, 3): ... print "row = ", row ... for column in range(0, 2): ... print matrix[row][column] ... row = 0 0.5 1.3 row = 1 1.7 -3.4 row = 2 2.4 5.4 >>> [[0.5, 1.3], [1.7, -3.4], [2.4, 5.4]] Row zero Column one

Terminating a loop Break: Jumps out of the closest enclosing loop >>> for integer in range(0,3): ... if (integer == 1): ... break ... print integer ...

Terminating a loop Continue: Jumps to the top of the closest enclosing loop >>> for integer in range(0, 3): ... if (integer == 1): ... continue ... print integer ... 2

Summary for <target> in <object>: <block> range(<start>, <stop>, <increment>) break – Jump out of a loop continue – Jump to the next loop iteration. Perform <block> for each element in <object>. Define a list of numbers. <start> and <increment> are optional.

Sample problem #1 Write a program add-arguments.py that reads any number of integers from the command line and prints the cumulative total for each successive argument. > python add-arguments.py 1 2 3 1 3 6 > python add-arguments.py 1 4 -1 5 4 My solution has 6 lines.

Solution #1 import sys total = 0 for argument in sys.argv[1:]: Skip the program name import sys total = 0 for argument in sys.argv[1:]: integer = int(argument) total = total + integer print total

Sample problem #2 Write a program word-count.py that prints the number of words on each line of a given file. > cat hello.txt Hello, world! How ya doin’? > python count-words.py 2 3 Don't worry about punctuation; just assume white-space-separated strings are words. My solution has 8 lines.

Solution #2 import sys filename = sys.argv[1] myFile = open(filename, "r") myLines = myFile.readlines() for line in myLines: words = line.split() print len(words) myFile.close()

Sample problem #3 Write a program count-letters.py that reads a file and prints a count of the number of letters in each word. > python count-letters.py hello.txt 6 3 2 My solution has 8 lines.

Solution #3 import sys filename = sys.argv[1] myFile = open(filename, "r") myLines = myFile.readlines() for line in myLines: for word in line.split(): print len(word) myFile.close()

Sample problem #4 (optional) Write a program average.py that reads a text file containing numeric values and computes the average score on each line. Download the example "matrix.txt" from the course web page. > python average.py matrix.txt 2.17 4.05 5.25 5.59 etc. My solution has 10 lines.

Solution #4 import sys my_file = open(sys.argv[1], "r") for line in my_file: sum = 0.0 num_values = 0 for value in line.split(): sum += float(value) num_values += 1 print "%g" % (sum / num_values) my_file.close()

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