lecture-10

lecture-10 - 1 Midterm Solutions Recap from last Python...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Midterm Solutions Recap from last Python lecture Interpreted, imperative, OO Language Everything is an object Dynamic Typing Programs are made up of: Expressions Statements Assignment if/elif/else while-loops Functions Classes (still to come) Show recap code Today: Revisit some objects Exploit features and build powerful expressions Base : int, float, complex Sequence : string, tuple, list What can sequences do ? Select i-th element: s[i] subsequence (slice): s[i:j] Update -- For mutable sequences (e.g. Lists) Update i-th element: s[i] = e Update subsequence: s[i:j] = e Update subsequence Update subsequence: s[i:j] = e Changes the object referred to by s May change the length of the sequence Increase: if RHS length > j-i Decrease: if RHS length < j-i s[i:j]=e 2 Update subsequence >>> z = [1,2,3,4,5,6,7,8,9,10] >>> z[3:6] = [a,b,c] >>> z [1,2,3,a,b,c,7,8,9,10] >>> z[3:6] = [a, b] * 2 >>> z [1,2,3,a,b,a,b,7,8,9,10] >>> z[4:]= >>> z [1,2,3,a] >>> z[:0] = [al, be] >>> z [al,be,1,2,3,a,b,a,b,7,8,9,10] s[i:j]=e What else can sequences do ? Q: Suppose you are given a sequence s How to find if the element x appears in s ? x in s Works for any sequence type Sequence contains x in s >>> a in cat True >>> a in entebbe False >>> a in (c, a, t) True >>> 2 in [1,2,3,4,5] True >>> 2 in [1,4,92,2.4] False What can sequences do ? Select i-th element: s[i] subsequence (slice): s[i:j] Update-- For mutable sequences (e.g. Lists ) Update i-th element: s[i] = e Update subsequence: s[i:j] = e Member Is an element in a sequence: x in s Doesnt Python have For-Loops ? Why havent we seen For-loops yet ? Because they are connected to sequences For-loops are used to iterate over sequences Unlike in C, but similar to new Java foreach Elegant, powerful mechanism use it!...
View Full Document

Page1 / 7

lecture-10 - 1 Midterm Solutions Recap from last Python...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online