Boston Python Workshop 7/Friday/Tutorial: Difference between revisions

imported>Jesstess
 
(11 intermediate revisions by one other user not shown)
Line 321:
<pre>
print "I'm a happy camper"
</pre>
 
or we can <b>escape</b> the quote with a backslash:
 
<pre>
print 'I\'m a happy camper'
print 'Ada Lovelace is often called the world\'s first programmer.'
print "Computer scientist Grace Hopper popularized the term \"debugging\"."
</pre>
 
Line 360 ⟶ 352:
c = "fox jumps over the lazy dog"
print "The " + a * 3 + " " + b * 3 + " " + c
</pre>
 
3.
<pre>
print 2.0 * 123 + str(2.0) * 123
</pre>
 
Line 502 ⟶ 489:
</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 6 > 5:</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.
 
<code> if 6 > 5:</code>
Enter 4 spaces, and then type <code>print "Six is greater than five!"</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 like this:
 
part, and press 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.
 
Enter 4 spaces, and then type
 
<code> print "Six is greater than five!"</code>
 
Press Enter 4 spaces, and then type <code>print "Six is greater than five!"</code>. Hit enter to end the line, and hitpress enterEnter again to tell Python you are done with this code block. All together, it will look like this:
 
<pre>
Line 513 ⟶ 512:
</pre>
 
So whatWhat 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 6 really is greater than 5, Python executes the code block under the if statement, and we see "Six is greater than five!" printed to the screen. Guess what will happen with these other expressions, then type them out and see if your guess was correct:
Line 529 ⟶ 528:
====more choices: <code>if</code> and <code>else</code>====
 
You can use the <code>else</code> keyword to execute code only when the <code>if</code> expression isn't True. Try this:
 
<pre>
Line 616 ⟶ 615:
 
<b>Remember that '=' is for assignment and '==' is for comparison.</b>
 
====In summary: the structure of if/elif/else====
 
Here's a diagram of <code>if/elif/else</code>:
 
[[File:If-elif-else.png]]
 
Do you understand the difference between <code>elif</code> and <code>else</code>? When do you indent? When do you use a colon? If you're not sure, talk about it with a neighbor or staff member.
 
==Writing Functions==
Line 640 ⟶ 647:
<b>Step 1: write a function signature</b>
 
A <b>function signature</b> tells you how the function will be called. It starts with the keyword <code>def</code>, which tells Python that you are defining a function. Then comes a space, the name of your function, an open parenthesis, the comma-separated input <b>parametersarguments</b> for your function, a close parenthesis, and a colon. Here's what a function signature looks like for a function that takes no arguments:
 
<code>def myFunction():</code>
Line 652 ⟶ 659:
<code>def myFunction(myList, myInteger):</code>
 
ParametersArguments should have names that usefully describe what they are used for in the function.
 
We've used the words <b>parameters</b> and <b>arguments</b> seemingly interchangeably to reference the input to functions. The distinction isn't really important right now, but if you're curious: in function signatures the input is called parameters, and when you are calling the function the input is called arguments.
 
<b>Step 2: do useful work inside the function</b>
Line 660 ⟶ 665:
Underneath the function signature you do your useful work. Everything inside the function is indented, just like with if/else blocks, so Python knows that it is a part of the function.
 
You can use the variables passed into the function as parametersarguments, just like you can use variables once you define them outside of functions.
 
<pre>
Line 722 ⟶ 727:
 
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.
 
====What is the difference between <code>print</code> and <code>return</code>?====
 
Think for a moment about the differences between <code>print</code> and <code>return</code>:
 
* <code>print</code> prints output to the screen so your eyes can see it.
 
* <code>return</code> is used to hand off a value from inside a function to a variable outside the function.
 
For example:
 
<pre>
def add(x, y):
print x + y
</pre>
 
will print <code>x + y</code> to the screen so your eyes can see it.
 
<pre>
def add(x, y):
return x + y
</pre>
 
will hand off <code>x + y</code> from inside the function to outside the function. This allows you to do something like:
 
<pre>
result = add(5, 6)
print result
</pre>
 
Does that make sense? If not, talk about it with a neighbor or staff member.
 
==End of Part 2==
Line 733 ⟶ 769:
 
 
[[BostonMontreal Python Workshop 7/Friday|&laquo; Back to the Friday Workshop page]]
Anonymous user