Introduction to Python

Slides:



Advertisements
Similar presentations
SPIM and MIPS programming
Advertisements

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.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
A First Book of ANSI C Fourth Edition
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
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.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2.
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/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
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.
Introduction to Programming
Introduction to Python
Basic concepts of C++ Presented by Prof. Satyajit De
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Introduction to Programming
Introduction to Python
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Lecture 4: Expressions and Variables
ECE Application Programming
Introduction to Python
Introduction to Python
Introduction to Python
Introduction to Python
Variables, Expressions, and IO
Introduction to Programming
Introduction to Programming
Python Data Structures: Lists
Introduction to Python
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Lecture 5: Basic Java Syntax AP Computer Science Principles
Introduction to Python
CHAPTER 3: String And Numeric Data In Python
C++ Programming Lecture 3 C++ Basics – Part I
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:

Introduction to Python BCHB524 Lecture 2 BCHB524 - Edwards

Outline VirtualBox Review Simple Numbers II DNA Sequence Homework View Shared Folders Review Hello World Simple Numbers Simple Numbers II DNA Sequence Homework BCHB524 - Edwards

Hello World Printing, order of execution, comments, blank-lines, syntax errors, various errors # Output Hello World to the terminal print "Hello World!" print "Hello Georgetown!" print 'Hello Everyone' BCHB524 - Edwards

Simple Numbers # 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." BCHB524 - Edwards

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 BCHB524 - Edwards

Simple Numbers II # Program input cars = 100.0 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." BCHB524 - Edwards

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? BCHB524 - Edwards

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) BCHB524 - Edwards

DNA Sequence # 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 BCHB524 - Edwards

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? BCHB524 - Edwards

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? BCHB524 - Edwards

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. BCHB524 - Edwards

DNA Sequence II # 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 BCHB524 - Edwards

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? BCHB524 - Edwards

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 BCHB524 - Edwards

Exercises Write a python program to print out the codon for Methionine (aka the start codon) backwards in lower-case symbols. Start with Methionine represented as string variable codon=“ATG” Use only the techniques shown in lecture Write a python program to find the position and the translation frame (1, 2, or 3) of the first start-codon in the DNA sequence: gcatcacgttatgtcgactctgtgtggcgtctgctggg BCHB524 - Edwards

Homework 1 Due Wednesday, September 7th. Make sure you can run the programs demonstrated in the lecture. Submit solutions to Exercises 2.1, 2.2 to Canvas. BCHB524 - Edwards