An Introduction to Python – Part III Dr. Nancy Warter-Perez.

Slides:



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

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
File I/O Dr. Nancy Warter-Perez. Introduction to Python – Part II2 Homework Submission Guidelines Submit your programs via
An Introduction to Python – Part III Dr. Nancy Warter-Perez.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez June 23, 2005.
An Introduction to Python – Part II Dr. Nancy Warter-Perez June 15, 2005.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
An Introduction to Python – Part III Dr. Nancy Warter-Perez May 1, 2007.
An introduction to Python and its use in Bioinformatics Csc 487/687 Computing for Bioinformatics Fall 2005.
Developing Pairwise Sequence Alignment Algorithms Dr. Nancy Warter-Perez May 10, 2005.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
UNIX Filters.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
A Review of C++ Dr. Nancy Warter-Perez June 16, 2003.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Built-in Data Structures in Python An Introduction.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
File I/O Michael Ernst UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
5 1 Data Files CGI/Perl Programming By Diane Zak.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Course A201: Introduction to Programming 09/30/2010.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()
Chapter 10 Loops: while and for CSC1310 Fall 2009.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
Lecture 4 Python Basics Part 3.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
DAY 3. ADVANCED PYTHON PRACTICE SANGREA SHIM TAEYOUNG LEE.
CSI 32 Lecture 20 Chapter 8 Input, Output, and Files 8.1 Standard Input and Output (review) 8.2 Formatted Strings 8.3 Working with Files 8.5 Case Studies.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Manipulating MATLAB Matrices Chapter 4
CSc 120 Introduction to Computer Programing II
Lecture 24: print revisited, tuples cont.
Introduction to Scripting
Basic Python Collective variables (sequences) Lists (arrays)
Introduction to Computer Science
Presentation transcript:

An Introduction to Python – Part III Dr. Nancy Warter-Perez

Introduction to Python – Part III2 Overview Assignments Solution to Programming Workshop #2 2-D Lists List comprehensions Zip File I/O Split Functions Programming Workshop #3

Introduction to Python – Part III3 Solution to Programming Workshop 2 # Script to calculate %GC of a sequence of nucleotides. # Inputs: sequence, window size # Outputs: nucleotide number, %GC for each window # Written by: Nancy Warter-Perez # Date created: April 22, 2004 # Last modified: print("Script for computing %GC.") seq = raw_input("Please enter sequence: ") winsize = input("Please enter window size: ")

Introduction to Python – Part III4 Solution to Programming Workshop 2 print("nucleotide\t%GC") for i in range(0, len(seq)-winsize+1): cnt=0 for j in seq[i:i+winsize]: if(j=='G' or j=='C' or j=='g' or j == 'c'): cnt+=1 gc = (cnt*100.0)/winsize print"%i\t\t%.2f"% ((i+1+winsize/2),gc) x = raw_input("\n\nPlease enter any character to exit.\n")

Introduction to Python – Part III5 Creating 2-D Lists To create a 2-D list L, with C columns and R rows initialized to 0: L = [[]] #empty 2-Dlist L = [[0 for col in range(C)] for row in range(R)] To assign the value 5 to the element at the 2 nd row and 3 rd column of L L[2][3] = 5

Introduction to Python – Part III6 Python List Comprehensions Precise way to create a list Consists of an expression followed by a for clause, then zero or more for or if clauses Ex: >>> [str(round(355/113.0, i)) for i in range(1,6)] ['3.1', '3.14', '3.142', '3.1416', ' '] Ex: replace all occurrences or G or C in a string of amino acids with a 1 and A and T with a 0 >>> x = "acactgacct" >>> y = [int(i=='c' or i=='g') for i in x] >>> y [0, 1, 0, 1, 0, 1, 0, 1, 1, 0]

Introduction to Python – Part III7 Zip – for parallel traversals Visit multiple sequences in parallel Ex: >>> L1 = [1,2,3] >>> L2 = [5,6,7] >>> zip(L1, L2) [(1,5), (2,6), (3,7)] Ex: >>> for(x,y) in zip(L1, L2): …print x, y, '--', x+y

Introduction to Python – Part III8 More on Zip Zip more than two arguments and any type of sequence Ex: >>> T1, T2, T3 = (1,2,3),(4,5,6),(7,8) >>> T3 (7,8) >>> zip(T1, T2, T3) [(1,4,7),(2,5,8)] -- truncates to shortest sequence

Introduction to Python – Part III9 Dictionary Construction with zip Ex: >>> keys = ['a', 'b', 'd'] >>> vals = [1.8, 2.5, -3.5] >>> hydro = dict(zip(keys,vals)) >>> hydro {'a': 1.8, 'b': 2.5, 'd': -3.5}

Introduction to Python – Part III10 File I/O To open a file myfile = open('pathname', ) modes: 'r' = read 'w' = write Ex: infile = open("D:\\Docs\\test.txt", 'r') Ex: outfile = open("out.txt", 'w') – in same directory

Introduction to Python – Part III11 Common input file operations OperationInterpretation input = open ('file', 'r')open input file S = input.read()read entire file into string S S = input.read(N)Read N bytes (N>= 1) S = input.readline()Read next line L = input.readlines()Read entire file into list of line strings

Introduction to Python – Part III12 Common output file operations OperationInterpretation output = open('file', 'w')create output file output.write(S)Write string S into file output.writelines(L)Write all line strings in list L into file output.close()Manual close (good habit)

Introduction to Python – Part III13 Extracting data from string – split String.split([sep, [maxsplit]]) - Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, it specifies a string to be used as the word separator. The optional third argument maxsplit defaults to 0. If it is nonzero, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements).

Introduction to Python – Part III14 Split Ex: >>> x = "a,b,c,d" >>> x.split(',')['a', 'b', 'c', 'd'] >>> x.split(',',2)['a', 'b', 'c,d'] Ex: >>> y = "533a4" >>> y.split()['5', '33', 'a', '4']

Introduction to Python – Part III15 Functions Function definition def adder(a, b, c): return a+b+c Function calls adder(1, 2, 3) -> 6

Introduction to Python – Part III16 Functions – Polymorphism >>>def fn2(c): …a = c * 3 …return a >>> print fn2(5) 15 >>> print fn2(1.5) 4.5 >>> print fn2([1,2,3]) [1,2,3,1,2,3,1,2,3] >>> print fn2("Hi") HiHiHi

Introduction to Python – Part III17 Functions - Recursion def fn_Rec(x): if x == []: return fn_Rec(x[1:]) print x[0], y = [1,2,3,4] fn_Rec(y) >>>

Introduction to Python – Part III18 Programming Workshop #3 Write a program to prompt the user for a scoring matrix file name and read the data into a dictionary ftp://ftp.ncbi.nih.gov/blast/matrices/