Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.

Similar presentations


Presentation on theme: "Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming."— Presentation transcript:

1 Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming

2  At the end of this lecture, students should be able to:  perform calculations which change the elements of a list  use the index operator to access individual elements of a list  copy the values of a list  use lists as parameters or return values in functions  use the split() function Learning outcomes 2CompSci 101 - Principles of Programming

3 Recap  From lecture 14  we can iterate through the elements of a list using a for…in loop  calculations can done on the elements of a list def print_xs(a_list): for item in a_list : if item == True: print("X", end="") else: print(" ", end="") def main(): print("01234567890123456789") my_list = [True, False, False, True, True, False, True, False, False, False, True, True, False, False, True] print_xs(my_list) main() def print_xs(a_list): for item in a_list : if item == True: print("X", end="") else: print(" ", end="") def main(): print("01234567890123456789") my_list = [True, False, False, True, True, False, True, False, False, False, True, True, False, False, True] print_xs(my_list) main() 3CompSci 101 - Principles of Programming 01234567890123456789 X XX X XX X

4 Why does this not work?  Using the following version of the for…in loop, each element of a list can be accessed but this is not useful for some tasks, e.g., 4CompSci 101 - Principles of Programming def main(): my_list = [10, 8, 6, 4, 7] print("1.", my_list) for item in my_list: item = item * 2 print(item, " ", end="") print() print("3.", my_list) main() def main(): my_list = [10, 8, 6, 4, 7] print("1.", my_list) for item in my_list: item = item * 2 print(item, " ", end="") print() print("3.", my_list) main() 1. [10, 8, 6, 4, 7] 20 16 12 8 14 3. [10, 8, 6, 4, 7] Note that the values of the elements in the list have not changed in any way.

5 Changing the values of elements in the list  The element values of a list can be changed if we access each element using the index value of the element, e.g., 5CompSci 101 - Principles of Programming def main(): my_list = [10, 8, 6, 4, 7] print("1.", my_list) number_of_elements = len(my_list) for index in range(number_of_elements): my_list[index] = my_list[index] * 2 print("2.", my_list) main() def main(): my_list = [10, 8, 6, 4, 7] print("1.", my_list) number_of_elements = len(my_list) for index in range(number_of_elements): my_list[index] = my_list[index] * 2 print("2.", my_list) main() 1. [10, 8, 6, 4, 7] 2. [20, 16, 12, 8, 14] Changing a value at an index location updates the content of the list.

6 Give the output 6CompSci 101 - Principles of Programming def main(): my_list = [10, 8, 6, 4, 7] number_of_elements = len(my_list) for index in range(len(my_list)): print(index, my_list[index]) main() def main(): my_list = [10, 8, 6, 4, 7] number_of_elements = len(my_list) for index in range(len(my_list)): print(index, my_list[index]) main()

7 Complete the main() function  Complete the code in the main() function so that 1 is added to all the odd values in the list. 7CompSci 101 - Principles of Programming def main(): my_list = [] for i in range(10) : my_list += [random.randrange(1, 100)] print("1.", my_list) print("2.", my_list) main() def main(): my_list = [] for i in range(10) : my_list += [random.randrange(1, 100)] print("1.", my_list) print("2.", my_list) main() 1. [69, 98, 7, 92, 13, 9, 27, 36, 96, 46] 2. [70, 98, 8, 92, 14, 10, 28, 36, 96, 46] #write code here

8 Complete the main() function  Complete the code in the main() function which changes the elements starting from position 1 so that each element is the accumulative total of the previous elements (i.e., element 1 is the sum of the element 0 and element 1, element 2 is the sum of element 1 and element 2, etc.) 8CompSci 101 - Principles of Programming def main(): my_list = [] for i in range(10): my_list += [random.randrange(1, 10)] print("1.", my_list) print("2.", my_list) main() def main(): my_list = [] for i in range(10): my_list += [random.randrange(1, 10)] print("1.", my_list) print("2.", my_list) main() 1. [8, 1, 9, 5, 6, 3, 6, 4, 5, 6] 2. [8, 9, 18, 23, 29, 32, 38, 42, 47, 53] #write code here

9 The split() function  The split() function separates a single string into a list of the parts of the string using the separator defined (within the parentheses).  If no separator is defined, whitespace is the separator, e.g., 9CompSci 101 - Principles of Programming def main(): phrase = 'The best cure for insomnia is to get a lot of sleep' list_of_words = phrase.split() print(list_of_words[0], list_of_words[4], list_of_words[7]) main() def main(): phrase = 'The best cure for insomnia is to get a lot of sleep' list_of_words = phrase.split() print(list_of_words[0], list_of_words[4], list_of_words[7]) main() The insomnia get

10 The split() function - example 10CompSci 101 - Principles of Programming def main(): line_of_nums = input("Enter a line of numbers: ") list_of_nums = line_of_nums.split() for i in range(len(list_of_nums)): list_of_nums[i] = int(list_of_nums[i]) total = 0 for number in list_of_nums: total += number print("Total", total) main() def main(): line_of_nums = input("Enter a line of numbers: ") list_of_nums = line_of_nums.split() for i in range(len(list_of_nums)): list_of_nums[i] = int(list_of_nums[i]) total = 0 for number in list_of_nums: total += number print("Total", total) main() Enter a line of numbers: 4 6 12 13 9 Total 44 Enter a line of numbers: 5 -3 6 8 1 Total 17 1 2 3 4 5 6 7 8 9 10 Note that split() function breaks the string up in a list of strings.

11 Give the output 11CompSci 101 - Principles of Programming def split_message(message): words = message.split() num = int(words[1]) num += 2 words[1] = str(num) + "ll" print(words[2], words[1], words[0]) def main(): phrase = 'out 2 lunch' split_message(phrase) main() def split_message(message): words = message.split() num = int(words[1]) num += 2 words[1] = str(num) + "ll" print(words[2], words[1], words[0]) def main(): phrase = 'out 2 lunch' split_message(phrase) main()

12 Assigning a list object to a variable  Python lists are objects. When an object is assigned to a variable, the reference is copied and stored in the variable. 12CompSci 101 - Principles of Programming my_list1 = [1, 2, 3] my_list2 = my_list1 my_list3 = [1, 2, 3] print(my_list1) print(my_list2) print(my_list3) print() my_list1[2] = 6 my_list3[2] = 6 print(my_list1) print(my_list2) print(my_list3) my_list1 = [1, 2, 3] my_list2 = my_list1 my_list3 = [1, 2, 3] print(my_list1) print(my_list2) print(my_list3) print() my_list1[2] = 6 my_list3[2] = 6 print(my_list1) print(my_list2) print(my_list3) 012012 1 2 3 6 my_list1 my_list2 my_list3012012 1 2 3 6 [1, 2, 3] [1, 2, 6]

13 Same output?  Do the following two sections of code give the same output? If not, what is the difference in output? 13CompSci 101 - Principles of Programming my_list1 = [1, 2, 3] my_list2 = my_list1 for i in range(len(my_list1)): my_list2[i] = my_list1[i] * 2 print(1, my_list1) print(2, my_list2) my_list1 = [1, 2, 3] my_list2 = my_list1 for i in range(len(my_list1)): my_list2[i] = my_list1[i] * 2 print(1, my_list1) print(2, my_list2) my_list1 = [1, 2, 3] my_list2 = [1, 2, 3] for i in range(len(my_list1)): my_list2[i] = my_list1[i] * 2 print(1, my_list1) print(2, my_list2) my_list1 = [1, 2, 3] my_list2 = [1, 2, 3] for i in range(len(my_list1)): my_list2[i] = my_list1[i] * 2 print(1, my_list1) print(2, my_list2)

14 Concatenating lists – a new object is created  The + operator can be used to concatenate (join) two lists. When two lists are concatenated the resulting list is a new list object. 14CompSci 101 - Principles of Programming list1 = [1, 2, 3] list2 = list1 print("1.", list1) list1 = list1 + [4, 5] print("2.", list1) print("3.", list2) list1 = [1, 2, 3] list2 = list1 print("1.", list1) list1 = list1 + [4, 5] print("2.", list1) print("3.", list2) 1. [1, 2, 3] 2. [1, 2, 3, 4, 5] 3. [1, 2, 3]

15 Give the output 15CompSci 101 - Principles of Programming list1 = [1, 2, 3] list2 = [] for item in list1: list2 += [item] for i in range(len(list1)): list1[i] += 1 print(1, list1) print(2, list2) print() list1 = [1, 2, 3] list2 = list1 for i in range(len(list1)): list1[i] += 1 print(3, list1) print(4, list2) list1 = [1, 2, 3] list2 = [] for item in list1: list2 += [item] for i in range(len(list1)): list1[i] += 1 print(1, list1) print(2, list2) print() list1 = [1, 2, 3] list2 = list1 for i in range(len(list1)): list1[i] += 1 print(3, list1) print(4, list2)

16 List objects can be passed to a function  List objects can be passed as parameters to a function. 16CompSci 101 - Principles of Programming def use_lists1(list1, list2): for i in range(len(list1)): list1[i] = list1[i] + list2[i] def main(): my_list1 = [1, 2, 3] my_list2 = [1, 2, 3] use_lists1(my_list1, my_list2) print(my_list1) print(my_list2) main() def use_lists1(list1, list2): for i in range(len(list1)): list1[i] = list1[i] + list2[i] def main(): my_list1 = [1, 2, 3] my_list2 = [1, 2, 3] use_lists1(my_list1, my_list2) print(my_list1) print(my_list2) main() [2, 4, 6] [1, 2, 3]

17 List objects can be returned by a function 17CompSci 101 - Principles of Programming def use_lists2(list1, list2): list3 = [] for i in range(len(list1)): sum = list1[i] + list2[i] list3 = list3 + [sum] return list3 def main(): my_list1 = [1, 2, 3] my_list2 = [1, 2, 3] my_list1 = use_lists2(my_list1, my_list2) print(my_list1) print(my_list2) main() def use_lists2(list1, list2): list3 = [] for i in range(len(list1)): sum = list1[i] + list2[i] list3 = list3 + [sum] return list3 def main(): my_list1 = [1, 2, 3] my_list2 = [1, 2, 3] my_list1 = use_lists2(my_list1, my_list2) print(my_list1) print(my_list2) main() [2, 4, 6] [1, 2, 3]

18 Summary  In a Python program:  the index operator can be used to access each individual element of a list  calculations can be performed on the elements of a list. These calculations can make changes to the elements of a list  a list is an object. Assigning a list to a variable makes a copy of the reference (not a copy of the list).  lists can be passed to functions as parameters, lists can be returned by a function.  we use the split() function to break a string into a list of its parts. The default separator for the split() function is whitespace. 18CompSci 101 - Principles of Programming

19 Examples of Python features used in this lecture def change_list(a_list): number_of_elements = len(a_list) for i in range(number_of_elements): a_list[i] = a_list[i] * 2 def use_lists(list1, list2): list3 = [] for i in range(len(list1)): list3 += [list1[i] + list2[i]] return list3 def split_message(message): words = message.split() print(words[2], words[0]) 19CompSci 101 - Principles of Programming


Download ppt "Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming."

Similar presentations


Ads by Google