More on Lists.

Slides:



Advertisements
Similar presentations
Lists Ruth Anderson CSE 140 University of Washington 1.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Today we will compute simple division of fractions. Compute= calculate or work out But First let’s review what we’ve already learned!
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Lists. The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python Let’s get started!.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
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.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
Python Programing: An Introduction to Computer Science
Lists Ruth Anderson University of Washington CSE 160 Winter
For Friday Read No quiz Program 6 due. Program 6 Any questions?
More String Manipulation. Programming Challenge Define a function that accepts two arguments: a string literal and a single character. Have the function.
Last of String Manipulation. Programming Challenge Write a program that asks the user for a string and encodes it by the following guidelines: –If the.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
FINAL WORDS ON LISTS. Sorting items in a list  We discussed how Python has a function for sorting out a list and that this could make our lives so much.
String and Lists Dr. José M. Reyes Álamo.
Final words on Lists.
String Manipulation Part 1
Data types: Complex types (List)
Python Let’s get started!.
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
The relational operators
String Manipulation Part 2
Topics Introduction to File Input and Output
© Akhilesh Bajaj, All rights reserved.
Creation, Traversal, Insertion and Removal
Exception Handling.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1111 Introduction to Programming Fall 2018
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
Topics Sequences Introduction to Lists List Slicing
Writing Functions( ) (Part 4)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
07/04/2019 INDEX NOTATION.
Topics Sequences Lists Copying Lists Processing Lists
Python Basics with Jupyter Notebook
Final words on Lists.
Introduction to Programming with Python
Topics Sequences Introduction to Lists List Slicing
Python Strings.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Introduction to File Input and Output
Arrays.
Python List.
IST256 : Applications Programming for Information Systems
Introduction to Computer Science
Presentation transcript:

More on Lists

Programming Challenge Write a program that continually asks the user for the price of an item, until the user enters “end” You should also ensure that the program will never crash (try/except) At the end, print out the total cost of the items, the total number of items bought, and a list of the price of each item

Slicing Lists Sometimes, you need to extract multiple items from a list Python contains functions that allow you to do this list_1 = [‘zero’, ‘one’, ‘two’, ‘three’, four’, ‘five’] list_2 = list_1[1:5] print(list_2) >> [‘one’, ‘two’, ‘three’, ‘four’]

Slicing Lists The rules and notation for slicing a list are more or less the same as slicing a string new_list = old_list[start : end : step] If you do not specify a start, Python will assume index 0 and if you do not specify end, Python will assume the last element Reminder that Python will not grab the element equivalent to the end index, it will go up to that index

Let’s Practice Given the following list: my_list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘I’, ‘j’, ‘k’, ‘l’] Write a program that: Extracts the first 7 elements into a new list Extracts the characters b, c, and d into a new list Extracts the last 5 characters into a new list

Programming Challenge Write a program that creates a list of all integers between 1 and 100 and then extract all numbers divisible by 6 Try this for all numbers divisible by 9

Finding items in a list You can easily find a particular item in a list by using the “in” operator my_list = [‘pie’, ‘cake’, ‘pizza] if ‘cake’ in my_list: print(“I found cake!”) else: print(“Sorry, there’s no cake”) The “in” operator will return a Boolean value the indicates whether an item was found or not

Programming Challenge Given these two lists: by6 = [6,12,18,24,40,46,42,48,54,60,66,72,78,84,90,96] by9 = [9,18,27,36,45,54,63,72,81,90,99] Write a program that finds all elements that exist in both lists. Then store your results in a new list and print it out to the user.

Adding items to a list We have already observed two ways in which we can add to a list: Repeating the list by using the “*” operator Concatenating lists by using the “+” operator Another way we can do this is by using the “append” method The append method allows you to add an item to the end of a list (recall that methods are specific to objects)

Adding items to a list mylist = [“Charis”, “Dan”, “Jin”, “Sam”] mylist.append(“AJ”) print(mylist) >> [“Charis”, “Dan”, “Jin”, “Sam”, “AJ”]

Removing items from a list You can remove items from a list by using the “remove” method prices = [3.99, 2.99, 1.99] prices.remove(2.99) print(prices) >> [3.99, 1.99] Note that you will raise an exception if you try to remove an item that is not found in the list (this would be a good place to use try/except)

Removing items from a list There’s one more way to remove items from a list but you will need to know the index of the item you wish to remove We will use the “del” method prices = [3.99, 2.99, 1.99] del prices[0] print(prices) >> [2.99, 1.99]

Programming Challenge There are teachers who calculate a student’s average by dropping their lowest test grade Assuming these are the grades of a student, calculate his “normal” average first and then calculate his average after dropping the lowest score grades = [99, 86, 90, 42, 89, 100]

Programming Challenge Write a program that continually prompts the user for a name and store all those names in a list Then, sort the names in alphabetical order and store them in a new list Finally, print out the new list of names in alphabetical order

Sorting items in a list Fortunately, there’s a method for that in Python my_list = [‘Donald’, ‘Bryan’, ‘Andrew’, ‘Mike’] my_list.sort( ) print(my_list) >> [‘Andrew’, ‘Bryan’, ‘Donald’, ‘Mike’]