Boston Python workshop 2/Friday tutorial: Difference between revisions

 
(15 intermediate revisions by 2 users not shown)
Line 154:
</pre>
 
are both valid Python '<b>syntax'</b>.
 
<pre>
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 '<b>traceback'</b>. A traceback is details on what was happening when Python encountered an Exception or Error -- something it doesn't know how to handle.
 
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 '<code>TypeError'</code>: we tried to do some operation on a data type that isn't supported for that data type.
 
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 '<b>escape'</b> the quote with a backslash:
 
<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: <codeb>=</code> is assignment, <code>==</code> is comparison</b>.
 
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 1True:
print "1I'm is truthyTrue!"
</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 1True:</code> part, and hit enter. The next line will have <code>...</code> as a prompt, instead of the usual <code>&gt;&gt;&gt;</code>. This is Python telling us that we are in the middle of a <b>code block</b>, and so long as we indent our code it should be a part of this code block.
 
Type 4 spaces, and then type <code>print "1I'm is truthyTrue!"</code>. Hit enter to end the line, and hit enter again to tell Python you are done with this code block. All together, it will look something like this:
 
<pre>
>>> if 1True:
... print "1I'm is truthyTrue!"
...
"I'm True!"
1 is truthy
</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:
==Practice==
 
<pre>
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.
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.
 
1.
<pre>
1 > 0 and 1 < 2
total = 1.5 - 1/2 + ((-2.0/2) - (1.0/2))
print total
type(total)
</pre>
 
2.
<pre>
1 < 2 and "x" in "abc"
a = "quick"
b = "brown"
c = "fox jumps over the lazy dog"
print "The", a * 3, b * 3, c
</pre>
 
3.
<pre>
print"a" 2.0in *"hello" 123,or "2.0e" *in 123hello"
print 2.0 * 123 + str(2.0) * 123
</pre>
 
4.
<pre>
1 <= 0 or "a" not in "abc"
a = "| (_| -()- -()- -()- -()- | -()- -()- -()- -()- ||\n"
</pre>
b = "|_\_|_/___|__|__|__|___|__|___|__|___________________________||\n"
c = "|________________________________|__|__()_|__()_|__()__|_____||\n"
d = " ___|)_______________________________________________________\n"
e = "|_/(|,\____/_|___/_|____/_|______|___________________________||\n"
f = "|___/____________________________|___________________________||\n"
g = "| | | () | () | () | | ||\n"
h = "|__\___|.________________________|___\_|___\_|___\_|___|_____||\n"
i = "|__/|_______/|____/|_____/|______|___________________________||\n"
j = "|_____/__________________________|____\|____\|____\|_________||\n"
k = "|____/___________________________|___________________________||\n"
l = "|__/___\_._______________________|__|__|__|__|__|__|___|_____||\n"
 
<pre>
print d + f + i + e + b + g + a + c + l + h + j + k
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|&laquo; Back to the Friday setup page]]
Anonymous user