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

Content added Content deleted
imported>Jesstess
No edit summary
imported>Jesstess
No edit summary
Line 224: Line 224:
<pre>
<pre>
name = "Jessica"
name = "Jessica"
print "Hello " + name
"Hello " + name
</pre>
</pre>


===Printing===
===Printing===


You can print strings using <code>print</code>:
You can print strings using the <code>print</code> function:


<pre>
<pre>
h = "Hello"
h = "Hello"
w = "World"
w = "World"
print h + w
print(h + w)
</pre>
</pre>


<pre>
<pre>
my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
my_string = "Alpha " + "Beta " + "Gamma " + "Delta"
print my_string
print(my_string)
</pre>
</pre>


Line 245: Line 245:


<pre>
<pre>
print "Hello" + 1
print("Hello" + 1)
</pre>
</pre>


Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened:
Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened:


<code>>>> print "Hello" + 1</code><br />
<code>>>> print("Hello" + 1)</code><br />
<code>Traceback (most recent call last):</code><br />
<code>Traceback (most recent call last):</code><br />
<code> File "<stdin>", line 1, in <module></code><br />
<code> File "<stdin>", line 1, in <module></code><br />
Line 266: Line 266:


<pre>
<pre>
print "Hello" + "World"
print("Hello" + "World")
</pre>
</pre>


Line 274: Line 274:


<pre>
<pre>
print "Hello" + 1
print("Hello" + 1)
</pre>
</pre>


Line 282: Line 282:


<pre>
<pre>
print "Hello" + str(1)
print("Hello" + str(1))
</pre>
</pre>


Line 292: Line 292:


<pre>
<pre>
print len("Hello")
print(len("Hello"))
print len("")
print(len(""))
fish = "humuhumunukunukuapuaʻa"
fish = "humuhumunukunukuapuaʻa"
length = str(len(fish))
length = str(len(fish))
print fish + " is a Hawaiian fish whose name is " + length + " characters long."
print(fish + " is a Hawaiian fish whose name is " + length + " characters long.")
</pre>
</pre>


Line 304: Line 304:


<pre>
<pre>
print 'Hello'
print('Hello')
print "Hello"
print("Hello")
</pre>
</pre>


Line 313: Line 313:


<pre>
<pre>
print 'I'm a happy camper'
print('I'm a happy camper')
</pre>
</pre>


Line 325: Line 325:


<pre>
<pre>
print "I'm a happy camper"
print("I'm a happy camper")
</pre>
</pre>


Line 331: Line 331:


<pre>
<pre>
print "A" * 40
print("A" * 40)
print "ABC" * 12
print("ABC" * 12)
h = "Happy"
h = "Happy"
b = "Birthday"
b = "Birthday"
print (h + b) * 10
print((h + b) * 10)
</pre>
</pre>


Line 347: Line 347:
<pre>
<pre>
total = 1.5 - 1/2
total = 1.5 - 1/2
print total
print(total)
type(total)
print(type(total))
</pre>
</pre>


Line 356: Line 356:
b = "brown"
b = "brown"
c = "fox jumps over the lazy dog"
c = "fox jumps over the lazy dog"
print "The " + a * 3 + " " + b * 3 + " " + c
print("The " + a * 3 + " " + b * 3 + " " + c)
</pre>
</pre>


Line 491: Line 491:
<pre>
<pre>
if 6 > 5:
if 6 > 5:
print "Six is greater than five!"
print("Six is greater than five!")
</pre>
</pre>


Line 506: Line 506:
Enter 4 spaces, and then type
Enter 4 spaces, and then type


<code> print "Six is greater than five!"</code>
<code> print("Six is greater than five!")</code>


Press Enter to end the line, and press Enter again to tell Python you are done with this code block. All together, it will look like this:
Press Enter to end the line, and press Enter again to tell Python you are done with this code block. All together, it will look like this:
Line 512: Line 512:
<pre>
<pre>
>>> if 6 > 5:
>>> if 6 > 5:
... print "Six is greater than five!"
... print("Six is greater than five!")
...
...
Six is greater than five!
Six is greater than five!
Line 523: Line 523:
<pre>
<pre>
if 0 > 2:
if 0 > 2:
print "Zero is greater than two!"
print("Zero is greater than two!")
</pre>
</pre>


<pre>
<pre>
if "banana" in "bananarama":
if "banana" in "bananarama":
print "I miss the 80s."
print("I miss the 80s.")
</pre>
</pre>


Line 539: Line 539:
brother_age = 12
brother_age = 12
if sister_age > brother_age:
if sister_age > brother_age:
print "sister is older"
print("sister is older")
else:
else:
print "brother is older"
print("brother is older")
</pre>
</pre>


Line 573: Line 573:
temperature = 32
temperature = 32
if temperature > 60 and temperature < 75:
if temperature > 60 and temperature < 75:
print "It's nice and cozy in here!"
print("It's nice and cozy in here!")
else:
else:
print "Too extreme for me."
print("Too extreme for me.")
</pre>
</pre>


Line 581: Line 581:
hour = 11
hour = 11
if hour < 7 or hour > 23:
if hour < 7 or hour > 23:
print "Go away!"
print("Go away!")
print "I'm sleeping!"
print("I'm sleeping!")
else:
else:
print "Welcome to the cheese shop!"
print("Welcome to the cheese shop!")
print "Can I interest you in some choice gouda?"
print("Can I interest you in some choice gouda?")
</pre>
</pre>


Line 598: Line 598:
brother_age = 12
brother_age = 12
if sister_age > brother_age:
if sister_age > brother_age:
print "sister is older"
print("sister is older")
elif sister_age == brother_age:
elif sister_age == brother_age:
print "sister and brother are the same age"
print("sister and brother are the same age")
else:
else:
print "brother is older"
print("brother is older")
</pre>
</pre>


Line 610: Line 610:
color = "orange"
color = "orange"
if color == "green" or color == "red":
if color == "green" or color == "red":
print "Christmas color!"
print("Christmas color!")
elif color == "black" or color == "orange":
elif color == "black" or color == "orange":
print "Halloween color!"
print("Halloween color!")
elif color == "pink":
elif color == "pink":
print "Valentine's Day color!"
print("Valentine's Day color!")
</pre>
</pre>