VPython Lists: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
<math> </math> | |||
By Natalie Standish | By Natalie Standish | ||
Line 34: | Line 34: | ||
Since python does not have a matrix data structure, nested lists are commonly used to represent matrices. For example, a 5x5 matrix can be represented in python as a list of five elements where each of those elements is another list that corresponds to a row in the matrix. | Since python does not have a matrix data structure, nested lists are commonly used to represent matrices. For example, a 5x5 matrix can be represented in python as a list of five elements where each of those elements is another list that corresponds to a row in the matrix. | ||
For example, consider the following | For example, consider the following 3 x 3 matrix: | ||
<math> | |||
\begin{bmatrix} | |||
5 & 12 & 1 \\ | |||
23 & 9 & 73 \\ | |||
0 & 61 & 2 | |||
\end{bmatrix} | |||
</math> | |||
We can represent the above matrix using nested lists as such: | |||
<code> | |||
matrix = [[5, 12, 1],[23, 9, 73],[0, 61, 2]] | |||
</code> | |||
===Empty Lists=== | ===Empty Lists=== | ||
Line 49: | Line 61: | ||
Indexing a list is how we can identify the value at a certain spot in the list. The first component of the list is the 0th index and then it goes up by one until the last component (the nth-1 index in an list of length n). When trying to find the nth index, we use this notation: listName[n] | Indexing a list is how we can identify the value at a certain spot in the list. The first component of the list is the 0th index and then it goes up by one until the last component (the nth-1 index in an list of length n). When trying to find the nth index, we use this notation: listName[n] | ||
For example, consider the list nums = [47,36,11,19] | For example, consider the following list: | ||
nums = [47,36,11,19] | |||
The following commands allow us to get access to the individual list elements: | |||
nums[0] # returns 47 | |||
nums[1] # returns 36 | |||
nums[2] # returns 11 | |||
nums[3] # returns 19 | |||
Indexing into nested lists follows the same logic as indexing into one dimensional lists. | |||
For example, consider the following nested list: | |||
matrix = [[5, 12, 1],[23, 9, 73],[0, 61, 2]] | |||
This list has three elements, each of which is another list. It follows then that: | |||
<code> | |||
matrix[0] returns [5, 12, 1] | |||
</code> | |||
To obtain an individual element in a nested list, we simply index twice: | |||
matrix[0][0] # returns 5 | |||
matrix[0][1] # returns 12 | |||
matrix[0][2] # returns 1 | |||
===Appending to a List=== | ===Appending to a List=== |
Revision as of 20:52, 11 April 2016
[math]\displaystyle{ }[/math]
By Natalie Standish
Claimed By Omar Hayek
This page is to help students that are inexperienced with coding learn about the basic concepts of lists.
Lists are one of the most, if not the most, ubiquitous and powerful tools in Python. Their usefulness comes from the fact that they can be used to hold any amount of data and that data can be of any type. As such, lists are generally the go-to method for storing information.
Types of Lists
Lists of Numbers
The most basic list is one that has numbers in it. The list of numbers may contain integers, floats, or both.
Example: numList = [1,2,3.4,5]
Lists of Strings
Lists can also be comprised of words (strings). These words must have "" surrounding them for the word to be considered a string and for the computer to accept it.
Example: strList = ["lists","are","super","cool"]
Lists of Both
Lists don't have to be made up of the same type of variable.
For example, the list ["twenty",20,5,"five"] is a valid list.
Lists of Lists
Lists can also contain lists within themselves. These are called nested lists.
Example: nested = [1,"nest",[1,2]]
Since python does not have a matrix data structure, nested lists are commonly used to represent matrices. For example, a 5x5 matrix can be represented in python as a list of five elements where each of those elements is another list that corresponds to a row in the matrix.
For example, consider the following 3 x 3 matrix:
[math]\displaystyle{ \begin{bmatrix} 5 & 12 & 1 \\ 23 & 9 & 73 \\ 0 & 61 & 2 \end{bmatrix} }[/math]
We can represent the above matrix using nested lists as such:
matrix = [[5, 12, 1],[23, 9, 73],[0, 61, 2]]
Empty Lists
Empty nests are commonly used to initiate a variable and avoid program errors when calling upon that empty list. They generally have variables appended into them and grow throughout the program.
Example: []
Properties of Lists
Indices
Indexing a list is how we can identify the value at a certain spot in the list. The first component of the list is the 0th index and then it goes up by one until the last component (the nth-1 index in an list of length n). When trying to find the nth index, we use this notation: listName[n]
For example, consider the following list:
nums = [47,36,11,19]
The following commands allow us to get access to the individual list elements:
nums[0] # returns 47 nums[1] # returns 36 nums[2] # returns 11 nums[3] # returns 19
Indexing into nested lists follows the same logic as indexing into one dimensional lists.
For example, consider the following nested list:
matrix = [[5, 12, 1],[23, 9, 73],[0, 61, 2]]
This list has three elements, each of which is another list. It follows then that:
matrix[0] returns [5, 12, 1]
To obtain an individual element in a nested list, we simply index twice:
matrix[0][0] # returns 5 matrix[0][1] # returns 12 matrix[0][2] # returns 1
Appending to a List
The append function takes a previously defined/calculated value and adds it to a pre-existing list. This generally occurs in a loop and is very useful when you are trying to find patterns in the behavior of a non-static variables.
Example Code:
emptyList = [] t = 0 while t < 10: t = t + 1 emptyList.append(t)
The results would give emptyList = [1,2,3,4,5,6,7,8,9,10]
Relation to PHYS 2212
Imagine you are trying to detect a pattern in the speed of a moving electron in a 30 second span of time. The code you write calculates on a loop a numerical value of the speed of the electron every second. You previously defined an empty list (elecSpeed = []). The append function takes the result calculated (Let's call this x) by the program and adds it to the list that is being appended. By putting this line of code inside of the loop, your program can create a list containing 30 calculated speeds of the electron from time t = 0 to t = 29.
Example code:
elecSpeed = [] while t < 30: CODE THAT CALCULATES SPEED, x elecSpeed.append(x)
RESULTS: elecSpeed = [x(at time t=0),x(at time t=1),x(at time t=2),..........x(at time t=29)]
Further Reading
http://openbookproject.net/thinkcs/python/english3e/lists.html
References
http://openbookproject.net/thinkcs/python/english3e/lists.html