🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

10 - List Tuple Sequence Structures.pptx

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Fall 2019 Computational Thinking with @cse_bennett @csebennett Programming Python Collections/Sequences (Arrays) List is a collection which is ordered and changeable. Allows duplicate members. Tuple...

Fall 2019 Computational Thinking with @cse_bennett @csebennett Programming Python Collections/Sequences (Arrays) List is a collection which is ordered and changeable. Allows duplicate members. Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Set is a collection which is unordered and unindexed. No duplicate members. Dictionary is a collection which is unordered, changeable and indexed. No duplicate members. List Structures List Structures A list is a linear data structure, meaning that its elements have a linear ordering. That is, there is a first element, a second element, and so on. Common List Operations Operations commonly performed on lists include retrieve, update, insert, remove, and append. List Traversal A list traversal is a means of accessing, one-by-one, the elements of a list either from first to last or last to first element. MCQs: Answers 1. What would be the range of index values for a list of 10 elements? (a) 0–9 (b) 0–10 (c) 1–10 2. Which one of the following is NOT a common operation on lists? (a) access (b) replace (c) interleave (d) append (e) insert (f ) delete 3. Which of the following would be the resulting list after inserting the value 50 at index 2? (a) (b) (c) MCQs: Answers 1. What would be the range of index values for a list of 10 elements? (a) 0–9 (b) 0–10 (c) 1–10 2. Which one of the following is NOT a common operation on lists? (a) access (b) replace (c) interleave (d) append (e) insert (f ) delete 3. Which of the following would be the resulting list after inserting the value 50 at index 2? (a) (b) (c) Lists (Sequences) in Python A list in Python is a mutable linear data structure, denoted by a comma- separated list of elements within square brackets, allowing mixed-type elements. Mutable means that the contents of the list may be altered. Elements are indexed from 0 to n-1 , where n is number of elements in the list. Example: [1, 2, 3], ['one', 'two', 'three’], ['apples', 50, True] An empty list is denoted by an empty pair of square brackets, []. Negative indexing, i.e, -1 and -2 refers to the last and second last items. Ex. lst[- 1]=3, lst[-2] = 2. Range Search : lst[0:2] = [1, 2] List Modification Operations in Python Tuples A tuple is an immutable linear data structure. Thus, in contrast to lists, once a tuple is defined, it cannot be altered. Tuples are denoted by parentheses instead of square brackets. Example: nums = (10, 20, 30) student = ('John Smith', 48, 'Computer Science’, 3.42) Tuples of one element must include a comma following the element. Otherwise, the parenthesized element will not be made into a tuple. An empty tuple is represented by a set of empty parentheses, (). What will happen? Sequences and Sequence Operations in Python A sequence in Python is a linearly ordered set of elements accessed by an index number. Lists, tuples, and strings are all sequences. Strings, like tuples, are immutable ; therefore, they cannot be altered What Will Happen ? Nested Lists Lists and tuples can contain elements of any type, including other sequences. Thus, lists and tuples can be nested to create arbitrarily complex data structures. Example: class_grades = [ [85, 91, 89], [78, 81, 86], [62, 75, 77],...], Here, class_grades equals [85, 91, 89], and class_grades equals [78, 81, 86] Calculate the class average on the first exam: a while loop can be constructed that iterates over the first grade of each student’s list of grades. Exercise (Consider the Previous List) We can produce a new list containing the exam average for each student in the class as follows What will happen? MCQs 1. Which of the following sequence 4. For lst = [4, 2, 9, 1], what is the result of types is a mutable type? the following operation, lst.insert(2, 3) ? a) strings b) lists c) tuples a) [4, 2, 3, 9, 1] b) [4, 3 ,2, 9, 1] c) [4, 2, 9, 2, 1] 2. Which of the following is true? a) Lists and tuples are denoted by the use of square brackets. 5. Which of the following is the correct way b) Lists are denoted by use of to denote a tuple of one element? square brackets and tuples are a) b) (6) c) [6,] denoted by the use of d) (6,) parentheses. c) Lists are denoted by use of parentheses and tuples are 6. Which of the following set of operations denoted by the use of square can be applied to any sequence? brackets. a) len(s), s[i], s+w (concatenation) 3. Lists and tuples must each contain b) max(s), s[i], sum(s) at least one element. c) len(s), s[i], s.sort() a) True b) False MCQs: Answers 1. Which of the following sequence 4. For lst = [4, 2, 9, 1], what is the result of types is a mutable type? the following operation, lst.insert(2, 3) ? a) strings b) lists c) tuples a) [4, 2, 3, 9, 1] b) [4, 3 ,2, 9, 1] c) [4, 2, 9, 2, 1] 2. Which of the following is true? a) Lists and tuples are denoted by the use of square brackets. 5. Which of the following is the correct way b) Lists are denoted by use of to denote a tuple of one element? square brackets and tuples a) b) (6) c) [6,] are denoted by the use of d) (6,) parentheses. c) Lists are denoted by use of parentheses and tuples are 6. Which of the following set of operations denoted by the use of square can be applied to any sequence? brackets. a) len(s), s[i], s+w (concatenation) 3. Lists and tuples must each contain b) max(s), s[i], sum(s) at least one element. c) len(s), s[i], s.sort() a) True b) False

Use Quizgecko on...
Browser
Browser