9/11/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 3.

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to C Programming
Python programs How can I run a program? Input and output.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
An Introduction to Textual Programming
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Fundamentals of Python: First Programs
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5.
Strings CS303E: Elements of Computers and Programming.
Variables and Expressions CMSC 201 Chang (rev )
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")‏ >>>
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7.
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Python
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Python
Introduction to Python
Introduction to Python
Advanced Python Concepts: Modules
Topic: Python’s building blocks -> Statements
Advanced Python Data Structures
Introduction to Python
Introduction to Python
CMPT 120 Topic: Python strings.
Advanced Python Data Structures
Introduction to Python
Introduction to Python
Variables, Expressions, and IO
Advanced Python Concepts: Object Oriented Programming
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
CS 100: Roadmap to Computing
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Python Data Structures: Lists
Introduction to Python
Introduction to Python
Advanced Python Data Structures
Introduction to Python
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Introduction to Python
Python Data Structures: Lists
Introduction to Python
Introduction to Python
Python Data Structures: Lists
Introduction to Computer Science
Advanced Python Concepts: Object Oriented Programming
General Computer Science for Engineers CISC 106 Lecture 03
CS 100: Roadmap to Computing
Presentation transcript:

9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3

9/11/2015BCHB Edwards Outline Review Homework #1 Solutions Functions & Methods Defining new functions Control flow: if statement 2

9/11/2015BCHB Edwards Outline Review Hello World (Printing, Execution) Simple Numbers (Variables, Integers) Simple Numbers II (Floats) DNA Sequence (Strings, characters from) DNA Sequence II (String arithmetic, methods) 3

Hello World Printing, order of execution, comments, blank-lines, syntax errors, various errors 9/11/2015BCHB Edwards # Output Hello World to the terminal print "Hello World!" print "Hello Georgetown!" print 'Hello Everyone' 4

Simple Numbers 9/11/2015BCHB Edwards # Program input cars = 100 people_per_car = 4 drivers = 30 passengers = 90 # Compute the dependent values cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * people_per_car average_people_per_car = ( drivers + passengers ) / cars_driven people_in_last_car = ( drivers + passengers - 1 ) % people_per_car + 1 # Output the results print "There are", cars, "cars available." print "There are only", drivers, "drivers available." print "There will be", cars_not_driven, "empty cars today." print "We can transport", carpool_capacity, "people today." print "We have", passengers, "to carpool today." print "We need to put about", average_people_per_car, "in each car." print "There are", people_in_last_car, "people in the last car." 5

Simple Numbers (Review) Variables (names) to store values Variables to store the result of expressions The variable name itself does not matter, but should be descriptive Variables must have a value before you use them Arithmetic operators +, -, *, /, % are available Arithmetic can use variables and values Result of integer division is an integer 9/11/2015BCHB Edwards6

Simple Numbers II 9/11/2015BCHB Edwards # Program input cars = people_per_car = 4.0 drivers = 30.0 passengers = 80.0 # Compute the dependent values cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * people_per_car average_people_per_car = ( drivers + passengers ) / cars_driven people_in_last_car = ( drivers + passengers - 1 ) % people_per_car + 1 # Output the results print "There are", cars, "cars available." print "There are only", drivers, "drivers available." print "There will be", cars_not_driven, "empty cars today." print "We can transport", carpool_capacity, "people today." print "We have", passengers, "to carpool today." print "We need to put about", average_people_per_car, "in each car." print "There are", people_in_last_car, "people in the last car." 7

Simple Numbers II (Review) Numbers can be fractional (float) or integer (int). Floats are entered using a decimal place. Variables can store floats or ints Arithmetic operators +, -, *, /, % are available Arithmetic can use variables or values Result of float division is a float Result of arithmetic with mixed floats and ints is a float. 9/11/2015BCHB Edwards8

DNA Sequence 9/11/2015BCHB Edwards # DNA is cool! dna_sequence = 'gcatgacgttattacgactctgtgtggcgtctgctggg' # Compute dependent values first_nucleotide = dna_sequence[0] last_nucleotide = dna_sequence[-1] first_four_nucs = dna_sequence[0:4] last_ten_nucs = dna_sequence[-10:] sequence_length = len(dna_sequence) # Output results print "First nucleotide",first_nucleotide print "Last nucleotide",last_nucleotide print "First four nucleotides",first_four_nucs print "Last ten nucleotides",last_ten_nucs print "Sequence length",sequence_length 9

DNA Sequence (Review) Strings are sequences of symbols (characters) Variables can store strings! (and ints and floats) Access characters from a string stored in a variable using an index (integer) between [ and ] Positive index from beginning of string 0… Negative index from end of string -1… The index may be stored in a variable The index may be the result of arithmetic Chunks of the sequence, using [s:e] Chunk starts at index s, ends before index e. If s is missing, start at beginning of string If e is missing, end at end of string. Function len(…) returns the length of the string 9/11/2015BCHB Edwards10

DNA Sequence II 9/11/2015BCHB Edwards # DNA is cool! dna_sequence = 'gcatgacgttattacgactctgtgtggcgtctgctggg' oligo1 = 'ATTCG' oligo2 = 'TCGAT' # Compute dependent values, using arithmetic and string methods ligated_oligos = oligo1 + oligo2 tandem_repeat = oligo1*6 polya = 'A'*20 in_uppercase_symbols = dna_sequence.upper() NumberOfA = dna_sequence.count('a') PositionOfT = dna_sequence.find('t') rna_sequence = dna_sequence.replace('t','u') # Output results print "Ligated oligos",ligated_oligos print "Tandem repeat",tandem_repeat print "Polynucleotide run",polya print "Uppercase",in_uppercase_symbols print "Number of Adenine",NumberOfA print "Position of first Thymine",PositionOfT print "As RNA",rna_sequence 11

DNA Sequence II (Review) Strings can be added (concatenation) Strings can be multiplied by an integer (concatenated copies) Upper and lower-case characters are not the same s.find(t) → (integer) position of the string t in string s, if t is in s. Otherwise, -1. s.count(t) → (integer) count of string t in string s. s.upper() → upper-case version of string s. s.replace(u,v) → string s with string u replaced by string v. 9/11/2015BCHB Edwards12

Homework Solutions 9/11/2015BCHB Edwards13

Conversion Functions 9/11/2015BCHB Edwards # There are many useful functions built into Python aString = 'abcdefghijkl' anInteger = 10 aFloat = integerString = '17' floatString = '1.234' # Conversion print "anInteger: before", anInteger, "after", float(anInteger) print "aFloat: before", aFloat, "after", int(aFloat) print "integerString: before", integerString, "after", int(integerString) print "floatString: before", floatString, "after", float(floatString) # Errors print "floatString: before", floatString, "after", int(floatString) print "aString: before", aString, "after", int(aString) print "aString: before", aString, "after", float(aString) print "floatString + 1:", floatString + 1 print "integerString + 1:", integerString + 1 # Figure out the internal representation print type(anInteger), repr(anInteger), anInteger print type(aFloat), repr(aFloat), aFloat print type(integerString), repr(integerString), integerString print type(floatString), repr(floatString), floatString 14

More Functions 9/11/2015BCHB Edwards # There are many useful functions built into Python aString = 'abcdefghijkl' anInteger = 10 aFloat = negativeInteger = -5 negativeFloat = # Math print "Absolute value:", abs(negativeInteger) print "Absolute value:", abs(negativeFloat) print "Min", min(anInteger,aFloat,negativeInteger,negativeFloat) print "Max", max(anInteger,aFloat,negativeInteger,negativeFloat) # String length print len(aString) # Complicated expressions! print "Also known as 1.0:",abs(-3)*(1/float('3.0')) print "Also known as 5: ",int(float(' '))*5 15

String Methods 9/11/2015BCHB Edwards # String methods are very useful! seq = 'gcatgacgttattacgactctgtgtggcgtctgctggg' # A few important string methods print "The number of 'a' symbols:",seq.count('a') print "The sequence in uppercase:",seq.upper() print "Does it end with tggg:",seq.endswith('tggg') print "Does it start with atg:",seq.startswith('atg') print "What position is tggg in:",seq.find('tggg') print "After conversion to uppercase?",seq.upper().find('TGGG') 16

Defining New Functions 9/11/2015BCHB Edwards # Name and describe a small task (no execution!) def helloworld(): print "Hello world" # Functions may be parameterized by arguments def hello(to): print "Hello",to # Functions can return values def bytwo(x): y = 2*x return y # Functions can be parameterized by more than one argument def rectangle_area(length,height): return length*height # Continued... 17

Defining New Functions 9/11/2015BCHB Edwards # Continuation... # Function execution must occur after its definition helloworld() hello("Georgetown") hello("everyone") helloworld() print bytwo(2), bytwo('abcdef'), bytwo( ) x = 3 y = 4 z = 5 print bytwo(x), bytwo(y), bytwo(z) print rectangle_area(x,y) 18

Defining New Functions Saves typing Reduces errors Single change, global effect Conceptual name aids readability Functions can use other functions! 9/11/2015BCHB Edwards19

Control Flow: if statement Execution path depends on string in seq. Make sure you change seq to different values. 9/11/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 20

Exercises 1. Download or copy-and-paste the DNA sequence of the Anthrax SASP gene from the anthrax_sasp.nuc file in the course data-directory. Treat the provided sequence as the sequence to be translated (no 5' UTR). Write a Python program to print answers to the following questions: Does the SASP gene start with a Met codon? Does the SASP gene have a frame 1 Met codon? How many nucleotides in the SASP gene? How many amino-acids in the SASP protein? What is the GC content (% G or C nucleotides) of the SASP gene? Test your program with other gene sequences. 9/11/2015BCHB Edwards21

9/11/2015BCHB Edwards Homework 2 Due Wednesday, September 16 th. Use only the techniques introduced so far. Make sure you can run the programs demonstrated in lecture. Submit Exercise 3.1 solution to Blackboard 22