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

Info iconThis preview shows pages 1–8. 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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight 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: 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 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 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 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 • 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 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 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....
View Full Document

This note was uploaded on 09/28/2011 for the course 1D04 ENG1D04 taught by Professor Smith during the Summer '10 term at McMaster University.

Page1 / 79

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

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

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