Anonymous user
Boston Python Workshop 6/Friday/Tutorial: Difference between revisions
Boston Python Workshop 6/Friday/Tutorial (view source)
Revision as of 14:04, 31 March 2012
, 12 years ago→End of Part 2
imported>Jesstess (Created page with "Welcome to the Friday tutorial! This tutorial covers several core programming concepts that we'll build upon during an interactive lecture tomorrow morning. It will take 1-2 ...") |
imported>Adamf |
||
(17 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".
==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
</pre>
Line 250 ⟶ 240:
<pre>
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.
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
</pre>
Line 377 ⟶ 359:
b = "brown"
c = "fox jumps over the lazy dog"
print "The "
</pre>
3.
<pre>
print 2.0 * 123 + str(2.0) * 123
</pre>
Line 420 ⟶ 401:
<ol>
<li>Download the file http://mit.edu/jesstess/www/
<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
▲<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 5/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 433 ⟶ 413:
<code>nobel.py</code> introduces two new concepts: comments and multiline strings.</li>
<li>Open <code>nobel.py</code> in your text editor (see [[Boston Python Workshop
<li>Read through the file in your text editor carefully and check your understanding of both the comments and the code.</li>
</ol>
Line 478 ⟶ 458:
</pre>
Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i>
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
==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.
==End of Part 2==
Line 779 ⟶ 751:
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
|