For loop Using lists.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
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?
Container Types in Python
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Tuples. Tuples 1 A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be.
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.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
For. for loop The for loop in Python is more like a foreach iterative-type loop in a shell scripting language than a traditional for conditional loop.
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.
Built-in Data Structures in Python An Introduction.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
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.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
INTRO2CS Tirgul 4 1. What will we see today?  Strings  Lists  Tuples  Mutable and Immutable  Iterating over sequences  Nested Loops  Shallow and.
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
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.
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
String and Lists Dr. José M. Reyes Álamo.
Python Variable Types.
Tuples and Lists.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
CS-104 Final Exam Review Victor Norman.
From Think Python How to Think Like a Computer Scientist
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Engineering Computing
CISC101 Reminders Quiz 2 this week.
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
CISC101 Reminders Slides have changed from those posted last night…
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
8 – Lists and tuples John R. Woodward.
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Topics Sequences Lists Copying Lists Processing Lists
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Introduction to Programming with Python
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Dictionary.
Python List.
Introduction to Computer Science
Presentation transcript:

For loop Using lists

Revise – what is list? Like strings, lists provide sequential storage through an index offset and access to single or consecutive elements through slices. String ? Lists? Strings consist only of characters and are immutable (cannot change individual elements) lists are flexible container objects that hold an arbitrary number of Python objects.

Continue.. The objects that you can place in a list can include standard types and objects as well as user-defined ones. Lists can contain different types of objects. Lists can be populated, empty, sorted, and reversed. Lists can be grown and shrunk. They can be taken apart and put together with other lists. Individual or multiple items can be inserted, updated, or removed at will.

List and tuple??? The key difference is that tuples are immutable, i.e., read-only, so any operators or functions that allow updating lists, such as using the slice operator on the left-hand side of an assignment, will not be valid for tuples.

Create and Assign Lists Creating lists is as simple as assigning a value to a variable. You handcraft a list (empty or with elements) and perform the assignment. Lists are delimited by surrounding square brackets ( [ ] ).

examples aList = [123, 'abc', 4.56, ['inner', 'list'], 7-9j] >>> print aList [123, 'abc', 4.56, ['inner', 'list'], (7-9j)] >>> anotherList = [None, 'something to see here'] >>> print anotherList [None, 'something to see here'] >>> aListThatStartedEmpty = [] >>> print aListThatStartedEmpty [] >>> list('foo') ['f', 'o', 'o']

Access Values in Lists use the square bracket slice operator ([ ] ) along with the index or indices. >>> aList[0] 123 >>> aList[1:4] ['abc', 4.56, ['inner', 'list']] >>> aList[:3] [123, 'abc', 4.56] >>> aList[3][1] 'list'

Update Lists update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method

example >>> aList [123, 'abc', 4.56, ['inner', 'list'], (7-9j)] >>> aList[2] 4.56 >>> aList[2] = 'float replacer' [123, 'abc', 'float replacer', ['inner', 'list'], (7-9j)] >>> >>> anotherList.append("hi, i'm new here") >>> print anotherList [None, 'something to see here', "hi, i'm new here"] >>> aListThatStartedEmpty.append('not empty anymore') >>> print aListThatStartedEmpty ['not empty anymore'] Replace this words at index 2

Remove List Elements and Lists To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method

Examples: >>> aList [123, 'abc', 'float replacer', ['inner', 'list'], (7-9j)] >>> del aList[1] [123, 'float replacer', ['inner', 'list'], (7-9j)] >>> aList.remove(123) ['float replacer', ['inner', 'list'], (7-9j)]

List Type Operators and used in for loop >>> [ i * 2 for i in [8, -2, 5] ] [16, -4, 10] >>> [ i for i in range(8) if i % 2 == 0 ] [0, 2, 4, 6]

example >>> nameList = ['Donn', 'Shirley', 'Ben', 'Janice', ... 'David', 'Yen', 'Wendy'] >>> for i, eachLee in enumerate(nameList): ... print "%d %s Lee" % (i+1, eachLee) ... 1 Donn Lee 2 Shirley Lee 3 Ben Lee 4 Janice Lee 5 David Lee 6 Yen Lee 7 Wendy Lee

Example: >>> [x ** 2 for x in range(6)] [0, 1, 4, 9, 16, 25] list >>> for x in range(6): ... x=x**2 ... print x ... 1 4 9 16 25 >>>

Use for to access data in list >>> for value in [1,2,3]: ...     print value ... 1 2 3

Example using for to display elements in list >>> list=['abc',123,'hello'] >>> for i in list: ... print i ... abc 123 hello >>> for i in ('abc'): a b c What the different?

Example : displaying two lists using for loop >>> list1=[1,2,3] >>> list2=[4,5,6] >>> for i in list1+list2: ... print i ... 1 2 3 4 5 6

List, for loop and break >>> list1=[1,2,3,4] >>> list2=[5,6,7,8] >>> for i in list1+list2: ... if i == 4: ... break ... print i ... 1 2 3

for loop and break: >>> a=[1,2,3,4,5] >>> for i in a: ... if i ==3: ... break ... print i ... 1 2

For loop and continue >>> a=[1,2,3,4,5] >>> for i in a: ... if i ==3: ... continue ... print i ... 1 2 4 5