The last CS lecture you’ll ever need! On Warner Brothers' insistence, we affirm that this 'C' does not stand for 'Chamber' and 'S' does not stand for 'Secrets.'

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Today’s whether: if, elif, or else!
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
HW 1: Problems 3 & 4 EC 1 & 2.
Python November 14, Unit 7. Python Hello world, in class.
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
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.
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Main task -write me a program
Recitation 1 Programming for Engineers in Python.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
IS 313 Tomorrow… IS 313 last week ? 9/20/09 - today: beyond recursion! 9/27/09 - next wk: web technologies, pt 2 Assignment reminders... Which door to.
Introduction to Python
Lists in Python.
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.
Now String theory To Do: Lists This stuff hurts my brane. when you learn string theory from google images… Goal: Thinking like a machine You should now.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
The last CS lecture you’ll ever need! On Warner Brothers' insistence, we affirm that this 'C' does not stand for 'Chamber' and 'S' does not stand for 'Secrets.'
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
IS 313 Tomorrow… IS 313 Today? 9/16/09 - today: recursion and beyond! 9/23/09 - next wk: no meeting (DC) 9/30/09 - following wk: for & while Homework functions.
Lesson Two: Everything You Need to Know
Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.
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.
Python Let’s get started!.
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
CompSci Today’s topics Problem Solving Pseudocode Notes from Zachary Dodds’ CS 5 course at Harvey Mudd Upcoming ä More Python Reading Brookshear,
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Fundamentals of Programming I Overview of Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
EECS 110: Lec 9: Review for the Midterm Exam
Introduction to Python
Introduction to Python
EECS 110: Lec 5: List Comprehensions
Aleksandar Kuzmanovic Northwestern University
Variables, Expressions, and IO
CMSC201 Computer Science I for Majors Lecture 03 – Operators
EECS 110: Lec 4: Functions and Recursion
The CS 5 Black Post Penguins Invade Dormitory
As close as CS gets to magic
Another problem to solve…
As close as CS gets to magic
Data types Numeric types Sequence types float int bool list str
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Another problem to solve…
Program Breakdown, Variables, Types, Control Flow, and Input/Output
EECS 110: Lec 4: Functions and Recursion
Introduction to Computer Science
CISC101 Reminders Assignment 3 due today.
Another problem to solve…
Class code for pythonroom.com cchsp2cs
Lecture 6 - Recursion.
Presentation transcript:

The last CS lecture you’ll ever need! On Warner Brothers' insistence, we affirm that this 'C' does not stand for 'Chamber' and 'S' does not stand for 'Secrets.' * Caution: do not take this statement too literally or it is possible find yourself in twice as many CS lectures as you need! Aye! Aye! Aye! mateys… Our legal counsel requires us to include these footnotes… Hw #1 due next Tuesday, 9/21, at 11:59 pm CS ? This is just like magic! * Hw #0 due this Tuesday, 9/14, at 11:59 pm IS 313 Today! Choose 2 out of 3 problems Run Apache!

What is it? What type is it?

Again, this is really data. data information knowledge wisdom

Python Data Types bool int long float ** True False Numeric NameExampleWhat is it? values with a fractional part integers > integers <= the results from a comparison: ==, !=,, = "Boolean value"

Datatypes as genes… bool Dominant int long float Recessive 41 + True 10** ** / 5 1 / 5 What will these results be?

Python Operators I’d go with parentheses over precedence Precedence * % ** / > < == + - Caution Level = Highest Lowest ** *%/><==+- = - ( ) It's not worth remembering all these %+/* things! remainder power is equal to set equal to divide as usual

7 % 3 % the "mod" operator 8 % 3 9 % 3 16 % 7 x%4 == 0 x%2 == 0 For what values of x are these True ? What happens on these years? x%y returns the remainder when x is divided by y x%2 == 1

the "equals" operators = != == What about === ?

= Names Data Aiden, Braden, Kaden…? Ava, Abigail, Caylin…? Choosing the right name is more important than I thought. >> x = 41 >> y = x + 1

Inside the machine… x = 41 y = x + 1 name: x type: int LOC: What is happening behind the scenes: What's happening in python: "variables as containers" memory location 300 id, del ComputationData Storage name: y type: int LOC: memory location 312

Computer Memory Random Access Memory (RAM) byte = 8 bits word = 4 bytes = 32 bits is a long list of memory locations bit = 1 "bucket" of charge name: y type: int LOC: bytes for an int on or off is it really a coincidence that this looks like the string theory picture?? 42 plus overhead…

Re-naming…! Aiden, Aiden, Aiden, … It's possible to lose your name in Python! >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? >> y ??

Are numbers enough? No! You need lists of numbers, as well! and strings are helpful, too. list str These are Python sequences…

str ing functions str len + * converts input to a string returns the string’s length str(42) returns '42' len('42') returns 2 'XL' + 'II' returns 'XLII' 'VI'*7 returns 'VIVIVIVIVIVIVI' concatenates strings repeats strings s1 = "ha" s2 = "t" Given these strings What did you say!?! s1 + s2 2*s1 + s2 + 2*(s1+s2) What are

String surgery s[ ] indexes into the string, returning a one-character string s = 'harvey mudd college' s[0] returns 'h' s[6] returns s[ ] returns 'e' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index

Negative indices… s = 'harvey mudd college' Negative indices count backwards from the end! s[-1] returns 'e' s[-11] returns s[-0] returns Python can suit any mood…

s[ : ] slices the string, returning a substring s[12:18] returns 'colleg' s[0:6] returns 'harvey' s = 'harvey mudd college' Slicing what to do if you want a bigger piece of the pie! What's going on here? s[17:] returns 'ge' s[:] returns 'harvey mudd college'

s[ : ] slices the string, returning a substring s[12:18] returns 'colleg' s[0:6] returns 'harvey' s = 'harvey mudd college' Slicing what to do if you want a bigger piece of the pie! the first index is the first character of the slice s[17:] returns 'ge' the second index is ONE AFTER the last character a missing index means the end of the string s[:] returns 'harvey mudd college'

s[ : ] slices the string, returning a substring s[15:-1] s[1:3] s = 'harvey mudd college' Slicing what to do if you want a bigger piece of the pie! What are these slices? How would you get 'mud' 'e' Don't 'wor' 'e'- Be 'hap' 'e' !

s[0:8:2] returns 'hre ' s[ : : ] skip-slices, returning a subsequence s = 'harvey mudd college' Skip-Slicing if you don't want your neighbor to get any… the third index is the "stride" length it defaults to 1 'doe' I love this one. What skip-slice returns What does this return? s[1::6]

Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) L[0] L[0:1] 'hi' How could you extract from L Slicing: always returns the same type Indexing: could return a different type Commas separate elements.

What is len(pi) pi = [3,1,4,1,5,9] What slice of pi is [3,4,5] c = 'claremont' Add parts of c, g, and u to create 'monty' What are pi[0]*(pi[1]+pi[2]) and pi[0]*(pi[1:2]+pi[2:3]) ? What is g[0:4] What is c[-1] What slice of pi is [3,1,4] Part 2Part 1 Add two slices of pi to get [3,1,1,5] g = 'graduate' u = 'university' What slice of pi is [9,1,1] What is pi[-1] Extra!! What is c[:3:-2] What is c[0] + u[7:] Try it!

Python slices… It dices... … but wait, there's more!

Computational Models Picobot!?Python?!

Guido van Rossum Python testimonials

20 years ago…

9/2010 C Python based on the number of skilled engineers world-wide, courses and third party vendors

Computation's Double Identity name: x type: int LOC: "variables as containers" memory location 300 ComputationData name: y type: int LOC: memory location 304

Computation's Double Identity name: x type: int LOC: "variables as containers" memory location 300 ComputationData name: y type: int LOC: memory location 304 the monomers are functions…

# my own function! def dbl( x ): """ returns the double of x """ return 2*x How functions look Comments Docstrings (1)describes overall what the function does, and (2)explains what the inputs mean/are They become part of python's built-in help system! With each function be sure to include one that They begin with # keywords def starts the function return stops it immediately and sends back the return value Some of Python's baggage… Look good to me!

return != print >>> dbl(21) 42 def dbl(x): """ doubles x """ return 2*x def dblPR(x): """ doubles x """ print 2*x >>> dbl(21) 42 return provides the function call's value … print just prints >>> 2 + dbl(21) 44 >>> 2 + dbl(21) crash!

How functions act >>> yours = undo('caf') >>> mine = undo(undo('caf')) def undo(s): """ undo undoes its input! input: s, any string """ return 'de' + s strings, lists, numbers … all data are fair game My brain has just hit recursophobic collapse!

How functions work… def f(x): return 11*g(x) + g(x/2) What is demo(-4) ? def demo(x): return x + f(x) def g(x): return -1 * x This puts the fun in function!

How functions work… def f(x): return 11*g(x) + g(x/2) >>> demo(-4) ? def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def g(x): return -1 * x

How functions work… def f(x): return 11*g(x) + g(x/2) def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f x = -4 return 11*g(x) + g(x/2) def g(x): return -1 * x >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f x = -4 return 11*g(x) + g(x/2) def g(x): return -1 * x These are different x 's ! >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f g x = -4 return 11*g(-4) + g(-4/2) x = -4 return -1.0 * x def g(x): return -1 * x >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f g x = -4 return 11* 4 + g(-4/2) x = -4 return -1 * -4 4 >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def g(x): return -1.0 * x def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f x = -4 return 11* 4 + g(-4/2) >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f g x = -4 return 11* 4 + g(-4/2) x = -2 return -1 * -2 2 >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def g(x): return -1.0 * x def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f x = -4 return 11* >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def g(x): return -1.0 * x def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f x = -4 return 11* >>> demo(-4) ?

How functions work… def f(x): return 11*g(x) + g(x/2) def g(x): return -1.0 * x def demo(x): return x + f(x) demo x = -4 return >>> demo(-4) 42

Douglas Adams's 42 puzzle answer: 42 question: unknown

Function stacking def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) f g x = -4 return 11* 4 + g(-4/2) x = -2 return -1 * -2 2 "The stack" (1) remembers separate functions' values for variables… (2) remembers where to send results back to… >>> demo(-4) 42

Function design

Thinking sequentially 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1 factorial 5! = 120

Thinking sequentially 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1 factorial 5! = 120 Next time & beyond…

Recursion == self-reference! Thinking recursively 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1 factorial 5! = 120 5! = N! =

Warning def fac(N): return N * fac(N-1) This is legal code! I wonder how this code will STACK up!?

def fac(N): return N * fac(N-1) No base case -- the calls to fac will never stop! Make sure you have a base case, then worry about the recursive step... Warning

def fac(N): return fac(N) Roadsigns and recursion examples of self-fulfilling danger

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) Base Case Recursive Step Thinking recursively ! Human: Base case and 1 stepComputer: Everything else

Behind the curtain… def fac(N): if N <= 1: return 1 else: return N * fac(N-1) >>> fac(1) Result: 1 The base case is No Problem!

Behind the curtain… def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5)

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1) Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1) 1 "The Stack" Remembers all of the individual calls to fac Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * 1 Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * fac(3) 3 * 2 Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * fac(4) 4 * 6 Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) 5 * 24 Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) fac(5) Result: N*** -> X 1 0 x*** -> N 0 Look familiar? Base Case Recursive Step Behind the curtain…

def fac(N): if N <= 1: return 1 else: return N * fac(N-1) You handle the base case – the easiest possible case to think of! Recursion does almost all of the rest of the problem! Exploit self-similarity Produce short, elegant code Less work ! Let recursion do the work for you.

But you do need to do one step yourself… def fac(N): if N <= 1: return 1 else: return fac(N) You handle the base case – the easiest possible case to think of! This will not work

Recursion's Big Picture? Need to see BOTH the self-similar piece AND the whole simultaneously!

Breaking Up… …is easy to do with Python s = "This has 2 T's" How do we get at the initial character of s? L = [ 42, 21, 7 ] How do we get at the initial element of L? How do we get at ALL THE REST of s? How do we get at ALL the REST of L?

Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ if : else: base case test!

Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ if s == '': return 0 else: rest = s[1:] len_of_rest = mylen( rest ) total_len = 1 + len_of_rest return total_len

Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ if s == '': return 0 else: rest = s[1:] len_of_rest = mylen( rest ) return 1 + len_of_rest

Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ if s == '': return 0 else: rest = s[1:] return 1 + mylen( rest )

Recursion Examples def mylen(s): """ input: any string, s output: the number of characters in s """ if s == '': return 0 else: return 1 + mylen(s[1:]) There's not much len left here!

Recursion Examples def mymax(L): """ input: a NONEMPTY list, L output: L's maximum element """ if : elif : else: base case test! another case…

Recursion Examples def mymax(L): """ input: a NONEMPTY list, L output: L's maximum element """ if len(L) == 1: return L[0] elif L[0] < L[1]: return mymax( L[1:] ) else: return mymax( L[0:1] + L[2:] ) Hey - do I get a slice?!

Try it! def power(b,p): """ returns b to the p power using recursion, not ** inputs: int b, int p output: a float """ power(5,2) == 25.0 def sajak(s): sajak('wheel of fortune') == 6 What seven-letter English word w maximizes sajak( w ) ? What about y ? You decide… """ returns the number of vowels in the input string, s """ Want more Pat? if : Base case test elif else: Base case test else: Remember: b p == b * b p-1

>>> 3*'i' in 'alien' False The in thing >>> 'i' in 'team' False >>> 'cs' in 'physics' True >>> 'sleep' not in 'CS 5' True >>> 42 in [41,42,43] True >>> 42 in [ [42], '42' ] ?? python is badly confused here… but otherwise it seems pretty perceptive! a little bit different for lists… What’s out here?

def sajak(s): if len(s) == 0: return 0 elif s[0] in 'aeiou': return 1 + sajak(s[1:]) else: return sajak(s[1:]) if it is NOT a vowel, the answer is just the number of vowels in the rest of s if it IS a vowel, the answer is 1 + the number of vowels in the rest of s Base Case Rec. Steps

Good luck with Homeworks #0 and #1 ! The key to understanding recursion is to first understand recursion… - advice from one of last year's students

def power(b,p): """ inputs: base b and power p (an int) implements: b**p """ if p == 0: return else: return Recursion is power!

behind the curtain power(2,3)

def sajak(s): Base case? when there are no letters, there are ZERO vowels if it is NOT a vowel, the answer is Rec. step? Look at the initial character. if it IS a vowel, the answer is

def sajak(s): Base case? when there are no letters, there are ZERO vowels if it is NOT a vowel, the answer is just the number of vowels in the rest of s Rec. step? Look at the initial character. if it IS a vowel, the answer is 1 + the number of vowels in the rest of s

def sajak(s): if s == '': return 0 elif s[0]=='a' or s[0]=='e' or… Checking for a vowel: Try #1 Base Case

sajak('eerier') behind the curtain

Recursion: not just numbers Relationships Self-similarity elsewhere... Natural phenomena Names / Acronyms What is an “ancestor” ? how much here is leaf vs. stem? GNU == "GNU’s Not Unix"

Recursion: not just numbers Relationships Self-similarity elsewhere... Natural phenomena Names / Acronyms What is an “ancestor” ? GNU all stem! An ancestor is a parent OR an ancestor of a parent… == GNU’s Not Unix

Can all information be represented using lists ? Networks Images/Video Sounds/Speech { 2, 3, 5, 7, 11 } Data and lists? ‘Many years later, as he faced the firing squad, Colonel Aureliano Buendia was to remember that distant afternoon when his father took him to discover ice.’ Text Sets Ideas? "sequences"

crazy recursion…

Recursion is the root of all computation since it trades description for time. - Alan Perlis Recursion is the secret to all happiness in writing short, powerful functional programs!

Monty Hall Let’s make a deal ’63-’86 Sept inspiring the “Monty Hall paradox”

Problem #2: montyhall() Write a program that plays the part of Monty…

while we're here… import random comp = random.choice( range(0,100) ) while True: user = input('Your choice: ') print user, '?' if user == comp: print 'Yes!' break else: print 'No...'

"Quiz" on recursion def power(b,p):def sajak(s): sajak('wheel of fortune') == 6 What five-letter English word w maximizes sajak( w ) ? Names: What about y ? You decide… Handle negative values of p, as well. """ returns the number of vowels in the input string, s """ """ returns b to the p power using recursion and not ** inputs: b and p are ints output: should be a float """ Want more Pat? Want more power ? power(5,2) == 25.0 For example, power(5,-1) == 0.2 (or so)

Any real computing with strings/lists? Some strings are more equal than others…

Our DNA's nucleotides: DNA AGCT

Genetic disease expression CTG repeats Myotonic Dystrophy neuromuscular weakness in the face/torso in the 3' UTR (three prime untranslated region), a particular section of messenger RNA DNA "Christmas Tree cataracts" Its computational basis

One sequence-checking algorithm… def check(geneseq): """ seeks evidence of Myo. Dys. """ if 'ctg'*50 in geneseq: return True else: return False Why problems might this algorithm run into? What if we hadn't discovered the CTG-repeats yet? Can we make this algorithm more concise? What information might we prefer to know? checks if one string is contained in another

One sequence-checking algorithm… def check(geneseq): """ seeks evidence of Myo. Dys. """ if 'ctg'*50 in geneseq: return 'Trouble' elif 'ctg'*500 in geneseq : return 'BIG Trouble' else: return "You're OK!" Will this work as a two-diagnosis system? Or is something awry?

One sequence-checking algorithm… def check(geneseq): """ seeks evidence of Myo. Dys. """ if 'ctg'*500 in geneseq: return 'BIG Trouble' elif 'ctg'*50 in geneseq : return 'Trouble' else: return "You're OK!" Feeling much better! most constrained least constrained

Any real computing with strings/lists? Some strings are more equal than others…

Our DNA's nucleotides: DNA AGCT

Genetic disease expression CTG repeats Myotonic Dystrophy neuromuscular weakness in the face/torso in the 3' UTR (three prime untranslated region), a particular section of messenger RNA DNA "Christmas Tree cataracts" Its computational basis

One sequence-checking algorithm… def check(geneseq): """ seeks evidence of Myo. Dys. """ if 'ctg'*50 in geneseq: return True else: return False Why problems might this algorithm run into? What if we hadn't discovered the CTG-repeats yet? Can we make this algorithm more concise? What information might we prefer to know? checks if one string is contained in another

Functioning in Python Some basic, built-in functions: abs max min sum range round bool float int long list str these change data from one type to another absolute value of lists creates lists only as accurately as it can! help dir These are the most important: You call that a language?!

Functioning in Python Far more are available in separate files, or modules: import math math.sqrt( 1764 ) dir(math) from math import * pi sin( pi/2 ) accesses math.py 's functions lists all of math.py 's functions same, but without typing math. all of the time… help() help modules

Functioning in Python This doesn't look quite right… # my own function! def dbl( x ): """ returns double its input, x """ return 2x

Functioning in Python # my own function! def dbl( x ): """ returns double its input, x """ return 2*x

# my own function! def dbl( x ): """ returns double its input, x """ return 2*x Functioning in Python Comments Docstrings (1)describes overall what the function does, and (2)explains what the inputs mean/are They become part of python's built-in help system! With each function be sure to include one that They begin with # keywords def starts the function return stops it immediately and sends back the return value Some of Python's baggage…

Functioning in Python >>> yours = undo('caf') >>> mine = undo(undo('caf')) def undo(s): """ this "undoes" its input, s """ return 'de' + s strings, lists, numbers … all data are fair game