Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 107 Computer Science Fundamentals

Similar presentations


Presentation on theme: "COMPSCI 107 Computer Science Fundamentals"— Presentation transcript:

1 COMPSCI 107 Computer Science Fundamentals
Lecture 05 – Sequences of data

2 Recap 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 Computer Science Fundamentals

3 Learning outcomes 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 Computer Science Fundamentals

4 Sequences Sequences allow you to store values in an organized fashion.
Built-in types: strings, lists, tuples COMPSCI Computer Science Fundamentals

5 Strings Strings are a sequence of characters
Strings have a number of methods that are useful split() is especially useful help(str) >>> name = 'Andrew' >>> name[0] 'A' >>> 'd' in name True >>> len(name) 6 >>> name + '_' + 'Luxton-Reilly' 'Andrew_Luxton-Reilly' COMPSCI Computer Science Fundamentals

6 Exercise 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 Computer Science Fundamentals

7 Python lists my_list = [1, 2, 3] 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] Show the Python visualiser online my_list

8 Python lists my_list = [1, 2, 3] 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 Show the Python visualiser online my_list 1 2 3 4

9 Python lists 3 my_list = [1, 2, 3] len(my_list)
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 Show the Python visualiser online my_list 1 2 3 4

10 Python lists my_list = [1, 2, 3] my_list = my_list + [4]
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 Note – the + operator here creates a new list my_list 1 2 3 4

11 Python lists my_list.append(7) Numerous list functions are supported 7
Use help(list) to see a complete list of functions Examples: my_list.append(7) 7 4 2 1 3 Show the Python visualiser online my_list 1 2 3 4

12 Python lists True 3 in my_list Numerous list functions are supported 4
Use help(list) to see a complete list of functions Examples: 3 in my_list True 4 2 1 3 Show the Python visualiser online my_list 1 2 3 4

13 Python lists 1 my_list[0] Numerous list functions are supported 4 2 1
Use help(list) to see a complete list of functions Examples: my_list[0] 1 4 2 1 3 Show the Python visualiser online my_list 1 2 3 4

14 Python lists Numerous list functions are supported append(item)
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) Show the Python visualiser online

15 Exercises 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 Computer Science Fundamentals

16 Lists of lists 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]] Show the Python visualiser online

17 Lists of lists 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]] Show the Python visualiser online

18 Lists of lists 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: Show the Python visualiser online

19 Slices of sequences 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] Show the Python visualiser online

20 Slices step value 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 : [ start : stop : step] length 1 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 = 'Computer' >>> print(word[:4:2]) Cm >>> print(word[::-1]) retupmoC Sure, the [::] is the extended slice operator. It allows you to take substrings. Basically, it works by specifying which elements you want as [begin:end:step], and it works for all sequences. Two neat things about it: You can omit one or more of the elements and it does "the right thing" Negative numbers for begin, end, and step have meaning For begin and end, if you give a negative number, it means to count from the end of the sequence. For instance, if I have a list: l = [1,2,3] Then l[-1] is 3, l[-2] is 2, and l[-3] is 1. For the step argument, a negative number means to work backwards through the sequence. So for a list:: l = [1,2,3,4,5,6,7,8,9,10] You could write l[::-1] which basically means to use a step size of -1 while reading through the list. Python will "do the right thing" when filling in the start and stop so it iterates through the list backwards and gives you [10,9,8,7,6,5,4,3,2,1]. I've given the examples with lists, but strings are just another sequence and work the same way. So a[::-1] means to build a string by joining the characters you get by walking backwards through the string. word C o m p u t e r Positive index 1 2 3 4 5 6 7 Negative index -8 -7 -6 -5 -4 -3 -2 -1

21 Exercise

22 For loops Used to iterate through a sequence 2 3 5
7 11 numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) A n d r e w name = 'Andrew' for c in name: print(c) Show the Python visualiser online

23 List comprehensions A list can be created using instructions that appear within the square brackets The general format of a list comprehension is: my_list = [x for x in range(90, 100)] print(my_list) [90, 91, 92, 93, 94, 95, 96, 97, 98, 99] Show the Python visualiser online [expression for variable in sequence]

24 List comprehensions 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) Show the Python visualiser online [2, 4, 6, 8, 10, 12, 14, 16, 18]

25 List comprehensions using conditions
We can extend the syntax for a list comprehension to include a condition: The general format of a list comprehension using a condition is: my_list = [x for x in range(0, 10) if x % 2 == 0] print(my_list) [0, 2, 4, 6, 8] Show the Python visualiser online [expression for variable in sequence if condition]

26 Exercises What is the output? (a) Result = [2, 4] (b) Result = [3, 4]
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] Answer = D

27 Exercise 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) Numbers are immutable, lists are mutable – more tomorrow!

28 List comprehensions using conditions
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 = '') Show the Python visualiser online Th qck brwn fx jmps vr th lzy dg

29 Summary 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 Computer Science Fundamentals


Download ppt "COMPSCI 107 Computer Science Fundamentals"

Similar presentations


Ads by Google