Anonymous user
Boston Python workshop 2/Friday tutorial: Difference between revisions
Boston Python workshop 2/Friday tutorial (view source)
Revision as of 01:54, 27 May 2011
, 13 years ago→Flow Control
(→Output) |
|||
(14 intermediate revisions by 2 users not shown) | |||
Line 170:
==Strings==
So far we've seen two data types: <b>integers</b> and <b>floats</b>. Another useful data type is a <b>string</b>, which is just what Python calls a bunch of characters (like numbers, letters, whitespace, and punctuation) put together. Strings are indicated by being surrounded by quotes:
<pre>
Line 218:
<code>TypeError: cannot concatenate 'str' and 'int' objects</code>
Python is giving us a
There are many kinds of Python errors, with descriptive names to help us humans understand what went wrong. In this case we are getting a
Python gives us a helpful error message as part of the TypeError:
Line 240:
</pre>
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
Line 287:
</pre>
or we can
<pre>
Line 303:
print (h + b) * 10
</pre>
==Part 1 Practice==
Read the following expressions, but don't execute them. Guess what the output will be. After you've made a guess, copy and paste the expressions at a Python prompt and check your guess.
1.
<pre>
total = 1.5 - 1/2 + ((-2.0/2) - (1.0/2))
print total
type(total)
</pre>
2.
<pre>
a = "quick"
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>
4.
<pre>
a = "| (_| -()- -()- -()- -()- | -()- -()- -()- -()- ||\n"
b = "|_\_|_/___|__|__|__|___|__|___|__|___________________________||\n"
c = "|________________________________|__|__()_|__()_|__()__|_____||\n"
d = " ___|)_______________________________________________________\n"
e = "|_/(|,\____/_|___/_|____/_|______|___________________________||\n"
f = "|___/____________________________|___________________________||\n"
g = "| | | () | () | () | | ||\n"
h = "|__\___|.________________________|___\_|___\_|___\_|___|_____||\n"
i = "|__/|_______/|____/|_____/|______|___________________________||\n"
j = "|_____/__________________________|____\|____\|____\|_________||\n"
k = "|____/___________________________|___________________________||\n"
l = "|__/___\_._______________________|__|__|__|__|__|__|___|_____||\n"
print d + f + i + e + b + g + a + c + l + h + j + k
</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==
Line 358 ⟶ 407:
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: <
Use <code>!=</code> to test for inequality:
Line 399 ⟶ 448:
Or check for a lack of containment with <code>not in</code>:
<pre>
"a" not in "abcde"
</pre>
<pre>
Line 406 ⟶ 459:
==Flow Control==
We can use these expressions that <i>evaluate</i> to booleans to make decisions and conditionally execute code:
<pre>
if
print "
</pre>
That was our first multi-line piece of code, and the way to enter it at a Python prompt is a little different. First, type the <code>if
Type 4 spaces, and then type <code>print "
<pre>
>>> if
... print "
...
"I'm True!"
</pre>
So what is going on here? When Python encounters the <code>if</code> keyword, it <i>evaluates</i> the <i>expression</i> following the keyword and before the colon. If that expression is <b>True</b>, Python executes the code in the indented code block under the <code>if</code> line. If that expression is <b>False</b>, Python skips over the code block
In this case, because <code>1</code> is truthy, Python executes the code block under the if statement, and we see "I'm True!" printed to the screen. What do you think will happen with this one:
<pre>
if False:
print "I'm True!"
</pre>
<pre>
if 1:
print "1 is truthy"
</pre>
<pre>
Line 441 ⟶ 506:
</pre>
You can use the <code>else</code> keyword to conditionally execute code when the expression for the <code>if</code> block isn't true:
<pre>
sister_age = 15
brother_age = 12
if sister_age > brother_age:
print "Sister is older"
else:
print "brother is older"
</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 is True.
<pre>
1 > 0 and 1 < 2
</pre>
<pre>
1 < 2 and "x" in "abc"
</pre>
<pre>
</pre>
<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>
<pre>
hour = 11
if hour < 7 or hour > 23:
print "Go away!"
print "I'm sleeping!"
else:
print "Welcome to the cheese shop!"
print "Can I interest you in some choice gouda?"
</pre>
[[Boston Python workshop 2/Friday setup|« Back to the Friday setup page]]
|