This shows you the differences between two versions of the page.
Next revision | Previous revision Next revisionBoth sides next revision |
184_notes:lists [2022/04/20 15:07] – created woodsna1 | 184_notes:lists [2022/05/06 21:59] – woodsna1 |
---|
</code> | </code> |
| |
You can think of a list like a column in an excel spreadsheet, it has many fields that can each be filled with an object. You can also access each bucket in a list to update or retrieve the object within it. One way is by using list indexing. Indices in a Python list start at 0 for the first element: | You can think of a list like an excel spreadsheet, it has many rows (each representing an object), and each row has several columns (representing the attributes of the object). You can also access each bucket in a list to update or retrieve the object within it. One way is by using list indexing. Indices in a Python list start at 0 for the first element: |
| |
<code> | <code> |
</code> | </code> |
| |
This works great for reading each element in a list. In the above code, "number" is a temporary variable. On each iteration of the for loop, number is assigned to the next element in the list "list_of_numbers" until none are left. Within the for loop, you can use the variable "number" to access the current element in the list. **The above loop does not work for changing values in the list** to change values, we need to use a different kind of loop. Usually, the best choice is to use a while loop as below: | This works great for reading each element in a list. In the above code, "number" is a temporary variable. On each iteration of the for loop, number is assigned to the next element in the list "list_of_numbers" until none are left. Within the for loop, you can use the variable "number" to access the current element in the list. **The above loop does not work for changing values in the list**. To change values, we need to use a different kind of loop. Usually, the best choice is to use a while loop as below: |
| |
<code> | <code> |