VPython Functions: Difference between revisions

From Physics Book
Jump to navigation Jump to search
(Added basic python function syntax)
Line 12: Line 12:
==Basic Functions==
==Basic Functions==


===Basic Python function syntax===


Basic Python function syntax:
Functions at a high level are used to perform a certain task. They can take anywhere number of inputs and can return 0 or 1 outputs.
Functions can be defined with the <code>def</code> keyword followed by the function name and a colon.
The <code>return</code> keyword can be used for a function to "give back" a result.


  def functionName(argumentOne, argumentTwo):
  def functionName(argumentOne, argumentTwo):
Line 19: Line 22:
     return functionReturnValue
     return functionReturnValue


Example function that adds three numbers together
===Example function===


# This function adds three numbers together and gives back the result using the return keyword
  def addNumbers(a, b, c):
  def addNumbers(a, b, c):
     return a + b + c
     return a + b + c


Example use of that function
===Example use of a function===
 
  # Calls the function addNumbers with the arguments 1, 2 and 4
  seven = addNumbers(1, 2, 4)
result = addNumbers(1, 2, 4)
 
===Vectors===
vector = vector(x,y,z)
===Spheres===
sphere = sphere(pos=POS, color=color.COLOR, radius=RADIUS )
POS: a vector giving the sphere location
COLOR: Options can be found on http://matplotlib.org/examples/color/named_colors.html
RADIUS: The radius of the sphere (usually given by the lab instructions)
===Arrows===
arrow = arrow(pos=POS, axis=AXIS color=color.COLOR)
POS: where the head of the arrow is to be placed
AXIS: the line that we want the axis to go along (generally some position - * position)
COLOR: Options can be found on http://matplotlib.org/examples/color/named_colors.html
 


==Connectedness==
==Connectedness==

Revision as of 14:29, 5 December 2015

Created by Natalie Standish Traded to Kevin Randrup

Introduction to functions in Python and applying them to write shorter and more understandable code.

The Main Idea

The main idea of this page is to serve as an aid for future physics students in writing python code during lab. Python, especially for students who are not very experienced with coding, can be a little confusing and sometimes stressful. This page should help give students the information that they would have to look up outside of the lab instructions but all in one place so that they can focus on the physics aspect of the course and not take too much time worrying about the coding in lab.


Basic Functions

Basic Python function syntax

Functions at a high level are used to perform a certain task. They can take anywhere number of inputs and can return 0 or 1 outputs. Functions can be defined with the def keyword followed by the function name and a colon. The return keyword can be used for a function to "give back" a result.

def functionName(argumentOne, argumentTwo):
    functionReturnValue = argumentOne + argumentTwo
    return functionReturnValue

Example function

# This function adds three numbers together and gives back the result using the return keyword
def addNumbers(a, b, c):
    return a + b + c

Example use of a function

# Calls the function addNumbers with the arguments 1, 2 and 4
result = addNumbers(1, 2, 4)

Connectedness

VPython is an important tool in PHYS 2211 and PHYS 2212 because it is able to take a very abstract concept and give a visual explanation to the student.


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

Phython colors: http://matplotlib.org/examples/color/named_colors.html


References

This section contains the the references you used while writing this page http://matplotlib.org/examples/color/named_colors.html n?