9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Program Design and Development
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Recitation 1 Programming for Engineers in Python.
Group practice in problem design and problem solving
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
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.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Introduction. 2COMPSCI Computer Science Fundamentals.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
10/20/2014BCHB Edwards Advanced Python Concepts: Modules BCHB Lecture 14.
8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
9/28/2015BCHB Edwards Basic Python Review BCHB Lecture 8.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
11/4/2015BCHB Edwards Advanced Python Concepts: Object Oriented Programming BCHB Lecture 17.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
 Type Called bool  Bool has only two possible values: True and False.
Introduction to Python
Introduction to Python
Introduction to Python
Introduction to Python
Matlab Training Session 4: Control, Flow and Functions
Introduction to Python
Introduction to Programming
Introduction to Python
Introduction to Python
The Selection Structure
Think What will be the output?
Topics The if Statement The if-else Statement Comparing Strings
Conditionals (if-then-else)
Imperative Programming
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Arithmetic operations, decisions and looping
Introduction to Programming
Introduction to Python
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Structured Program
Introduction to Programming
CSC1018F: Intermediate Python
Python Data Structures: Lists
Introduction to Programming Using Python PART 2
Introduction to Python
Advanced Python Concepts: Exceptions
Introduction to Python
Introduction to Python
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Introduction to Python
Python Data Structures: Lists
Advanced Python Concepts: Exceptions
Introduction to Python
Python Data Structures: Lists
CHAPTER 5: Control Flow Tools (if statement)
Types, Truth, and Expressions
Introduction to Programming
Imperative Programming
COMPUTING.
Presentation transcript:

9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4

9/14/2015BCHB Edwards Outline Review Homework #1 Notes Control flow: if statement Control flow: for statement Exercises 2

9/14/2015BCHB Edwards Review Printing and execution Variables and basic data-types: integers, floats, strings Arithmetic with, conversion between String characters and chunks, string methods Functions, using/calling and defining: Use in any expression Parameters as input, return for output Functions calling other functions (oh my!) If statements – conditional execution 3

Homework #1 Notes Python programs: Upload.py files Don't paste into comment box Don't paste into your writeup Writeup: Upload.txt files, Don't paste into comment box Text document preferred 9/14/2015BCHB Edwards4

Homework #1 Notes Multiple submissions: OK, but… …I'll ignore all except the last one Make each (re-)submission complete Grading: Random grading order Comments Grading "curve" 9/14/2015BCHB Edwards5

Control Flow: if statement Execution path depends on string in seq. Make sure you change seq to different values. 9/14/2015BCHB Edwards # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there if seq.startswith('atg'): print "Sequence without initial Met:",seq[3:] else: print "Sequence (no initial Met):",seq 6

Control Flow: if statement 9/14/2015BCHB Edwards # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there if seq.startswith('atg'): initMet = True newseq = seq[3:] else: initMet = False newseq = seq # Output the results print "Original sequence:",seq print "Sequence starts with Met:",initMet print "Sequence without initial Met:",newseq 7

Control Flow: if statement 9/14/2015BCHB Edwards # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there initMet = seq.startswith('atg'): if initMet: newseq = seq[3:] else: newseq = seq # Output the results print "Original sequence:",seq print "Sequence starts with Met:",initMet print "Sequence without initial Met:",newseq 8

Control Flow: if statement 9/14/2015BCHB Edwards # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there initMet = seq.startswith('atg') if initMet: seq = seq[3:] # Output the results print "Sequence starts with Met:",initMet print "Sequence without initial Met:",seq 9

Serial if statement 9/14/2015BCHB Edwards # Determine the complementary nucleotide def complement(nuc): if nuc == 'A': comp = 'T' if nuc == 'T': comp = 'A' if nuc == 'C': comp = 'G' if nuc == 'G': comp = 'C' return comp # Use the complement function print "The complement of A is",complement('A') print "The complement of T is",complement('T') print "The complement of C is",complement('C') print "The complement of G is",complement('G') 10

Compound if statement 9/14/2015BCHB Edwards # Determine the complementary nucleotide def complement(nuc): if nuc == 'A': comp = 'T' elif nuc == 'T': comp = 'A' elif nuc == 'C': comp = 'G' elif nuc == 'G': comp = 'C' else: comp = nuc return comp # Use the complement function print "The complement of A is",complement('A') print "The complement of T is",complement('T') print "The complement of C is",complement('C') print "The complement of G is",complement('G') 11

If statement conditions Any expression (variable, arithmetic, function call, etc.) that evaluates to True or False Any expression tested against another expression using: == (equality), != (inequality) < (less than), <= (less than or equal) > (greater than), >= (greater than or equal) in (an element of) Conditions can be combined using: and, or, not, and parentheses 9/14/2015BCHB Edwards12

Sequential/Iterative execution Note use of indentation to define a block! 9/14/2015BCHB Edwards13 For (each) statements # Print the numbers 0 through 4 for i in range(0,5): print i # Print the nucleotides in seq seq = 'ATGGCAT' for nuc in seq: print nuc

9/14/2015BCHB Edwards14 For (each) statements # Input to program seq = 'AGTAGTTCGCGTAGCTAGCTAGCTATGCG' # Examine each symbol in seq and count the A's count = 0 for nuc in seq: if nuc == 'A': count = count + 1 # Output the result print "Sequence",seq,"contains",count,"A symbols"

9/14/2015BCHB Edwards15 For (each) statements # Examine each symbol in seq and count the A's def countAs(seq): count = 0 for nuc in seq: if nuc == 'A': count = count + 1 return count # Input to program inseq = 'AGTAGTTCGCGTAGCTAGCTAGCTATGCG' # Compute count aCount = countAs(inseq) # Output the result print "Sequence",inseq,"contains",aCount,"A symbols"

9/14/2015BCHB Edwards16 For (each) statements # Examine each symbol in seq and count those that match sym def countSym(seq,sym): count = 0 for nuc in seq: if nuc == sym: count = count + 1 return count # Input to program inseq = 'AGTAGTTCGCGTAGCTAGCTAGCTATGCG' # Compute count aCount = countSym(inseq,'A') # Output the result print "Sequence",inseq,"contains",aCount,"A symbols"

Exercise 1 Write a Python program to compute the reverse complement of a codon Use my solution to Homework #1 Exercise #1 as a starting point Add the “complement” function of this lecture (slide 12) as provided. Modularize! Place the reverse complement code in a new function. Call the new function with a variety of codons Change the complement function to handle upper and lower-case nucleotide symbols. Test your code with upper and lower-case codons. 9/14/2015BCHB Edwards17

9/14/2015BCHB Edwards18 Exercise 2 Write a Python program to determine whether or not a DNA sequence consists of a (integer) number of (perfect) "tandem" repeats. Test it on sequences: AAAAAAAAAAAAAAAA CACACACACACACAC ATTCGATTCGATTCG GTAGTAGTAGTAGTA TCAGTCACTCACTCAG Hint: Is the sequence the same as many repetitions of its first character? Hint: Is the first half of the sequence the same as the second half of the sequence?