VPython Lists: Difference between revisions

From Physics Book
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Claimed by Natalie Standish
By Natalie Standish




Line 5: Line 5:




This page is about using lists in VPython to do computing at a scale larger than one or two objects.
This page is to help students that are inexperienced with coding learn about the basic concepts of lists.


==VPython List==
==VPython List==
Line 33: Line 33:
Example: nested = [1,"nest",[1,2]]
Example: nested = [1,"nest",[1,2]]


===Empty Nests===
===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.
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: []




Line 52: Line 54:
nums[3] = 19
nums[3] = 19


===Appending to a List===
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)]






How do we visualize or predict using this topic. Consider embedding some vpython code here [https://trinket.io/glowscript/31d0f9ad9e Teach hands-on with GlowScript]


==Examples==
==Examples==

Revision as of 17:20, 4 December 2015

By Natalie Standish


NOT FINISHED


This page is to help students that are inexperienced with coding learn about the basic concepts of lists.

VPython List

State, in your own words, the main idea for this topic

Types of Nests

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 loops.

Example: nested = [1,"nest",[1,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 list nums = [47,36,11,19]

nums[0] = 47

nums[1] = 36

nums[2] = 11

nums[3] = 19

Appending to a List

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)]



Examples

Be sure to show all steps in your solution and include diagrams whenever possible

Simple

Middling

Difficult

Connectedness

  1. How is this topic connected to something that you are interested in?
  2. How is it connected to your major?
  3. Is there an interesting industrial application?

History

Put this idea in historical context. Give the reader the Who, What, When, Where, and Why.

See also

Are there related topics or categories in this wiki resource for the curious reader to explore? How does this topic fit into that context?

Further reading

Books, Articles or other print media on this topic

External links

[1]


References

This section contains the the references you used while writing this page