Lists.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Advertisements

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.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Main task -write me a program
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
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.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Built-in Data Structures in Python An Introduction.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
1. Understand the application of Pseudo Code for programming purposes 2. Be able to write algorithms in Pseudo Code.
Course A201: Introduction to Programming 09/30/2010.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Arrays.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
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.
String and Lists Dr. José M. Reyes Álamo.
Microsoft Visual Basic 2008: Reloaded Third Edition
Data types: Complex types (List)
Lecture 5 of Computer Science II
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
REPETITION CONTROL STRUCTURE
EasyCode Foundations Vocabulary Terms.
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
While Loops in Python.
Repeating Program Instructions
Siti Nurbaya Ismail Senior Lecturer
The relational operators
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Microsoft Visual Basic 2005: Reloaded Second Edition
Bryan Burlingame 03 October 2018
© Akhilesh Bajaj, All rights reserved.
Creation, Traversal, Insertion and Removal
EKT150 : Computer Programming
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CIS16 Application Development and Programming using Visual Basic.net
Data Structures – 1D Lists
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Remembering lists of values lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
3.1 Iteration Loops For … To … Next 18/01/2019.
Using Lists and Functions to Create Dialogue
Console.WriteLine(“Good luck!”);
How to use hash tables to solve olympiad problems
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Structures & Algorithms
Vocabulary Memory Cards--Sample
Data Structures & Algorithms
Hint idea 2 Split into shorter tasks like this.
For loop Using lists.
Arrays & Loops.
While Loops in Python.
Arrays & Loops.
How do you do the following?
GCSE Computing.
Tuple.
Python Creating a calculator.
Presentation transcript:

Lists

Lists Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name. (Datatypes you've already learned about include strings, numbers, and booleans.) You can assign items to a list with an expression of the form list_name = [item_1, item_2] with the items in between brackets. A list can also be empty: empty_list = []. Lists are very similar to strings, but there are a few key differences.

Example 1 zoo_animals = [”monkey", ”giraffe", "sloth", ”elephant” ]; if len(zoo_animals) > 3: print ("The first animal at the zoo is the " + zoo_animals[0]) print ("The second animal at the zoo is the " + zoo_animals[1]) print ("The third animal at the zoo is the " + zoo_animals[2]) print ("The fourth animal at the zoo is the " + zoo_animals[3])

List Indexing You can access an individual item on the list by its index. An index is like an address that identifies the item's place in the list. The index appears directly after the list name, in between brackets, like this: list_name[index]. List indices begin with 0, not 1! You access the first item in a list like this: list_name[0]. The second item in a list is at index 1: list_name[1]. Computer scientists love to start counting from zero.

Example 2 numbers = [5, 6, 7, 8] print ("Adding the numbers at indices 0 and 2“) print (numbers[0] + numbers[2]) print ("Adding the numbers at indices 1 and 3“) print (numbers[1] + numbers[3])

Editing Lists zoo_animals = [”Monkey", ”Snake", "sloth", "tiger"] #Last night our zoo's sloth brutally attacked #the poor tiger and ate it whole. # The ferocious sloth has been replaced by a friendly hyena. zoo_animals[2] = "hyena" # What shall fill the void left by our dear departed tiger? zoo_animals[3] = "rhino"

What will this print? letters = ['a', 'b', 'c'] letters.append('d') print len(letters) print letters letters = ['a', 'b', 'c'] letters.append('d') adds d to the end print len(letters) prints amount of letters =4 print letters prints abcd

Negative List Indices li = ['a', 'b', 'mpilgrim', 'z', 'example'] Returns 'example' >>> li[-3] 2 Returns 'mpilgrim'

Slicing a List >>> li Returns ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] Returns ['b', 'mpilgrim'] >>> li[1:-1] Returns ['b', 'mpilgrim', 'z'] >>> li[0:3] Returns ['a', 'b', 'mpilgrim']

Searching Lists >>> li #update list as shown below ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.index("example") Returns 5 >>> li.index("new") Returns 2

Deleting List Elements Returns ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.remove("z") Returns ['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements'] >>> li.remove("new") Returns ['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements'] #only removes the first occurrence >>> li.remove("c") #what will happen if not in list?

Task – Rock, Paper, Scissors Create a program that solves the following, you could write an algorithm to aid your planning: You are required to create a Rock, Paper, Scissors game where the user will input their selection and that will be compared to a random computer response. You should create a list to store the three possible computer responses and then use the random.randint function to select a random number that in turn selects one of the items from the list. Hints – you must store the user and computer choices in variables. To compare the selections, a number of nested IF (IF within an IF) statements are required. Developments – Use a while loop to play again. Use variables to store and display the user and computer scores.