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

Content added Content deleted
imported>Jesstess
imported>Jesstess
Line 614: Line 614:
* They do some useful bit of work.
* They do some useful bit of work.
* They let us re-use code without having to type it out each time.
* 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.
* They take input and possibly produce output (we say they <b>return</b> a value). You can assign a variable to this output.
* You call a function by using its name followed by the input in parenthesis.
* You call a function by using its name followed by the input in parenthesis.


Line 625: Line 625:


Running this assigns the type of <code>my_var</code> -- a boolean -- to <code>x</code>.
Running this assigns the type of <code>my_var</code> -- a boolean -- to <code>x</code>.

We also saw the <code>str</code> function, which returns a string representations of an objects so we can print it. For example:

<pre>
age = str(23)
print "I am ", age, "years old."
</pre>



We can right our own functions to encapsulate bits of useful work so we can reuse them. Here's how you do it:
We can right our own functions to encapsulate bits of useful work so we can reuse them. Here's how you do it: