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

csc 1010 lecture 7
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 1

CSC 1010 Lecture 7 1

CSC 1010 Programming for All

Lecture 7 Lists

What do we know so far?

  • Class – lecture, lab, Rephactor, Quick Checks, R&R, easter eggs
  • Solve problems, computers useful, user vs. programmer
  • Sequence of instructions, algorithm is step‐by‐step
  • Python is 3rd 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
  • Input function, comparing strings, programming conventions
  • Variable & function names lowercase, CONSTANTS, indent
  • 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

Lists

A list is a sequence. The items or elements of a list can be just about anything, including a character, string, integer, or even another list.

3

l et t er s = [ ' a' , ' b' , ' c' , ' d' , ' e' ] num ber s = [ 867, 5, 3, 0, 9] beat l es = [ ' John' , ' Paul ' , ' G eor ge' , ' Ri ngo' ]

The elements in a list can be different types.

j ackson5 = [ [ ' a' , ' b' , ' c' ] , [ 1, 2, 3] ]

List Indexing

The elements in a list can be accessed individually using their indexes similar to string indexing.

4

beat l es = [ ' John' , ' Paul ' , ' G eor ge' , ' Ri ngo' ] nam e = beat l es[ 3] pr i nt ( nam e) Ri ngo

Element values also can be changed using their index, because lists are mutable meaning their values can be changed.

beat l es[ 3] = ' Pet e' pr i nt ( beat l es) [ ' John' , ' Paul ' , ' G eor ge' , ' Pet e' ]

Iterating a List

Iterating the elements in a list can be done with a for loop.

5

beat l es = [ ' John' , ' Paul ' , ' G eor ge' , ' Ri ngo' ] f or nam e i n beat l es: pr i nt ( nam e) John Paul G eor ge Ri ngo

List Concatenation

Concatenation with lists works the same as with strings, using the concatenation operator (+).

6

l i st 1 = [ ' o' , ' m ' , ' g' ] l i st 2 = [ ' n' , ' f ' , ' w' ] excl am = l i st 1 + l i st 2 pr i nt ( excl am ) [ ' o' , ' m ' , ' g' , ' n' , ' f ' , ' w' ]

Augmented assignment works on lists, as well.

excl am += [ ' ! ' ] pr i nt ( excl am ) [ ' o' , ' m ' , ' g' , ' n' , ' f ' , ' w' , ' ! ' ]

1 2 3 4 5 6

slide-2
SLIDE 2

CSC 1010 Lecture 7 2

List Containment

Containment works with lists using the in operator.

7

l i st 1 = [ ' o' , ' m ' , ' g' ] i f ' m ' i n l i st 1: pr i nt ( ' I t em f ound i n l i st ' ) el se: pr i nt ( ' I t em not f ound i n l i st ' )

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.

Repeating Lists

The repetition operator (*) will creating a new list by repeating another list.

8

f ood = [ ' Spam ' ] m eal = f ood * 5 pr i nt ( m eal ) [ ' Spam ' , ' Spam ' , ' Spam ' , ' Spam ' , ' Spam ' ]

Membership

The in and not in operators determine membership in any sequence, including lists and strings.

9

m enu = ' spam , baked beans, i ce cr eam , st r awber r i es' i f ' beans' i n m enu: pr i nt ( ' Beans ar e good f or your hear t ' ) i f ' cupcake' not i n m enu: pr i nt ( ' G

  • od! I am

avoi di ng car bs! ' ) Beans ar e good f or your hear t G

  • od! I am

avoi di ng car bs!

Identity

The is and is not operators determine whether to variables refer to the exact same object.

10

x = 7 y = x i f x i s y: pr i nt ( ' x and y r ef er t o t he sam e obj ect ' ) el se: 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

When you assign one variable to another variable, the two refer to the same underlying object.

Type Checking

The is and is not operators also can determine the type of an

  • bject.

11

x = 7 i f t ype( x) i s i nt : pr i nt ( ' x r ef er s t o an i nt eger ' ) el se: pr i nt ( ' x does NO T r ef er t o an i nt eger ' ) x r ef er s t o an i nt eger

Use the isinstance function for more complicated types.

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' )

Common List Algorithms: Minimum Value

A common algorithm used with lists is finding a minimum.

12

val ues = [ 7, 11, 99, 5, 17, 2, 73, 3, 9, 12, 8] m i n_i ndex = 0 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] : m i n_i ndex = i pr i nt ( ' m i n_i ndex i s' , m i n_i ndex) pr i nt ( ' m i n val ue i s' , val ues[ m i n_i ndex] m i n_i ndex i s 5 m i n val ue i s 2

Lists enable the use of many other common algorithms:

  • Filling and printing a list
  • Finding an average, minimum or maximum
  • Searching for and swapping elements

7 8 9 10 11 12

slide-3
SLIDE 3

CSC 1010 Lecture 7 3

Slicing Strings and Lists

Slicing a string or list means getting a sub‐sequence.

13

m yst r i ng = ' Hel l o, wor l d! ' par t = m yst r i ng[ 7: 12] pr i nt ( par t ) wor l d

A slice starts with the item at the first index and includes everything up to but not including the second index.

Slicing Strings and Lists

Slicing a list is done the same way as slicing a string.

14

l et t er s = [ ' a' , ' b' , ' c' , ' d' , ' e' , ' f ' , ' g' ] sl i ce = l et t er s[ 2: 5] 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) 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' ]

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

List Methods

Adding, deleting, finding, counting, reordering, copying.

15

# Add usi ng append, ext end or i nser t nam

  • es. append( ' Ji m

m y' ) # Del et e usi ng r em

  • ve, pop or cl ear

nam

  • es. r em
  • ve( ' 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( )

13 14 15