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

Slides:



Advertisements
Similar presentations
Lists CMSC 201. Overview Today we will learn about: For loops.
Advertisements

Lecture 12 – Loops – while loops COMPSCI 1 1 Principles of Programming.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Lists Introduction to Computing Science and Programming I.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Functions.
Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close.
Lists in Python.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Lecture 20 – Test revision COMPSCI 101 Principles of Programming.
COMPSCI 101 Principles of Programming Lecture 4 –The type() function, the string type, the len() function, string slices.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
PYTHON LISTS. What are lists? An ordered set of elements A variable with 0 or more things inside of it Examples myList = [8, 6, 7, 5, 3, 0, 9] strList.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
I Power Higher Computing Software Development High Level Language Constructs.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
For Loops CMSC 201. Overview Today we will learn about: For loops.
Count Controlled Loops Look at the little children … Why is the sun’s face features orange …
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Lists COMPSCI 105 S Principles of Computer Science.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
More about Iteration Victor Norman CS104. Reading Quiz.
COMPSCI 107 Computer Science Fundamentals
Generating Random Numbers
Computer Programming Fundamentals
Algorithm Analysis The case of recursion.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
While Loops in Python.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists Part 1 Taken from notes by Dr. Neil Moore
Nate Brunelle Today: Repetition, A Trip To The Moon
Chapter 10 Lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists in Python Outputting lists.
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Python Basics with Jupyter Notebook
The structure of programming
COMPUTER PROGRAMMING SKILLS
Topics Sequences Introduction to Lists List Slicing
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

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

 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 Principles of Programming

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(" ") 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(" ") my_list = [True, False, False, True, True, False, True, False, False, False, True, True, False, False, True] print_xs(my_list) main() 3CompSci Principles of Programming X XX X XX X

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 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] [10, 8, 6, 4, 7] Note that the values of the elements in the list have not changed in any way.

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 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.

Give the output 6CompSci 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()

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 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

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 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

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 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

The split() function - example 10CompSci 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: Total 44 Enter a line of numbers: Total Note that split() function breaks the string up in a list of strings.

Give the output 11CompSci 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()

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 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) my_list1 my_list2 my_list [1, 2, 3] [1, 2, 6]

Same output?  Do the following two sections of code give the same output? If not, what is the difference in output? 13CompSci 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)

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 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]

Give the output 15CompSci 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)

List objects can be passed to a function  List objects can be passed as parameters to a function. 16CompSci 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]

List objects can be returned by a function 17CompSci 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]

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 Principles of Programming

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 Principles of Programming