# Lists & Algorithms <!-- .element: class="r-fit-text" --> Week 9 --- ## Review  <!-- .element: style="height:400px" --> -- What happens to RAM when the power goes off? It is forgotten <!-- .element: class="fragment green" --> -- What does an operating system do? It lets programs use the hardware <!-- .element: class="fragment green" --> -- What are some roles of the operating system? Users, files, multitasking, drivers, VRAM <!-- .element: class="fragment green" --> -- Which of these is a Windows path? `C:\Users\Rylan` `/Users/Rylan` `C:\Users\Rylan` <!-- .element: class="fragment green" --> --- ## Lists <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- A variable holds **one** value ```py score = 95 ``` -- What if you need to keep track of dozens of numbers? Use a list <!-- .element: class="fragment green" --> -- A **list** holds values in order ```py scores = [85, 92, 78] ``` NOTES: Week 2 showed `[1, 2, 3]` and called it a compound type. Square brackets, commas between the values. -- Each spot has an **index** Indexes start at **0** | Index | Score | | --- | --- | | 0 | 85 | | 1 | 92 | | 2 | 78 | NOTES: The index is how far the item is from the start. The first item is zero steps away. -- ```py scores = [85, 92, 78] print(scores[0]) print(scores[2]) ``` ``` 85 78 ``` <!-- .element: class="fragment green" --> -- `len` is how many items are in the list ```py print(len(scores)) ``` ``` 3 ``` <!-- .element: class="fragment green" --> NOTES: `len` is short for length. -- The last index is one less than the length 0, 1, 2, 3, 4 <!-- .element: class="fragment" --> That is 5 items <!-- .element: class="fragment" --> -- ```py print(scores[5]) ``` `IndexError: list index out of range` <!-- .element: class="fragment red" --> NOTES: There is no index 5. The last score is at index 4. You can also write `scores[-1]` for the last item. Counting backward is not needed for the project. -- You can replace one item ```py scores = [2, 3, 5, 8] scores[0] = 100 print(scores) ``` ``` [100, 3, 5, 8] ``` <!-- .element: class="fragment" --> -- A list is a **data structure** A way of organizing values <!-- .element: class="fragment" --> NOTES: An array is the same idea. Python calls it a list. -- A string has similar operations A string is **NOT** a list <!-- .element: class="fragment" --> -- ```py [2] message = "Hello, World!" print("First character is", message[0]) ``` ``` H ``` <!-- .element: class="fragment" --> -- ```py [2] message = "Hello, World!" print("Length is", len(message)) ``` ``` 5 ``` <!-- .element: class="fragment" --> -- ```py [2] message = "Hello, World!" message[len(message) - 1] = "?" print(message) ``` ``` Hello, World? ``` <!-- .element: class="fragment" --> --- ## Loops <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> NOTE: we will cover loops in detail next week -- Printing every score by hand ```py print(scores[0]) print(scores[1]) print(scores[2]) print(scores[3]) print(scores[4]) ``` -- That breaks when the list changes length -- A **for loop** runs once for each item ```py for score in scores: print(score) ``` NOTES: `score` is 85, then 92, then 78, then 95, then 88. Same colon and indent rules as `if` and functions. -- What is wrong with this code? ```py for score in scores print(score) ``` Missing `:` <!-- .element: class="fragment green" --> -- What is wrong with this code? ```py for score in scores: print(score) ``` The loop body must be indented <!-- .element: class="fragment green" --> -- What does this print? ```py scores = [85, 92, 78] for score in scores: print(score) ``` ``` 85 92 78 ``` <!-- .element: class="fragment green" --> --- ## Algorithms <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- An **algorithm** is a step-by-step procedure The list **stores** the scores The algorithm **finds** an answer <!-- .element: class="fragment" --> NOTES: A recipe. The data structure is the ingredients. The algorithm is the steps. -- Given a stack of bills, how do you count the total? ```py [1, 1, 2, 5, 10, 10, 50] ``` -- Find the sum 1. Keep track of total, starting at 0 <!-- .element: class="fragment" --> 2. Look at every number <!-- .element: class="fragment" --> 3. Add number to total <!-- .element: class="fragment" --> -- ```py [2-4] bills = [1, 1, 2, 5, 10, 10, 50] total = 0 for bill in bills: total += bill print(total) ``` -- Find the highest number in a list 1. Start with the first number <!-- .element: class="fragment" --> 2. Look at every number <!-- .element: class="fragment" --> 3. If this one is higher, remember it instead <!-- .element: class="fragment" --> -- ```py [2-5] scores = [85, 92, 78, 95, 88] highest = scores[0] for score in scores: if score > highest: highest = score print(highest) ``` -- `scores = [85, 92, 78, 95, 88]` | Score | Highest so far | | --- | --- | | 85 | <span class="fragment">85</span> | | 92 | <span class="fragment">92</span> | | 78 | <span class="fragment">92</span> | | 95 | <span class="fragment">95</span> | | 88 | <span class="fragment">95</span> | --- ## Exercise <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- **Vowel Counter** - Ask use for a message - Print the number of vowels in that message For example, `"mississippi"` would be 4 -- ```py def count_vowels(message): vowels = ['a', 'e', 'i', 'o', 'u'] total = 0 for letter in message: if letter in vowels: total += 1 return total message = input("Enter a message: ") vowel_count = count_vowels(message.lower()) print(f'There are {vowel_count} vowels in "{message}"') ``` ---  <!-- .element: style="height:400px" -->