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" |
||
"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 |
print(h + w) |
||
</pre> |
</pre> |
||
<pre> |
<pre> |
||
my_string = "Alpha " + "Beta " + "Gamma " + "Delta" |
my_string = "Alpha " + "Beta " + "Gamma " + "Delta" |
||
print |
print(my_string) |
||
</pre> |
</pre> |
||
Line 245: | Line 245: | ||
<pre> |
<pre> |
||
print |
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 |
<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 |
print("Hello" + "World") |
||
</pre> |
</pre> |
||
Line 274: | Line 274: | ||
<pre> |
<pre> |
||
print |
print("Hello" + 1) |
||
</pre> |
</pre> |
||
Line 282: | Line 282: | ||
<pre> |
<pre> |
||
print |
print("Hello" + str(1)) |
||
</pre> |
</pre> |
||
Line 292: | Line 292: | ||
<pre> |
<pre> |
||
print |
print(len("Hello")) |
||
print |
print(len("")) |
||
fish = "humuhumunukunukuapuaʻa" |
fish = "humuhumunukunukuapuaʻa" |
||
length = str(len(fish)) |
length = str(len(fish)) |
||
print |
print(fish + " is a Hawaiian fish whose name is " + length + " characters long.") |
||
</pre> |
</pre> |
||
Line 304: | Line 304: | ||
<pre> |
<pre> |
||
print |
print('Hello') |
||
print |
print("Hello") |
||
</pre> |
</pre> |
||
Line 313: | Line 313: | ||
<pre> |
<pre> |
||
print |
print('I'm a happy camper') |
||
</pre> |
</pre> |
||
Line 325: | Line 325: | ||
<pre> |
<pre> |
||
print |
print("I'm a happy camper") |
||
</pre> |
</pre> |
||
Line 331: | Line 331: | ||
<pre> |
<pre> |
||
print |
print("A" * 40) |
||
print |
print("ABC" * 12) |
||
h = "Happy" |
h = "Happy" |
||
b = "Birthday" |
b = "Birthday" |
||
print |
print((h + b) * 10) |
||
</pre> |
</pre> |
||
Line 347: | Line 347: | ||
<pre> |
<pre> |
||
total = 1.5 - 1/2 |
total = 1.5 - 1/2 |
||
print |
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 |
print("The " + a * 3 + " " + b * 3 + " " + c) |
||
</pre> |
</pre> |
||
Line 491: | Line 491: | ||
<pre> |
<pre> |
||
if 6 > 5: |
if 6 > 5: |
||
print |
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 |
<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 |
... 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 |
print("Zero is greater than two!") |
||
</pre> |
</pre> |
||
<pre> |
<pre> |
||
if "banana" in "bananarama": |
if "banana" in "bananarama": |
||
print |
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 |
print("sister is older") |
||
else: |
else: |
||
print |
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 |
print("It's nice and cozy in here!") |
||
else: |
else: |
||
print |
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 |
print("Go away!") |
||
print |
print("I'm sleeping!") |
||
else: |
else: |
||
print |
print("Welcome to the cheese shop!") |
||
print |
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 |
print("sister is older") |
||
elif sister_age == brother_age: |
elif sister_age == brother_age: |
||
print |
print("sister and brother are the same age") |
||
else: |
else: |
||
print |
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 |
print("Christmas color!") |
||
elif color == "black" or color == "orange": |
elif color == "black" or color == "orange": |
||
print |
print("Halloween color!") |
||
elif color == "pink": |
elif color == "pink": |
||
print |
print("Valentine's Day color!") |
||
</pre> |
</pre> |
||