class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture \10\:] .title[Ranges and lists] ] .footer[[/lecture/10/](/lecture/10/)] --- # Recall: iteration ### Walking through a bunch of things, _one at a time_ ### IRL: design _iterations_ ### Computing: loops! .floatright[ ```python for letter in 'Jonathan Anderson': print(letter) ``` ] * get "next" letter, use it * ... until no more letters #### Later: lists, arrays and `iter()` --- # Today ### Iterating over integer ranges ### Lists ### List operations --- # Motivation ### Example looping problem: 1. What is the sum of the squares of the first 10 whole numbers (i.e., $1^2 + 2^2 + 3^2 + \dots$)? -- 2. What is the sum of the squares of the first $n$ whole numbers? --- # Iterating over integers -- .column[ ```python total = 0 i = 1 while i <= n: total += i*i i += 1 ``` ] -- .column[ $$ \sum_{i=1}^{n} {i^2} $$ ] -- .column[ If only we could write something a bit more like this mathematical expression! ] -- * Too bad we can't write "$\textrm{for } i \in [2,n]$"... -- * or can't we? --- # Home on the `range()` -- ### `range()` gives us a _range_ of integers -- ### A range can be _iterated_ over (just like a string) -- .floatleft[ ### 1 to 100: ```python for n in range(100): print(n) ``` ] -- .floatleft[ ### 10 to 20: ```python for n in range(10, 20): print(n) ``` ] -- .floatleft[ ### 1000 to 2000, by 500s: ```python for n in range (1000, 2000, 500): print(n) ``` ] --- # Help on the `range()` #### If we type `help(range)`: -- ```python class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return an object that produces a sequence of integers from start (inclusive) | to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. | start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. | These are exactly the valid indices for a list of 4 elements. | When step is given, it specifies the increment (or decrement). ``` -- * some details here that we don't understand (yet) -- * big picture: two versions of `range()`; more detail later --- # Mo' Python, Mo' Problems ## [Project Euler](https://projecteuler.net) -- e.g., Problem 1: > Find the sum of all the multiples of 3 or 5 below 1000. -- ##### Your solution can be simplified with `range()` --- # Other collections ### What about non-sequential collections of values? -- * student IDs for everyone in the class -- * grades for everyone in the class -- * each day's temperature -- * whether or not class has been cancelled each day --- # Lists ```python student_ids = [202012345, 202098765] grades = [99.7, 94.8, 96.2] temperatures = [-2, -4, -20, 6] cancelled = [True, True, False, True] ``` -- ### Syntax: -- * `[` -- — opening **bracket** -- * zero or more expressions -- — **separated by commas** -- * `]` -- — closing **bracket** --- # List operations ```python ["Engineering", 1020] ``` ### Creation -- ### Mutation -- * for now: `my_list += ["and", 1030]` -- * later: methods (`my_list.append(...)`) -- ### Iteration --- # Iterating over lists ### Just like iterating over ranges! -- ```python grades = [99.7, 94.8, 96.2] for grade in grades: if grade >= 55: print(grade, ' will count towards promotion average') ``` -- ```python for temp in [-2, -4, -20, 6]: if temp < 10: print('brrrr') ``` --- # Iterators ### Common syntax not a coincidence: .floatleft[ ```python for c in "hello world": print(c > 'a') ``` ] .floatleft[ ```python for n in range(10): print(n*n) ``` ] .floatleft[ ```python for temp in [-2, -20, 6]: print(temp) ``` ] -- <br/> <br/> .floatright[ ```python i1 = iter("hello world") i2 = iter(range(10)) i3 = iter([-2, -20, 6]) ``` ] * All use _iterators_ * Can use `for` loop with _any_ iterator * We'll see more iterators later --- # Indexing -- ### Indices in math: $$ \textrm{Given } X = \left\\{ 0, \frac{\pi}{2}, \pi, \frac{3 \pi}{2} \right\\} $$ What is $X_2$? -- $X_0 = 0$ -- , $X_1 = \frac{\pi}{2}$ -- , $X_2 = \pi$, ... -- ### Lists and strings (and more) can be _indexed_ -- ```python X = [0, pi/2, pi, 3*pi/2] s = X[2] / pi ``` --- # Python indices ### Start counting from 0 -- ### Will cause an error if _out of range_ -- ### Can be _negative!_ -- * start counting from the back * range checks still apply! --- # Names ### Example: ```python names = ['Jonathan', 'Anderson'] ``` -- ```python first_name = names[0] ``` -- ```python initial = first_name[0] another_way_to_write_initial = names[0][0] ``` -- ### What's the last letter of your last name? --- # Summary ### `range()` ### Lists -- ## Go play with [Project Euler](https://projecteuler.net)! --- class: big, middle (here endeth the lesson)