Boston Python workshop 2/Friday tutorial: Difference between revisions

 
(7 intermediate revisions by the same user not shown)
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 410 ⟶ 459:
==Flow Control==
 
We can use these expressions that <i>evaluate</i> to booleans to make decisions and conditionally execute code:
 
<pre>
Line 430 ⟶ 479:
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 here?with this one:
 
<pre>
Line 457 ⟶ 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