Lists: Difference between revisions
Mr. MacKenty (talk | contribs) No edit summary |
Mr. MacKenty (talk | contribs) No edit summary |
||
Line 4: | Line 4: | ||
Please be careful as lists can be thought of as analogous to arrays, but they aren't the same thing. Arrays traditionally have a fixed memory size whilst lists have dynamic memory allocation. When you are working in Python, call a list a list. When you are working in PHP or Javascript, call an array an array. When you are working in C, call for help! | Please be careful as lists can be thought of as analogous to arrays, but they aren't the same thing. Arrays traditionally have a fixed memory size whilst lists have dynamic memory allocation. When you are working in Python, call a list a list. When you are working in PHP or Javascript, call an array an array. When you are working in C, call for help! | ||
Lists, like arrays are indexed starting at zero. The first element in a list is the "zero-th" element. | |||
== Creating a list == | == Creating a list == | ||
Line 18: | Line 20: | ||
# However, it is far more common to slice into a list | # However, it is far more common to slice into a list | ||
print(polishAnimals[2]) | print(polishAnimals[2]) | ||
# there is a lot more to slicing in Python. | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== inserting into a list == | |||
== deleting a list or deleting an element from a list == | == deleting a list or deleting an element from a list == | ||
== finding an element in a list == | == finding an element in a list == |
Revision as of 07:36, 7 August 2017
![](/images/f/f0/Arrows.png)
Programming basics[1]
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].[2]
Please be careful as lists can be thought of as analogous to arrays, but they aren't the same thing. Arrays traditionally have a fixed memory size whilst lists have dynamic memory allocation. When you are working in Python, call a list a list. When you are working in PHP or Javascript, call an array an array. When you are working in C, call for help!
Lists, like arrays are indexed starting at zero. The first element in a list is the "zero-th" element.
Creating a list[edit]
# The code below creates a list named polishAnimals
polishAnimals = ['Bison', 'Moose', 'Deer', 'Lynx', 'Wolf', 'Beaver', 'Otter']
accessing a list[edit]
# If you want to print a list (kind of ugly) you can simply:
print(polishAnimals)
# However, it is far more common to slice into a list
print(polishAnimals[2])
# there is a lot more to slicing in Python.
inserting into a list[edit]
deleting a list or deleting an element from a list[edit]
finding an element in a list[edit]
sorting a list[edit]
Do you understand this?[edit]
Standards[edit]
- Construct algorithms using pre- defined sub-programmes, one- dimensional arrays and/or collections.