Boston Python Workshop 3/Friday/Tutorial: Difference between revisions

no edit summary
imported>Jesstess
imported>Jesstess
No edit summary
Line 81:
So now we've seen two data types: integers and floats.
 
I used the term 'function' without explaining what it is -- we'll talk about functions amore lotin more ona Saturdaybit, and write our own, but for now know twothese things:
 
* Functions do some useful bit of work. We save that useful bit of work inside the function so we don't have to type it over and over again every time we want to use it. So, for example, some nice person decided that being able to determine the type of an object was useful, so he or she put the Python code that figures out an object's type into the function <code>type</code>, and now we all get to use it, instead of having to write it ourselves.
* Functions are a lot like functions in math class. You provide input to a function, and it possibly produces output. The <code>type</code> function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output.
* To use a function, write the name of the function followed by an open parenthesis, possibly some data (we call that data the 'arguments' to the function), and then a close parenthesis.
 
So in this case 'type' is the name of the function, and it takes one argument; in the example we first give <code>type</code> an argument of 1 and then give it an argument of 1.0.
 
* Functions are a lot like functions in math class. You provide input to a function, and it possibly produces output. The <code>type</code> function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output.
 
===Command history===
Line 605:
print "Can I interest you in some choice gouda?"
</pre>
 
==Writing Functions==
 
We talked a bit about functions when we introduced the <code>type()</code> function. Let's review what we know about functions:
 
* They do some useful bit of work.
* They let us re-use code without having to type it out each time.
* They take input and possibly produce output. You can assign a variable to this output.
* You call a function by using its name followed by the input in parenthesis. For example:
 
<pre>
my_var = True
x = type(my_var)
</pre>
 
Running this assigns the type of <code>my_var</code> -- a boolean -- to <code>x</code>.
 
We can right our own functions to encapsulate bits of useful work so we can reuse them. Here's how you do it:
 
<b>Step 1: write a function signature</b>
 
A function signature is the keyword <code>def</code>, a space, the name of your function, an open parenthesis, the comma-separated <b>parameters</b> for your function, a close paranthesis, and a colon. Here's what a function signature looks like for a function that takes no arguments:
 
<pre>
def myFunction():
</pre>
 
Here's what a function signature looks like for a function that takes one argument called <code>string</code>:
 
<pre>
def myFunction(string):
</pre>
 
And one for a function that takes two arguments:
 
<pre>
def myFunction(myList, myInteger):
</pre>
 
 
==End of Part 2==
Anonymous user