lec11 A crash course in Python contd

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

Info icon This 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
Image of page 1

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

View Full Document Right 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 ?
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern