lec11 A crash course in Python contd

lec11 A crash course in Python contd - CSE 130 : Fall 2006...

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

View Full Document Right Arrow Icon
1 CSE 130 : Fall 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 11: Crash course in Python (contd.) A crash course in Python 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) 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 Sequence “contains” x in s >>> 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 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 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 Doesn’t Python have For-Loops ? Why haven’t we seen For-loops yet ?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2008 for the course CSE 130 taught by Professor Jhala during the Fall '06 term at UCSD.

Page1 / 6

lec11 A crash course in Python contd - CSE 130 : Fall 2006...

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