class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture \12\:] .title[More fun with lists] ] .footer[[/lecture/12/](/lecture/12/)] --- # Lists so far: ### Creating ### Iterating ### Indexing and slicing --- # Lists today: ### List-related functions ### List comprehensions ### The `in` keyword --- # List-related functions #### These functions get information from iterables (like lists!): ??? Try using the `help()` function to learn a bit about each of these functions... -- .floatleft[ * `all()` * `any()` * `iter()` (optional) * `len()` ] -- .floatleft[ * `max()` * `min()` * `next()` (optional) * `sorted()` ] --- # Why lists? -- ### Can represent arbitrary numbers of things * Not stuck with pre-defined (_static_) problem sizes -- * Remember [exercise 3 (EOPA)](../../exercise/3)? -- What if you only have seven or eight numeric grades? -- ### Can be a more elegant representation -- * Even if we know the problem size, nice to be general! --- # Exercise 3 .column[ | Subject | Course | |---------|---------| | CHEM | 1050 | ENGI | 1010, 1020, 1030, 1040 | ENGL | 1090 | MATH | 1001, 2050 | PHYS | 1051 ] .column[ > Calculate the Engineering One **average** and **promotability** of a student, > given their Engineering One grades. ] ??? Let's do this exercise again, this time using **lists** to make for a simpler, more re-usable implementation. ## Addition operator: We can **add** lists together using the **`+` operator**. This is known as **list concatenation**, and it's one way to make lists **change size**. --- # A very common pattern: ```python total = 0 for x in something: if x > something_else: total += x ``` -- ```python passing_grades = [] for grade in courses: if grade >= 55: passing_grades += [grade] ``` -- ```python aggregate = ... for x in something: if f(x): aggregate += g(x) # or *=, or ... ``` --- # A common solution #### List comprehension: _an elegant tool for a civilized age_ -- * syntax for constructing a list from something iterable -- * Can _filter_ and _transform_ individual elements -- #### e.g., The first ten squares of even numbers: -- ```python squares = [] for i in range(2, 21): if i % 2 == 0: squares += [i*i] ``` --- # List comprehensions ### Syntax: .floatright[ ```python [i*i for i in range(2, 21) if i%2 == 0] ``` ] * `[` — opening bracket -- * _expression_ -- * `for` _loop parameter_ `in` _iterable_ -- * `if` _condition_ -- * `]` — closing bracket --- # A familiar problem ### Problem 1 from [Project Euler](https://projecteuler.net): > Find the sum of all the multiples of 3 or 5 below 1000. -- ```python total = 0 for n in range(1000): if n % 3 == 0 or n % 5 == 0: total += n ``` -- ```python total = sum([n for n in range(1000) if n % 3 == 0 or n % 5 == 0]) ``` --- # More list comprehensions ### Possible to get more complex: * Can have more than one `for` loop: ```python [i+j for i in range(10) for j in range(10,20)] ``` * Not required for this course! * Further details available at [python.org](python.org) ([5.1.3 in the Data Structures tutorial](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)) --- # One more thing ### Remember the `in` keyword? ```python for i in range(10): ``` -- ### Can also use `in` as an operator! ```python if 100 in grades: print('Wow, well done!') ``` -- ```python is_cool_guy = 'Jon' in username if letter in 'EAIONRTLSU': # ... ``` ??? This new syntax may be helpful for [assignment 2](../../assignment/2)... --- # Summary ### How cool are lists? ### List-related functions ### List comprehensions ### `in` keyword --- class: big, middle (here endeth the lesson)