Presentation is loading. Please wait.

Presentation is loading. Please wait.

For loop Using lists.

Similar presentations


Presentation on theme: "For loop Using lists."— Presentation transcript:

1 For loop Using lists

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

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

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

5 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 ( [ ] ).

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

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

8 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

9 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

10 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

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

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

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

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

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

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

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

18 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

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

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


Download ppt "For loop Using lists."

Similar presentations


Ads by Google