8/29/2014BCHB524 - 2014 - Edwards Introduction to Python BCHB524 2014 Lecture 2.

Slides:



Advertisements
Similar presentations
SPIM and MIPS programming
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.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
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.
A First Book of ANSI C Fourth Edition
An Introduction to Textual Programming
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Computer Science 101 Introduction to Programming.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
COMP Primitive and Class Types Yi Hong May 14, 2015.
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.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
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.
Introduction to Programming
Introduction to Python
Introduction to Python
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Introduction to Python
Chapter 2: Introduction to C++
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Lecture 4: Expressions and Variables
Introduction to Python
Introduction to Python
Revision Lecture
Introduction to Python
Introduction to Python
Variables, Expressions, and IO
String and Lists Dr. José M. Reyes Álamo.
Python Data Structures: Lists
Introduction to Python
Lecture 5: Basic Java Syntax AP Computer Science Principles
Introduction to Python
CHAPTER 3: String And Numeric Data In Python
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Lecture 4: Expressions and Variables
Introduction to Python
Topics Designing a Program Input, Processing, and Output
Python Data Structures: Lists
Introduction to Python
Introduction to Python
Python Data Structures: Lists
EECE.2160 ECE Application Programming
Unit 3: Variables in Java
General Computer Science for Engineers CISC 106 Lecture 03
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
EECE.2160 ECE Application Programming
Class code for pythonroom.com cchsp2cs
Presentation transcript:

8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2

8/29/2014BCHB Edwards Outline VirtualBox View Shared Folders Review Hello World Simple Numbers Simple Numbers II DNA Sequence Homework 2

Hello World Printing, order of execution, comments, blank-lines, syntax errors, various errors 8/29/2014BCHB Edwards # Output Hello World to the terminal print "Hello World!" print "Hello Georgetown!" print 'Hello Everyone' 3

Simple Numbers 8/29/2014BCHB 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." 4

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 8/29/2014BCHB Edwards5

Simple Numbers II 8/29/2014BCHB 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." 6

Experiment with Simple Numbers II How are fractional values treated differently than whole numbers? How do we write a “float” vs an “int” What happens if we mix integers and floats? What happens if we divide by zero? How can we convert an integer to a float, or vice versa? How can we round a floating point number? 8/29/2014BCHB Edwards7

Lessons Fractional numbers (floats) do fractional division, and print with a decimal point. Specify the fractional digits to make a float (even if zero!) A single float in an expression will "force" a fractional value. Divide by zero is an error! Convert to float using float(10) Convert to int using int(1.2345) Round using round(1.234,1) 8/29/2014BCHB Edwards8

DNA Sequence 8/29/2014BCHB 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

Experiment with DNA Sequence Does it matter what quotes we use? Forget them? Can we change the order of the statements? How do we access the symbols of a string? How can we get the second nucleotide? How can we get the second last nucleotide? How can we get the nucleotide in the middle? What if the DNA sequence has an even number of nucleotides? Odd? Can the “index” be a float? What if it is? 8/29/2014BCHB Edwards10

Experiment with DNA Sequence When we get a subsequence, what do the first and second numbers represent? What does it mean if they are missing? How can we get the first half of the nucleotide sequence? The last half? What happens if the DNA sequence is really short? Do we need all of these variables? 8/29/2014BCHB Edwards11

Lessons Symbols are accessed using [index]. Index starts at 0! Negative index counts from the end Index can be an (integer) variable or an expression Subsequences are accessed using [start:end] where symbol at end is NOT included Missing start is 0, missing end is end of string Can’t access index that isn’t there, but subsequence access is tolerant We can use the expressions directly. 8/29/2014BCHB Edwards12

DNA Sequence II 8/29/2014BCHB 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 13

Experiment with DNA Sequence II What other arithmetic operators work? What happens if we mix upper-case and lower-case? Can we find multi-symbol motifs? Can we count multi-symbol motifs? How might we get lower-case symbols? Can we multiply a string with a float? What if the find argument isn’t there? 8/29/2014BCHB Edwards14

Lessons Limited arithmetic for strings, restricted operations Upper and lower-case symbols are different A does not match a, etc. Find and count can match multi-symbol motifs Use lower for lower-case symbols Find needs to indicate when motif is not found 8/29/2014BCHB Edwards15

Exercises 1. Write a python program to print out the codon for Methionine (aka the start codon) backwards in lower- case symbols. Represent Methionine as string variable codon=“ATG” Use only the techniques shown in lecture 2. Write a python program to find the position and the translation frame number of the first start-codon in the DNA sequence: gcatcacgttatgtcgactctgtgtggcgtctgctggg 8/29/2014BCHB Edwards16

8/29/2014BCHB Edwards Homework 1 Due Wednesday, September 3 rd. Make sure you can run the programs demonstrated in the lecture. Complete Rosalind problems: 1,2,3 Submit solutions to Exercises 2.1, 2.2 to Blackboard. 17