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

imported>Jesstess
imported>Adamf
 
(14 intermediate revisions by one other user not shown)
Line 210:
 
===String Concatenation===
 
You can smoosh strings together (called "concatenation") using the '+' sign:
 
<pre>
Line 228 ⟶ 230:
w = "World"
print h + w
print h, w
</pre>
 
Line 239 ⟶ 240:
 
<pre>
print "Hello", 1
print "Hello" + 1
</pre>
Line 273:
 
produces a <code>TypeError</code>. We are telling Python to concatenate a string and an integer, and that's not something Python understands how to do.
 
In the similar expression
 
<pre>
print "Hello", 1
</pre>
 
Python is actually turning the integer 1 into a string before printing, and that's why that concatenation works: Python does know how to concatenate two strings.
 
We can convert an integer into a string ourselves, using the <code>str</code> function:
Line 298 ⟶ 290:
print len("")
fish = "humuhumunukunukuapuaʻa"
length = str(len(fish))
print fish, + " is a Hawaiian fish whose name is ", len(fish),+ length + " characters long."
</pre>
 
Line 366 ⟶ 359:
b = "brown"
c = "fox jumps over the lazy dog"
print "The ", + a * 3, + " " + b * 3, + " " + c
</pre>
 
3.
<pre>
print 2.0 * 123, "2.0" * 123
print 2.0 * 123 + str(2.0) * 123
</pre>
Line 409 ⟶ 401:
 
<ol>
<li>Download the file http://mit.edu/jesstess/www/BostonPythonWorkshop6/nobel.py by right-clicking on it and saying to save it as a ".py" file to your Desktop. The ".py" extension hints that this is a Python script.</li>
<li>Save the file in your Desktop directory.</li>
<li>Open a command prompt, and use the navigation commands (<code>dir</code> and <code>cd</code> on Windows, <code>ls</code>, <code>pwd</code>, and <code>cd</code> on OS X and Linux) to navigate to your home directory. See [[Boston Python Workshop 6/Friday#Goal_.234:_practice_navigating_the_computer_from_a_command_prompt|navigating from a command prompt]] for a refresher on those commands.</li>
<li>Once you are in your home directory, execute the contents of <code>nobel.py</code> by typing
Line 467 ⟶ 458:
</pre>
 
Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i> (e.g. <code>my_string == "Hello"</code>).
 
This is an important idea and can be a source of bugs until you get used to it: <b>= is assignment, == is comparison</b>.
Line 645 ⟶ 636:
If color had been "purple", that code wouldn't have printed anything.
 
<b>Remember! that '=' is for assignment, and '==' is for comparison.</b>
 
==Writing Functions==
Line 752 ⟶ 743:
 
Functions don't have to return anything, if you don't want them to. They usually return something because we usually want to be able to assign variables to their output.
 
==Part 2 Practice==
 
[[File:Detective.png|100px]]
 
Learning about functions opens up a whole new way for us to practice, using the programming site codingbat.com. The big goal of this practice section is to get you thinking about how to solve problems in Python.
 
<div style="font-size:125%">[http://codingbat.com/home/bostonpythonworkshop@gmail.com/PythonIntro &raquo; Please visit our CodingBat page and complete tonight's practice problems &laquo;]</div>
 
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==
Line 770 ⟶ 751:
 
 
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material. All that's left today is to get checked off, and we'll pick up with more Python fundamentals tomorrow morning.
 
 
Anonymous user