Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists COMPSCI 105 S2 2015 Principles of Computer Science.

Similar presentations


Presentation on theme: "Lists COMPSCI 105 S2 2015 Principles of Computer Science."— Presentation transcript:

1 Lists COMPSCI 105 S2 2015 Principles of Computer Science

2 Lists  Lists are a built-in type in Python  Use square brackets to signify a list  Lists can contain any type of data, or any mixture of data my_list1 = [1, 2, 3] my_list2 = ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] my_list3 = [1, 5.899, 'Hello'] my_list4 = [4, 2, 6, 9, 3] my_list1 = [1, 2, 3] my_list2 = ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] my_list3 = [1, 5.899, 'Hello'] my_list4 = [4, 2, 6, 9, 3] my_list 426 9 3 Lecture 02COMPSCI 1052

3 List functions  Numerous list functions are supported  Use help(list) to find out the functions  Examples: >>> x = [1, 2, 3] >>> len(x) >>> x = [1, 2, 3] >>> len(x) Lecture 02COMPSCI 1053 3 3 [1, 2, 3, 4] [1, 2, 3, 5] >>> x += [5] >>> x + [4] >>> 3 in x True 1 1 >>> x[0] x 123 x 123 5

4  append(item)  insert(i, item)  pop()  pop(i)  sort()  reverse()  del( my_list[i] )  index(item)  count(item)  remove(item) Functions of lists 4COMPSCI 105 Lecture 02

5  Write a function that determines if a list is a palindrome.  Your function should return true if the contents of the list are the same regardless of whether the elements are accessed in forward or reverse order.  >>> is_palindrome([1, 2, 3, 2, 1])  True Exercise 5COMPSCI 105 Lecture 02

6 Lists of lists  Since a list can contain anything, it can of course contain a list  In memory my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] my_list 0 1 2 3 123 0 1 2 456 0 1 2 7 0 89 0 1 Lecture 02COMPSCI 1056

7 Exercise 4  Draw a diagram showing how the following list is structured in memory: my_list = [[1, 2], [[3, 4], [5, 6, 7]]] Lecture 02COMPSCI 1057

8 Slices of sequences  A piece of a sequence can be obtained using the following syntax  sequence_name[x:y]  where x is the index of the first element and y is the index after the last element >>> name = 'Andrew' >>> name[0:0] >>> name = 'Andrew' >>> name[0:0] Lecture 02COMPSCI 1058 '' 'A' 'ndr' >>> name[0:1] >>> name[1:4]

9 Slice 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 if omitted, it defaults to [start:end:1]  If the step size is negative, it starts at the end and steps backward towards the start. >>> name = 'Andrew' >>> name[:4:2] >>> name = 'Andrew' >>> name[:4:2] >>> name = 'Andrew' >>> name[::-1] >>> name = 'Andrew' >>> name[::-1] nameAndrew Positive Index 012345 Negative index -6-5-4-3-2 Lecture 02COMPSCI 1059 'ad' 'werdnA'

10 For loops  Used to iterate through a sequence numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) name = "Andrew" for c in name: print(c) name = "Andrew" for c in name: print(c) Lecture 02COMPSCI 10510 2 3 5 7 11 2 3 5 7 11 AndrewAndrew AndrewAndrew Example01.py

11 While loops  Used to execute code when the end condition is unknown name = "Andrew Luxton-Reilly" i = 0 while name[i] != ' ': i += 1 print('Space is at position:', i) name = "Andrew Luxton-Reilly" i = 0 while name[i] != ' ': i += 1 print('Space is at position:', i) Space is at position: 6 Lecture 02COMPSCI 10511 Example01.py

12 Range  Range is a special object in Python  Used to generate integer numbers within a range of values  Can iterate through the range for x in range(0, 5): print(x) for x in range(0, 5): print(x) Lecture 02COMPSCI 10512 0123401234 0123401234 Example01.py

13 List comprehensions  A list can be created using instructions that appear within the square brackets  The general format is as follows:  Examples:  To convert a string to a list of characters:  To generate all the even numbers between 1 and 20 my_list = [x for x in range(0, 10)] [expression for variable in sequence] my_list = [c for c in 'Andrew'] my_list = [n * 2 for n in range(1, 10)] Lecture 02COMPSCI 10513

14 Exercise 5  Write a list comprehension that generates all the odd numbers between 1 and 50 Lecture 02COMPSCI 10514

15  We can extend the syntax for a list comprehension to include a condition:  The general format is as follows:  Examples:  Generate a list of the non-vowel letters that appear in a string: List comprehensions that use conditions my_list = [x for x in range(0, 10) if x % 2 == 0] [expression for variable in sequence if condition] Lecture 02COMPSCI 10515 >>> name = 'Andrew Luxton-Reilly' >>> vowels = 'aeiou‘ >>> my_list = [c for c in name if c not in vowels] >>> name = 'Andrew Luxton-Reilly' >>> vowels = 'aeiou‘ >>> my_list = [c for c in name if c not in vowels] ['A', 'n', 'd', 'r', 'w', ' ', 'L', 'x', 't', 'n', '-', 'R', 'l', 'l', 'y']

16  Information in a list is stored contiguously in memory  location of the information can be calculated  location = start of the list + index * size of each element  Efficiency issues  It takes the same time to access any of the elements  Slow to move elements around (i.e. add and delete elements from within the list) Features of lists Lecture 02COMPSCI 10516


Download ppt "Lists COMPSCI 105 S2 2015 Principles of Computer Science."

Similar presentations


Ads by Google