csc 1010 lecture 7
play

CSC 1010 Lecture 7 What do we know so far? Class lecture, lab, - PDF document

CSC 1010 Lecture 7 What do we know so far? Class lecture, lab, Rephactor, Quick Checks, R&R, easter eggs Solve problems, computers useful, user vs. programmer CSC 1010 Programming for All Sequence of instructions,


  1. CSC 1010 Lecture 7 What do we know so far? • Class – lecture, lab, Rephactor, Quick Checks, R&R, easter eggs • Solve problems, computers useful, user vs. programmer • CSC 1010 Programming for All Sequence of instructions, algorithm is step‐by‐step • Python is 3 rd most popular language, core principles • Syntax, runtime, & logic errors, testing & debugging, hardware vs. software • Control flow – step‐by‐step, function call, conditional, loop • IDLE shell, editor, install Python, Hello World • Intrepreter, compiler, Python Standard Library • Variables, assignment, numeric expr., precedence • Print function, Strings, concatenation, indexes, in, * • Interactive programs, if, if‐else, if‐elif‐else, int, float • Boolean expressions: ==, !‐, <, <=, >, >=, not, and, or Lecture 7 • Input function, comparing strings, programming conventions • Variable & function names lowercase, CONSTANTS, indent Lists • while, for, range, augmented assignments, palindromes • Turtle Graphics, forward, left, right, pensize, pencolor, dot, circle • goto, penup, pendown, fillcolor, begin_fill, end_fill, speed • Calling & defining functions, import, parameters vs. arguments, return • Positional args, default args, variable args, keyword args, local variables • String methods, replace, method vs. function, built‐in & external functions • Using loops and functions to create graphics, random numbers, design process 2 1 2 Lists List Indexing A list is a sequence. The items or elements of a list can be just The elements in a list can be accessed individually using their about anything, including a character, string, integer, or even indexes similar to string indexing. another list. beat l es = [ ' John' , ' Paul ' , ' G eor ge' , ' Ri ngo' ] nam e = beat l es[ 3] l et t er s = [ ' a' , ' b' , ' c' , ' d' , ' e' ] pr i nt ( nam e) num ber s = [ 867, 5, 3, 0, 9] beat l es = [ ' John' , ' Paul ' , ' G eor ge' , ' Ri ngo' ] Ri ngo The elements in a list can be different types. Element values also can be changed using their index, because lists are mutable meaning their values can be changed. j ackson5 = [ [ ' a' , ' b' , ' c' ] , [ 1, 2, 3] ] beat l es[ 3] = ' Pet e' pr i nt ( beat l es) [ ' John' , ' Paul ' , ' G eor ge' , ' Pet e' ] 3 4 3 4 Iterating a List List Concatenation Iterating the elements in a list can be done with a for loop. Concatenation with lists works the same as with strings, using the concatenation operator (+). beat l es = [ ' John' , ' Paul ' , ' G eor ge' , ' Ri ngo' ] l i st 1 = [ ' o' , ' m ' , ' g' ] l i st 2 = [ ' n' , ' f ' , ' w' ] f or nam e i n beat l es: pr i nt ( nam e) excl am = l i st 1 + l i st 2 pr i nt ( excl am ) John [ ' o' , ' m ' , ' g' , ' n' , ' f ' , ' w' ] Paul G eor ge Augmented assignment works on lists, as well. Ri ngo excl am += [ ' ! ' ] pr i nt ( excl am ) [ ' o' , ' m ' , ' g' , ' n' , ' f ' , ' w' , ' ! ' ] 5 6 5 6 1

  2. CSC 1010 Lecture 7 List Containment Repeating Lists Containment works with lists using the in operator. The repetition operator (*) will creating a new list by repeating another list. l i st 1 = [ ' o' , ' m ' , ' g' ] f ood = [ ' Spam ' ] i f ' m ' i n l i st 1: m eal = f ood * 5 pr i nt ( ' I t em f ound i n l i st ' ) pr i nt ( m eal ) el se: pr i nt ( ' I t em not f ound i n l i st ' ) [ ' Spam ' , ' Spam ' , ' Spam ' , ' Spam ' , ' Spam ' ] The not in operator works on lists, as well. i f ' a' not i n l i st 1: pr i nt ( " I t em doesn' t bel ong i n l i st anyway! " ) Containment is an example of a membership operation. 7 8 7 8 Membership Identity The in and not in operators determine membership in any The is and is not operators determine whether to variables sequence, including lists and strings. refer to the exact same object. m enu = ' spam , baked beans, i ce cr eam , st r awber r i es' x = 7 y = x i f ' beans' i n m enu: i f x i s y: pr i nt ( ' Beans ar e good f or your hear t ' ) pr i nt ( ' x and y r ef er t o t he sam e obj ect ' ) el se: i f ' cupcake' not i n m enu: pr i nt ( ' G ood! I am avoi di ng car bs! ' ) pr i nt ( ' x and y do NO T r ef er t o t he sam e obj ect ' ) x an y r ef er t o t he sam e obj ect Beans ar e good f or your hear t G ood! I am avoi di ng car bs! When you assign one variable to another variable, the two refer to the same underlying object. 9 10 9 10 Type Checking Common List Algorithms: Minimum Value The is and is not operators also can determine the type of an A common algorithm used with lists is finding a minimum. object. val ues = [ 7, 11, 99, 5, 17, 2, 73, 3, 9, 12, 8] m i n_i ndex = 0 x = 7 f or i i n r ange( 1, l en( val ues) - 1) : i f val ues[ i ] < val ues[ m i n_i ndex] : i f t ype( x) i s i nt : m i n_i ndex = i pr i nt ( ' x r ef er s t o an i nt eger ' ) pr i nt ( ' m i n_i ndex i s' , m i n_i ndex) el se: pr i nt ( ' m i n val ue i s' , val ues[ m i n_i ndex] pr i nt ( ' x does NO T r ef er t o an i nt eger ' ) m i n_i ndex i s 5 x r ef er s t o an i nt eger m i n val ue i s 2 Use the isinstance function for more complicated types. Lists enable the use of many other common algorithms: • Filling and printing a list i f i si nst ance( som e_var i abl e, st r ) : pr i nt ( ' The var i abl e i s a st r i ng' ) • Finding an average, minimum or maximum • Searching for and swapping elements 11 12 11 12 2

  3. CSC 1010 Lecture 7 Slicing Strings and Lists Slicing Strings and Lists Slicing a string or list means getting a sub‐sequence. Slicing a list is done the same way as slicing a string. m yst r i ng = ' Hel l o, wor l d! ' l et t er s = [ ' a' , ' b' , ' c' , ' d' , ' e' , ' f ' , ' g' ] par t = m yst r i ng[ 7: 12] sl i ce = l et t er s[ 2: 5] pr i nt ( par t ) pr i nt ( ' O r i gi nal l i st : ' , l et t er s) pr i nt ( ' Sl i ce of l i st : ' , sl i ce) wor l d A slice starts with the item at the first index and includes O r i gi nal l i st : [ ' a' , ' b' , ' c' , ' d' , ' e' , ' f ' , ' g' ] Sl i ce of l i st : [ ' c' , ' d' , ' e' ] everything up to but not including the second index. Slicing can be done from one end or the other. pr ef i x = l et t er s[ : 3] # l i st up t o not i ncl i ndex 3 suf f i x = l et t er s[ 4: ] # l i st f r om i ndex 4 t o t he end 13 14 13 14 List Methods Adding, deleting, finding, counting, reordering, copying. # Add usi ng append, ext end or i nser t nam es. append( ' Ji m m y' ) # Del et e usi ng r em ove, pop or cl ear nam es. r em ove( ' El i sabet h' ) # Fi nd and count usi ng i ndex and count pr i nt ( ' M ar y i s at i ndex: ' , nam es. f i nd( ' M ar y' ) ) pr i nt ( ' Joes i n t he l i st : ' , nam es. count ( ' Joe' ) ) # Reor der usi ng sor t , r ever se, and var i at i ons nam es. sor t ( ) # Copy usi ng copy, l i st ( ) or sl i ci ng[ : ] newnam es = nam es. copy( ) 15 15 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend