Anonymous user
Boston Python Workshop 3/Friday/Tutorial: Difference between revisions
Boston Python Workshop 3/Friday/Tutorial (view source)
Revision as of 13:12, 24 September 2011
, 12 years agoProtected "Boston Python Workshop 3/Friday/Tutorial" ([edit=autoconfirmed] (indefinite) [move=autoconfirmed] (indefinite))
imported>Jesstess (→Types) |
imported>Jesstess m (Protected "Boston Python Workshop 3/Friday/Tutorial" ([edit=autoconfirmed] (indefinite) [move=autoconfirmed] (indefinite))) |
||
(18 intermediate revisions by 3 users not shown) | |||
Line 95:
* Functions encapsulate 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
* To use a function, write the name of the function followed by an open parenthesis, what the function takes as input (we call that input the <b>arguments</b> to the function), and then a close parenthesis.
Line 102:
===Command history===
Stop here and try hitting the Up arrow on your keyboard a few times. The Python <b>interpreter</b> saves a history of what you've entered, so you can arrow up to old commands and hit Return to re-run them!
==Variables==
Line 208:
<pre>
</pre>▼
Like with the math data types above, we can use the <code>type</code> function to check the type of
<pre>▼
type("Hello")▼
type(1)▼
type("1")▼
</pre>
Line 215 ⟶ 223:
<pre>
print "Hello", "World"▼
</pre>
<pre>▼
name = "Jessica"
</pre>▼
===Printing===
You can print strings using <code>print</code>:
<pre>
Line 229 ⟶ 245:
my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
print my_string
▲</pre>
▲Like with the math data types above, we can use the <code>type</code> function to check the type of these strings:
▲<pre>
▲type("Hello")
▲type(1)
▲type("1")
</pre>
Line 283 ⟶ 291:
</pre>
Python is actually turning the integer 1 into a string before printing, and that's why
We can convert an integer into a string ourselves, using the <code>str</code> function:
Line 291 ⟶ 299:
</pre>
Like the <code>type</code> function from before, the <code>str</code> function takes 1 argument. In the above example it took the integer 1. <code>str</code> takes
There's another useful function that works on strings called <code>len</code>. <code>len</code> returns the length of a string as an integer:
Line 321 ⟶ 329:
</pre>
This gives us another <b>traceback</b>, for a new kind of error, a <code>SyntaxError</code>. When Python looks at that expression, it sees the string 'I' and then
<code>m a happy camper'</code>
Line 685 ⟶ 693:
A <b>function signature</b> tells you how the function will be called. It starts with the keyword <code>def</code>, which tells Python that you are defining a function. Then comes a space, the name of your function, an open parenthesis, the comma-separated input <b>parameters</b> for your function, a close parenthesis, and a colon. Here's what a function signature looks like for a function that takes no arguments:
<code>def myFunction():</code>▼
▲<pre>
▲def myFunction():
▲</pre>
Here's what a function signature looks like for a function that takes one argument called <code>string</code>:
<code>def myFunction(string):</code>▼
▲def myFunction(string):
And one for a function that takes two arguments:
<code>def myFunction(myList, myInteger):</code>▼
▲def myFunction(myList, myInteger):
Parameters should have names that usefully describe what they are used for in the function.
Line 778 ⟶ 780:
Learning about functions opens up a whole new way for us to practice, using the programming site [http://codingbat.com codingbat.com]. The big goal of this practice section is to get you thinking about how to solve problems in Python.
Please visit http://codingbat.com/home/jessica.mckellar@gmail.com/Friday to complete the practice problems. You don't have to create a CodingBat account to do the exercises, but if you do create an account it'll keep track of which problems you've completed and other statistics.
==End of Part 2==
|