Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 05 – Sequences of data Previously, we looked at: how our programs can have unexpected consequences for society the importance of writing tests before code documenting code after it is written using doctest to integrate testing and documentation COMPSCI 107 - Computer Science Fundamentals 2 At the end of this lecture, students should be able to: Use index values to refer to elements of a sequence Use the “slice” syntax to refer to a subsequence Use operations that apply to sequences Use the for loop syntax to iterate over a sequence Use list comprehensions to create a list Visualise how lists are stored in memory Create multi-dimensional lists COMPSCI 107 - Computer Science Fundamentals 3 Sequences allow you to store values in an organized fashion. Built-in types: strings, lists, tuples http://en.wikibooks.org/wiki/Python_Programming/Sequences COMPSCI 107 - Computer Science Fundamentals 4 Strings are a sequence of characters >>> name = 'Andrew' >>> name[0] 'A' >>> 'd' in name True >>> len(name) 6 >>> name + '_' + 'Luxton-Reilly' 'Andrew_Luxton-Reilly' Strings have a number of methods that are useful split() is especially useful help(str) COMPSCI 107 - Computer Science Fundamentals 5 Write a function that determines whether a given string of text contains a vowel or not. The function should return True if the text contains a vowel. COMPSCI 107 - Computer Science Fundamentals 6 Lists are a built-in type in Python Use square brackets to signify a list Can contain any type of data, or any mixture of types How do you visualise the following list? my_list = [1, 2, 3] my_list 7 Lists are a built-in type in Python Use square brackets to signify a list Can contain any type of data, or any mixture of types How do you visualise the following list? my_list = [1, 2, 3] 2 1 3 my_list 0 1 2 3 4 8 Numerous list functions are supported Use help(list) to see a complete list of functions Examples: my_list = [1, 2, 3] len(my_list) 3 2 1 3 my_list 0 1 2 3 4 9 Numerous list functions are supported Use help(list) to see a complete list of functions Examples: my_list = [1, 2, 3] my_list = my_list + [4] 4 2 1 3 my_list 0 1 2 3 4 10 Numerous list functions are supported Use help(list) to see a complete list of functions Examples: my_list.append(7) 7 4 2 1 3 my_list 0 1 2 3 4 11 Numerous list functions are supported Use help(list) to see a complete list of functions Examples: True 3 in my_list 4 2 1 3 my_list 0 1 2 3 4 12 Numerous list functions are supported Use help(list) to see a complete list of functions Examples: 1 my_list[0] 4 2 1 3 my_list 0 1 2 3 4 13 Numerous list functions are supported Use help(list) to see a complete list of functions append(item) insert(i, item) pop() pop(i) sort() reverse() del( my_list[i] ) index(item) count(item) remove(item) Make sure you are comfortable with these functions (i.e. you can use the Python docs to call them and understand how they work) 14 Write a function that sums the elements of a list that contains numbers. Write a function that accepts a list and returns a new list with the same contents, but in reverse order. COMPSCI 107 - Computer Science Fundamentals 15 Since a list element can be of any type, it can actually be a list my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] 16 Since a list element can be of any type, it can actually be a list my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] 17 Since a list element can be of any type, it can actually be a list my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] The online Python Tutor has a useful visualisation engine: http://www.pythontutor.com/visualize.html 18 A piece of a sequence can be obtained using the following syntax: sequence_name[start : stop] Where start is the index of the first element, and stop is the index after the last element >>> name = "Andrew" >>> values = [5,8,2,3,7,1] >>> print(name[1:2]) n >>> print(values[2:5]) [2, 3, 7] 19 Actually, the syntax allows for a third value, used to define the step size between elements included in the slice. If a value is omitted, it defaults to : 0 length 1 [ start : stop : step] >>> word = 'Computer' >>> print(word[:4:2]) Cm >>> print(word[::-1]) retupmoC Using a step size of -1, and not providing start and stop means that Python will step through the word right to left, from end to beginning word C o m p u t e r Positive index 0 1 2 3 4 5 6 7 Negative index -8 -7 -6 -5 -4 -3 -2 -1 20 21 Used to iterate through a sequence numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) name = 'Andrew' for c in name: print(c) 2 3 5 7 11 A n d r e w 22 A list can be created using instructions that appear within the square brackets my_list = [x for x in range(90, 100)] print(my_list) [90, 91, 92, 93, 94, 95, 96, 97, 98, 99] The general format of a list comprehension is: [expression for variable in sequence] 23 Examples Converting a string into a list of characters: my_list = [c for c in 'COMPSCI107'] print(my_list) ['C', 'O', 'M', 'P', 'S', 'C', 'I', '1', '0', ‘7'] Generating all even numbers between 1 and 20 my_list = [n * 2 for n in range(1, 10)] print(my_list) [2, 4, 6, 8, 10, 12, 14, 16, 18] 24 We can extend the syntax for a list comprehension to include a condition: my_list = [x for x in range(0, 10) if x % 2 == 0] print(my_list) [0, 2, 4, 6, 8] The general format of a list comprehension using a condition is: [expression for variable in sequence if condition] 25 What is the output? values = [x + x for x in range(5) if x + x < x * x] print('Result =', values) (a) Result = [2, 4] (b) Result = [3, 4] (c) Result = [0, 2, 4, 6, 8] (d) Result = [6, 8] (e) Result = [0, 1, 2, 3, 4] 26 What is the output? numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] primes = [2, 3, 5, 7] my_list = [x+x for x in numbers if x not in primes] print(my_list) 27 Example Generate a list of the non-vowel letters that appear in a string: phrase = 'The quick brown fox jumps over the lazy dog' vowels = 'aeiou' my_list = [c for c in phrase if c not in vowels] for c in my_list: print(c, end = '') Th qck brwn fx jmps vr th lzy dg 28 Information in a list is stored contiguously in memory (i.e. in consecutive memory locations) Location (memory address) of the information can be calculated as: location = address of start of list + index * size of each element Efficiency issues It takes the same amount of time to access any of the elements It is slow to move elements around (i.e. add and delete elements from within the list) COMPSCI 107 - Computer Science Fundamentals 29