Boston Python workshop 2/Friday tutorial: Difference between revisions

No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 303:
print (h + b) * 10
</pre>
 
==Python scripts==
 
Until now we've been executing commands at the Python prompt. This is great for math, short bits of code, and testing. For longer ideas, it's easier to store the code in a file.
 
<ol>
<li>Download the file http://mit.edu/jesstess/www/BostonPythonWorkshop2/nobel.py. 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_2/Friday_setup#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
 
<pre>
python nobel.py
</pre>
 
at a command prompt.
 
<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_2/Friday_setup#Goal_.232:_Prepare_a_text_editor|preparing your text editor]] for a refresher on starting the editor).</li>
<li>Read through the file in your text editor carefully and check your understanding of both the comments and the code.</li>
</ol>
 
==Part 1 Practice==
Line 369 ⟶ 347:
</pre>
 
==End of Part 1===
 
Congratulations! You've learned about and practiced math, strings, variables, data types, exceptions, tracebacks, and executing Python from the Python prompt and from a file.
 
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
 
==Python scripts==
 
Until now we've been executing commands at the Python prompt. This is great for math, short bits of code, and testing. For longer ideas, it's easier to store the code in a file.
 
<ol>
<li>Download the file http://mit.edu/jesstess/www/BostonPythonWorkshop2/nobel.py. 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_2/Friday_setup#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
 
<pre>
python nobel.py
</pre>
 
at a command prompt.
 
<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_2/Friday_setup#Goal_.232:_Prepare_a_text_editor|preparing your text editor]] for a refresher on starting the editor).</li>
<li>Read through the file in your text editor carefully and check your understanding of both the comments and the code.</li>
</ol>
 
==Booleans==
Line 517:
</pre>
 
Like with <code>if</code>, the code block under the <code>else</code> statement must be indented.
You can check multiple expressions together using the <code>and</code> and <code>or</code> keywords. If two expressions are joined by an <code>and</code>, they <b>both</b> have to be True for the overall expression to be True. If two expressions are joined by an <code>or</code>, as long as <b>one</b> is True, the overall expression to be True.
 
You can check multiple expressions together using the <code>and</code> and <code>or</code> keywords. If two expressions are joined by an <code>and</code>, they <b>both</b> have to be True for the overall expression to be True. If two expressions are joined by an <code>or</code>, as long as <b>one</b> is True, the overall expression to beis True.
 
<pre>
Line 533 ⟶ 535:
<pre>
1 <= 0 or "a" not in "abc"
</pre>
 
<pre>
temperature = 32
if temperature > 60 and temperature < 75:
print "It's nice and cozy in here!"
else:
print "Too extreme for me."
</pre>
 
Anonymous user