slideset2 - Engineer 1D04 Engineering Computation Term 2...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Engineer 1D04 Engineering Computation Term 2, Summer 2011 Important Notice: This document does NOT contain full record of lecture material. In the classroom, these slides are accompanied by writings and drawings on the board, as well as verbal explanations. If you miss a class, it is your responsibility to consult with a colleague who attended the class to obtain as much information as possible A Bokhari, Eng 1D04, Summer 2011 1
Image of page 1

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

View Full Document Right Arrow Icon
about the presented material. Material tested in the midterm and the final may include any material presented in class, regardless of whether it appears in these slides or not. A Bokhari, Eng 1D04, Summer 2011 2
Image of page 2
Data Collections: What is a collection? A group of items that are related in some identifiable way - antiques collection, wikipedia, library In CS a collection - sometimes called a container- is a grouping of variable number of related data items (possibly zero) Python implements several collections as ordered list of objects (collectively called sequences) List, tuple, dictionary, string data types have similar predefined A Bokhari, Eng 1D04, Summer 2011 3
Image of page 3

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

View Full Document Right Arrow Icon
operations Type: str : contains 8-bit characters - "xyz", ’xyz’ unicode: contains 32 bit characters - u"abc", u’abc’ list: contains any values - [23, "John", 80.4] [] tuple: contains any values - (23, "John", 80.4) (), (2,) What is the difference between a list and a tuple? To create a tuple with only one element v , use the special syntax (v,) . A Bokhari, Eng 1D04, Summer 2011 4
Image of page 4
Mutability: You can’t change part of an immutable value. A single element can be extracted from a sequence by using an expression of the form S[i], where S is a sequence, and i is an integer value that selects the element just after that position. (index starts at zero!) Examples: >>> s= ’abcd’ >>> L = [1, 2, 3, 4, 5] >>> t = ("x", "y") >>> s ’abcd’ >>> L [1, 2, 3, 4, 5] >>> t (’x’, ’y’) Accessing elements: >>> L[0] A Bokhari, Eng 1D04, Summer 2011 5
Image of page 5

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

View Full Document Right Arrow Icon
10 >>> L[4] 5 >>> L[5] Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> L[5] IndexError: list index out of range >>> L[-1] 5 >>> Mutability: >>> s[0] ’a’ >>> s[0] = ’x’ Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> s[0] = ’x’ TypeError: ’str’ object does not support item assignment >>> >>> L[0] 1 >>> L[0] = 10 >>> L [10, 2, 3, 4, 5] Tuple with a single element: >>> x = (3+4) >>> y = (3+4,) >>> x 7 >>> y (7,) >>> Functions and operators for A Bokhari, Eng 1D04, Summer 2011 6
Image of page 6
sequences: >>> print len(s), len(L), len(t) 4 5 2 >>> >>> >>> max(s) ’d’ >>> min(s) ’a’ >>> max(L) 10 >>> min(L) 2 >>> max(t) ’y’ >>> min(t) ’x’ >>> For a value v and a sequence S , the expression v in S returns the Boolean value True if there is at least one element of S that equals v ; it returns False otherwise. >>> 2 in L True >>> 8 in L False >>> c in s Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> A Bokhari, Eng 1D04, Summer 2011 7
Image of page 7

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

View Full Document Right Arrow Icon
c in s NameError: name ’c’ is not defined >>> ’c’ in s True >>> "f" in s False >>> "c" not in s False >>> 2 not in L False >>> ’x’ not in t False Concatenation operator +: 5 >>> s ’abcd’ >>> s + "xyz" ’abcdxyz’ >>> ’xyz’ + s ’xyzabcd’ >>> s = s + s >>> s ’abcdabcd’ >>> L + L [10, 2, 3, 4, 5, 10, 2, 3, 4, 5] >>> t + t (’x’, ’y’, ’x’, ’y’) Repetition Operator *: >>> s
Image of page 8
Image of page 9
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