PYTHON LISTS What are lists An ordered set

  • Slides: 12
Download presentation
PYTHON LISTS

PYTHON LISTS

What are lists? • An ordered set of elements • A variable with 0

What are lists? • An ordered set of elements • A variable with 0 or more things inside of it • Examples my. List = [8, 6, 7, 5, 3, 0, 9] str. List = [“this”, “a”, “list”, “of”, “words”] combo. List = [3, “blah”, 423]

Creating a list • Brackets [ ] are your friend • Examples: empty. List

Creating a list • Brackets [ ] are your friend • Examples: empty. List = [] print(empty. List) groceries = [“milk”, “eggs”, “yogurt”] print(groceries)

Elements and Indexing • Each slot / thing in a list is called an

Elements and Indexing • Each slot / thing in a list is called an “element” • How many elements are in the list my. List = [3, 5, 7, 8]? • Each element has an “index” or location • JUST LIKE IN STRINGS! • In the my. List above, • 3 is found at the 0 th index • 5 is at the 1 th index • 7? • 8?

Accessing elements • Just like in strings • Access individual elements using brackets •

Accessing elements • Just like in strings • Access individual elements using brackets • Example: int_list=[202, 10, 54, 23] print( int_list[1] + int_list[3] ) int_list[0] = 1 #changing an element print(int_list)

List length • Use len() function to find out the size of a list

List length • Use len() function to find out the size of a list • Examples: my. List = [] print(len(my. List)) my. List 2 = [8, 6, 7, 5, 3, 0, 9] print(len(my. List 2))

List slicing • Access a slice (sub-list) using ranges (just like w/ strings) •

List slicing • Access a slice (sub-list) using ranges (just like w/ strings) • Includes first element in range, excludes last in range • Example: int_list=[202, 10, 54, 23] print(int_list[0: 1]) print(int_list[2: 3]) print(int _list[0: 0]) • Omit values to get start or end of list int_list[: 2] int_list[2: ]

Adding to a list • Example my. List = [5, 6, 87, 9, 2,

Adding to a list • Example my. List = [5, 6, 87, 9, 2, 4] print(my. List) my. List. append(99999) print(my. List)

Deleting from a list • Example my. List = [5, 6, 87, 9, 2,

Deleting from a list • Example my. List = [5, 6, 87, 9, 2, 4] print(my. List) del my. List[2] print(my. List)

Iterating through a list • Use a loop to walk through a list •

Iterating through a list • Use a loop to walk through a list • Example my. List = [4, 3, 2, 6, 7, 8] x=0 while x < len(my. List): print(my. List[x]) x=x+1

Looping through a list to do something useful • What is the algorithm for

Looping through a list to do something useful • What is the algorithm for summing up a list of numbers? total = 0 my. List = [4, 3, 2, 6, 7, 8] x=0 while x < len(my. List): total = total + my. List[x] x=x+1 print(total)

Additional List Resources • Complete library of list functions/methods http: //docs. python. org/release/3. 1.

Additional List Resources • Complete library of list functions/methods http: //docs. python. org/release/3. 1. 3/tutorial/datastructures. html