|
| 1 | +# ThinkPython: Lists |
| 2 | + |
| 3 | +Now we have learned to save values to variables. |
| 4 | + |
| 5 | +```py |
| 6 | +thing = 'Hello World!' |
| 7 | +``` |
| 8 | + |
| 9 | +Then we can do whatever we want with the variable. |
| 10 | + |
| 11 | +```py |
| 12 | +print(thing) |
| 13 | +``` |
| 14 | + |
| 15 | +But what if you have a lot of values? You can just make a lot of |
| 16 | +variables... |
| 17 | + |
| 18 | +```py |
| 19 | +thing1 = 'Hello World!' |
| 20 | +thing2 = 'hi' |
| 21 | +thing3 = 123 |
| 22 | +thing4 = 3.14 |
| 23 | +thing5 = 42 |
| 24 | +thing6 = 'This is yet another thing.' |
| 25 | +thing7 = 'Python is fun!' |
| 26 | + |
| 27 | +print(thing1) |
| 28 | +print(thing2) |
| 29 | +print(thing3) |
| 30 | +print(thing4) |
| 31 | +print(thing5) |
| 32 | +print(thing6) |
| 33 | +print(thing7) |
| 34 | +``` |
| 35 | + |
| 36 | +...or you can use a list and keep everything in one variable. |
| 37 | + |
| 38 | +```py |
| 39 | +things = ['Hello World!', 'hi', 123, 3.14, 42, |
| 40 | + 'This is yet another thing.', 'Python is fun!'] |
| 41 | +for thing in things: |
| 42 | + print(thing) |
| 43 | +``` |
| 44 | + |
| 45 | +[Read more about lists in ThinkPython |
| 46 | +here.](http://greenteapress.com/thinkpython2/html/thinkpython2011.html) |
| 47 | +Skip the chapter "10.7 Map, filter and reduce" and the excercises. You |
| 48 | +would need to know how to define functions using the `def` keyword, but |
| 49 | +we haven't talked about that yet. |
| 50 | + |
| 51 | +## Summary |
| 52 | + |
| 53 | +- Lists are a way to store multiple values in one variable. We can |
| 54 | + create lists by putting whatever we want inside [square brackets], |
| 55 | + for example, `our_list = []` creates an empty list. |
| 56 | +- Never do `list = ...`. `list` is a built-in class, and it's used for |
| 57 | + converting other values to lists, like `list_of_thingy = list(thingy)`. |
| 58 | + If we do `list = something`, then `list(thingy)` will probably do |
| 59 | + something else than we want it to do. |
| 60 | +- When we have created a list, we can slice it. For example, |
| 61 | + `our_list[2:]` results in a new list with everything in the |
| 62 | + original list except the first two elements. Negative indexes start |
| 63 | + from the end of the list, for example, `our_list[-2:]` is a list of |
| 64 | + the last two elements. |
| 65 | +- We can also index lists, `our_list[0]` is the first element in the |
| 66 | + list. Non-negative indexes start at zero, and negative indexes |
| 67 | + start at -1. |
| 68 | +- You can assign to indexes and slices like `some_list[0] = 'hi'`, or |
| 69 | + delete them like `del some_list[:2]`. |
| 70 | +- `a = b` does not create a copy of b. |
| 71 | + |
| 72 | + ```py |
| 73 | + >>> a = [] |
| 74 | + >>> b = a # this does not copy anything |
| 75 | + >>> b += [1, 2, 3] |
| 76 | + >>> a |
| 77 | + [1, 2, 3] |
| 78 | + >>> |
| 79 | + ``` |
| 80 | + |
| 81 | + If you want a copy, use the `.copy()` list method: |
| 82 | + |
| 83 | + ```py |
| 84 | + >>> a = [] |
| 85 | + >>> b = a.copy() |
| 86 | + >>> b += [1, 2, 3] |
| 87 | + >>> a |
| 88 | + [] |
| 89 | + >>> |
| 90 | + ``` |
| 91 | + |
| 92 | +*** |
| 93 | + |
| 94 | +You may use this tutorial freely at your own risk. See [LICENSE](LICENSE). |
| 95 | + |
| 96 | +[Back to the list of contents](README.md) |
0 commit comments