An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
An Introduction to Python – Part II Dr. Nancy Warter-Perez June 15, 2005.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
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.
Chapter 2 Writing Simple Programs
An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez April 19, 2005.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
An Introduction to Python – Part III Dr. Nancy Warter-Perez.
Geography 465 Assignments, Conditionals, and Loops.
An Introduction to Python Dr. Nancy Warter-Perez April 15, 2004.
An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez.
Python programs How can I run a program? Input and output.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
An Introduction to Textual Programming
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Introduction to Python
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Strings CS303E: Elements of Computers and Programming.
If statements while loop for loop
A Review of C++ Dr. Nancy Warter-Perez June 16, 2003.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Introduction to Python
Control Flow (Python) Dr. José M. Reyes Álamo.
Whatcha doin'? Aims: To start using Python. To understand loops.
CSc 120 Introduction to Computer Programing II Adapted from slides by
An Introduction to Python and Its Use in Bioinformatics
Python: Control Structures
Basic operators - strings
Engineering Innovation Center
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Introduction to Strings
Introduction to Python
T. Jumana Abu Shmais – AOU - Riyadh
Iteration: Beyond the Basic PERFORM
Introduction to Python
CHAPTER 6: Control Flow Tools (for and while loops)
Introduction to Python
Introduction to Strings
Introduction to Strings
LOOP Basics.
Presentation transcript:

An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005

4/21/05Introduction to Python – Part II2 Overview Solution to Programming Workshop #1 If tests Loops for while Example amino acid search program Programming Workshop #2

4/21/05Introduction to Python – Part II3 Solution to Programming Workshop 1 Write a Python program to compute the hydrophobicity of an amino acid # Program to compute the hydrophobicity of an amino acid # (solution only includes first 3 amino acids) # Written by: Prof. Warter-Perez # Date created: April 15, 2004 # Last modified: hydro = {"A":1.8,"C":2.5,"D":-3.5} aa = raw_input ("Please enter amino acid: ") print "The hydrophobicity of %s is %f."% (aa, hydro[aa])

4/21/05Introduction to Python – Part II4 Make solution case insensitive # Program to compute the hydrophobicity of an amino acid # Written by: Prof. Warter-Perez # Date created: April 15, 2004 # Last modified: April 20, made script case insensitive for # amino acids hydro = {"A":1.8,"C":2.5,"D":-3.5} aa = raw_input ("Please enter amino acid: ") aa = aa.upper() print "The hydrophobicity of %s is %f."% (aa, hydro[aa])

4/21/05Introduction to Python – Part II5 Python Basics – Relational and Logical Operators Relational operators ==equal !=not equal >greater than >=greater than or equal <less than <=less than or equal Logical operatorsandornot

4/21/05Introduction to Python – Part II6 if Statement if expression: action Example: a1 = 'A‘; a2 = 'C'; match = 0; if (a1 == a2) : match+=1;

4/21/05Introduction to Python – Part II7 if-elif-else Statement if expression: action 1 elif expression: action 2 else : action 3 Example: a1 = 'A‘; a2 = 'C'; match = 0; gap = 0; if (a1 == a2) : match+=1; elif (a1 > a2): else: gap+=1;

4/21/05Introduction to Python – Part II8 String operations mystring = “Hello World!” ExpressionValuePurpose len(mystring)12 number of characters in mystring “hello”+“world”“helloworld” Concatenate strings “%s world”%“hello”“hello world” Format strings (like sprintf) “world” == “hello” “world” == “world” 0 or False 1 or True Test for equality “a” < “b” “b” < “a” 1 or True 0 or False Alphabetical ordering

4/21/05Introduction to Python – Part II9 Lists mylist=[“a”,”b”,3.58,”d”,4,0] mylist[0] mylist[2] a 3.58 Indexing mylist[-1] mylist[-2] 0404 Negative indexing (counts from end) mylist[1:4][“b”,3.58,”d”]Slicing (like strings) “b” in mylist “e” not in mylist 1 or True mylist.append(8)[“a”,”b”,3.58,”d”,4,0,8]Add to end of list

4/21/05Introduction to Python – Part II10 Dictionaries mydict={“r”:1,”g”:2,”y”:3.5,8.5:8,9:”nine”} mydict.keys()['y', 8.5, 'r', 'g', 9]List of the keys mydict.values()[3.5, 8, 1, 2, 'nine']List of the values mydict[“y”]3.5Value lookup mydict.has_key(“r”)True or 1Check for keys mydict.update({“a”:75}){8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y': 3.5, 9: 'nine'} Add pairs to dictionary

4/21/05Introduction to Python – Part II11 for Statement for var in list: action Sets var to each item in list and performs action range() function generates lists of numbers: range (5) -> [0,1,2,3,4] Example mylist=[“hello”,”hi”,”hey”,”!”]; for i in mylist: print i Iteration 1 prints: hello Iteration 2 prints: hi Iteration 3 prints: hey Iteration 4 prints: !

4/21/05Introduction to Python – Part II12 while Statement while expression: action Example x = 0; while x != 3: x = x + 1 Iteration 1: x=0+1=1 Iteration 2: x=1+1=2 Iteration 3: x=2+1=3 Iteration 4: don’t exec / 2 Infinite loop!

4/21/05Introduction to Python – Part II13 Example: Amino Acid Search Write a program to count the number of occurrences of an amino acid in a sequence. The program should prompt the user for A sequence of amino acids (seq) The search amino acid (aa) The program should display the number of times the search amino acid (aa) occurred in the sequence (seq)

4/21/05Introduction to Python – Part II14 Example: Amino Acid Search (2) #this program will calculate the number of occurrences of an amino acid in a #sequence #by Bryce Ready done=0 while (not done): sequence=raw_input("Please enter a sequence:"); aa=raw_input("Please enter the amino acid to look for:");

4/21/05Introduction to Python – Part II15 Example: Amino Acid Search (3) #compute the number of occurrences using for loop cnt=0 for i in sequence: if i == aa: cnt+=1 if cnt == 1: print "%s occurs in that sequence once" % aa; else: print "%s occurs in that sequence %d times" % (aa, cnt); answer=raw_input("try again? [yn]") if answer == "n" or answer == "N": done = 1

4/21/05Introduction to Python – Part II16 Creating a Python Program Enter your program in the editor Notice that the editor has a color coding Comments Key words Etc… Also notice that it automatically indents Don’t override!! – this is how python tells when block statements end! If doesn’t indent to proper location – indicates bug

4/21/05Introduction to Python – Part II17 Running your Program To build your program Under File->Run… Select No Debugging in the drop-down window Fix any errors, then run again

4/21/05Introduction to Python – Part II18 Programming Workshop #2 Write a sliding window program to compute the %GC in a sequence of nucleotides. The program should prompt the user for The DNA sequence The window size (assume the window increment is 1) Test your program using the data for Workshop 3.