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

imported>Jesstess
imported>Adamf
 
(15 intermediate revisions by one other user not shown)
Line 67:
 
The two previous expressions produce the same result. You only need to make one of the numbers in each fraction have a decimal. When the Python interpreter goes to do the division, it notices that one of the numbers in the fraction cares about decimals and says "that means I have to make the other number care about decimals too".
 
===Modulus===
 
The modulus operator <code>%</code> gives you the remainder after division. For example, 6 % 3 returns 0 because 3 goes into 6 twice, with nothing left over. 5 % 3 returns 2, because 3 goes into 5 once, with 2 left over.
 
<pre>
10 % 2
10 % 3
10 % 4
10 % 5
</pre>
 
==Types==
Line 221 ⟶ 210:
 
===String Concatenation===
 
You can smoosh strings together (called "concatenation") using the '+' sign:
 
<pre>
Line 239 ⟶ 230:
w = "World"
print h + w
print h, w
</pre>
 
Line 250 ⟶ 240:
 
<pre>
print "Hello", 1
print "Hello" + 1
</pre>
Line 284 ⟶ 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 309 ⟶ 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 377 ⟶ 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 420 ⟶ 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 478 ⟶ 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 656 ⟶ 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 763 ⟶ 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 781 ⟶ 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