VPython Functions: Difference between revisions

From Physics Book
Jump to navigation Jump to search
(Attempt 2 - show/hide block)
Line 37: Line 37:
As a reminder, this is the formula we are modeling:
As a reminder, this is the formula we are modeling:


<math>E=\frac{-q}{||^2}</math>
<math>\overrightarrow{E}=\frac{1}{4πε_0}  \frac{q}{|\overrightarrow{r}|^2} \hat r</math>


First, we ask what do we need to calculate the electric field?
First, we ask what do we need to calculate the electric field?
* <math>q</math> - the charge of the source
* <math>\overrightarrow{r}</math> - the distance from the source to the electric field


{{hidden|Show arguments|q - the charge of the source<br/> r - the distance from the source to the electric field}}
<math>q</math> and <math>\overrightarrow{r}</math> will be the two arguments to our function which will look like this:


  # Calculates the eletric field at a given vector away from the charge
  # Calculates the eletric field at a given vector away from the charge
  def eletricField(q, r):
  def eletricField(q, r):
     eletricConstant = 9e9 # 1/(4 * pi * ε0)
     eletricConstant = 9e9 # 1/(4 * π * ε0)
     return eletricConstant * q * r.norm() / r.mag2
     return eletricConstant * q * r.norm() / r.mag2



Revision as of 17:10, 5 December 2015

Written by Kevin Randrup

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

Summary

Functions at a high level are used to perform a certain task. Functions can accept a number of inputs (also "arguments" or "parameters") and can give back 0 or 1 output values. Furthermore, they make code much easier to understand by encapsulating a task in a single line of code instead of being repeated over and over again.

Whenever you type print(5), you are calling a function called print with an argument of 5.


Basic Functions

Basic Python function syntax

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)

Code Reuse

Writing a function for a task instead of copy and pasting code makes your program much easier to understand and debug. The best way to illustrate this is with an example.

For this example, we will create a function to calculate the electric field created by a point charge.

As a reminder, this is the formula we are modeling:

[math]\displaystyle{ \overrightarrow{E}=\frac{1}{4πε_0} \frac{q}{|\overrightarrow{r}|^2} \hat r }[/math]

First, we ask what do we need to calculate the electric field?

  • [math]\displaystyle{ q }[/math] - the charge of the source
  • [math]\displaystyle{ \overrightarrow{r} }[/math] - the distance from the source to the electric field

[math]\displaystyle{ q }[/math] and [math]\displaystyle{ \overrightarrow{r} }[/math] will be the two arguments to our function which will look like this:

# Calculates the eletric field at a given vector away from the charge
def eletricField(q, r):
    eletricConstant = 9e9 # 1/(4 * π * ε0)
    return eletricConstant * q * r.norm() / r.mag2

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?