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

imported>Jesstess
 
(3 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 740 ⟶ 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 751 ⟶ 769:
 
 
[[BostonMontreal Python Workshop 7/Friday|&laquo; Back to the Friday Workshop page]]
Anonymous user