Computer Science 111 Fundamentals of Programming I Sequences: Strings.
CSE 111 Bio: Program Design I Lecture 5: More Strings ...
Transcript of CSE 111 Bio: Program Design I Lecture 5: More Strings ...
CSE111Bio:ProgramDesignILecture5:MoreStrings,IntroLists,Translation&Central
DogmaofBiology
RobertSloan(CS)&RachelPoretsky (Bio)UniversityofIllinois,Chicago
September12,2017
Whatdoesthiscodeprint?
x = "Go"y = "Flames!"print(x + y)
A. Go Flames!B. GoFlames!C. ErrorbecausetheadditionisillegalD. Go
Admin:Midterm1nosoonerthan9/28
•Midterm1moved.Thursday9/28• (slightlytentative,couldbealittlelater)
•Wereallydon'twanttoscheduleanyhourexamonadaywhenastudentwillbeoutforareligiousholiday—sowedidthatPiazzapoll
Review:Indexingstrings
• [i]afterstringgivescharacternumberi• Important:Python(andmanycomputerscientists!)countsfrom0,notfrom1!
• After:my_DNA = "AATGCCGTGCTT"• my_DNA[2] à 'T'
0 1 2 3 4 5 6 7 8 9 10 11A A T G C C G T G C T T
Negativeindices
• Sometimeswewanttogetourhandsonthelastcharacterofastring• Oneway:my_DNA[len(my_DNA)– 1]• Whyisthat– 1inthere?• Becausemy_DNA stringhas11characters,solength11,butcharactersnumbered0,1,2,…,10andingeneralthecharactersofstringsarenumbered• 0,1,…,len(s)– 1
Negativeindices
• Sometimeswewanttogetourhandsonthelastcharacterofastring• Oneway:my_DNA[len(my_DNA)– 1]• Another,ofteneasiertothinkaboutway:• Index–1isalwayslastcharacterofstring• Andindex-22nd lastcharacter,andsoon• Somy_DNA[-1]à 'T';my_DNA[-3]à 'C'
0 1 2 3 4 5 6 7 8 9 10 11A A T G C C G T G C T T
Slicing:Gettingpartofastring
• Substringofastringiscalledslice• s[m:n]returnscharactersfromindexm(countingfrom0asalways)uptobutnotincluding charactern
bt = "Dr. Rosalind Franklin"0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
bt[0:3] à 'Dr.'bt[4:12] à 'Rosalind'
Slicing:omission=start/end
bt = "Dr. Rosalind Franklin"0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
bt[:12] à 'Dr. Rosalind'bt[13:] à 'Franklin'
andsortofsillyone:bt[:] à 'Dr. Rosalind Franklin'
Trivia:Overendtreatedasend
>>> s = "Register to vote!"
In [1]: s[9:11]Out[1]: 'to'In [2]: s[12:20]Out[2]: 'vote!'
0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 0 1 2 3 4 5 6
Usingstrings:slicing(Recap&check)
>>> myDNA = "AATGCCGTGCTT"
>>> myDNA[0:4]
>>> myDNA[3:7]
>>> myDNA[1:]
>>> myDNA[:4]
A A T G C C G T G C T T0 1 2 3 4 5 6 7 8 9 10 11
'AATG'
'GCCG’
'ATGCCGTGCTT'
'AATG'
TGCTT ?
A. myDNA[5]
B. myDNA[7:11]
C. myDNA[7:12]
D. myDNA[7:len(myDNA)]
E. No clue
Fancyslicingyou'llalmostneveruse
• Youareallowedtogive3indices,whichareinterpretedasstart:end:stepbt = "Dr. Rosalind Franklin"
012345678901234567890bt[4:17:3] à RanFn'
Strings:lotsofotherthingtoo!(Labhint!)
• Stringshavemanybuilt-into-Pythonmethodsfordoinglotsofusefulthings.Willcoverindetailinnextfewweeks.Oneexamplefornow:• Countingoccurrenceofletter(oranysubstringinastring):withthe.count()stringmethod:• sentence = 'Mary had a little lamb'• sentence.count('a') à 4• sentence.count('had') à 1• sentence.count('T') à 0
Somepeople objectsjustneverchange
• InPython,integersareimmutable• Cannotassignto integerobject• I.e.,cannotwrite1=0• Probablynotasurprise
• Important:stringsareimmutable too!
first_codon = "ACT"first_codon[0] = "G" Illegal; ERROR!
ButProfessor
• Canwriten = 1n = 0
• Isthatchanginganinteger?• NO!Changingwhichobjectvariablenamenisassignedto• Couldreassign(entireobjectof)first_codon too
Pictures
1
Someimmutableobjectsinmemory
'ACT'
nfirst_codon
0
n=0doesn'tchangeimmutableinteger1,justtheassignmentofavariabletoanobject
Pictures
1
Someimmutableobjectsinmemory
'ACT'
nfirst_codon
0
first_codon =0doesn'tchangeimmutableinteger1,justtheassignmentofavariabletoanobject
Problemwithfirst_codon[0]='G' Someimmutableobjects
inmemory
'ACT'
first_codon
first_codon[0]="G"illegalbecausecannot changecontentsofany blueboxes.Thoseobjectsareimmutable
Comingattractions:Mutabletypes
• Numbers,Booleans,andstringsareallimmutable• Pythondoeshavesomemutabletypes• Twoveryimportantoneswewillseeare• Listsanddictionaries
Lists
>>> primes = [2,3,5,7,11]
>>> biologists = ["james","francis","rosalind"]
>>> L = [2, ”zebra", 11]
>>> M = [2, ”zebra", 11, ["spam","spamity","spam"] ]
Listsindex/slicethesameasstrings>>> M = [2, ”zebra", 11, ["spam","spamity","spam"] ]
>>> len(M)4>>> M[2]11>>> M[3]['spam', 'spamity', 'spam']
>>> M[3][0] A. ["spam","spamity","spam"], B. ['spam']C. 'spam' D. Error E. No clue
>>> M[2:] A. 11 B.[11] D.[11, ["spam","spamity","spam"]]E. [11, "spam","spamity","spam"]
0 1 2 3
Additioninlists>>> my_list = [42, 47, 23]>>> newList = my_list + 100BARF!>>> newList = my_list + [100]>>> newList[42, 47, 23, 100]>>> my_list[42, 47, 23]>>> newList = newList + newList>>> newList
A. [42, 47, 23, 100, 42, 47, 23, 100] B. [42, 47, 23, 100]C. “42, 47, 23, 100”D. No clue
Mutability:ListsaremutableIn [1]: my_list = [42, 47, 23]In [2]: my_list = my_list + [3]In [3]: my_listOut[3]: [42, 47, 23, 3]In [4]: my_list.append(15)In [5]: my_listOut[5]: [42, 47, 23, 3, 15]In [6]: x = my_listIn [7]: x.append(234)In [8]: xOut[8]: [42, 47, 23, 3, 15, 234]>>> my_list A. [42, 47, 23, 3, 15] B. x
C. [42, 47, 23, 3, 15, 234] D. No clue