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

Content added Content deleted
imported>Jesstess
No edit summary
imported>Jesstess
No edit summary
Line 227: Line 227:
</pre>
</pre>


How about concatenating different data types?
===Printing===

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


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

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

How about printing different data types together?

<pre>
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>>>> "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 251:


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


Line 274: Line 259:


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


Line 282: Line 267:


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


Line 292: Line 277:


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


Line 304: Line 289:


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


Line 313: Line 298:


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


Line 325: Line 310:


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


Line 331: Line 316:


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


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


Line 356: Line 341:
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)
"The " + a * 3 + " " + b * 3 + " " + c
</pre>
</pre>


Line 364: Line 349:


Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.

== Part 2: Printing==

So far we've been learning at the interactive <b>Python interpreter</b>. When you are working at the interpreter, any work that you do gets printed to the screen. For example:

<pre>
h = "Hello"
w = "World"
h + w
</pre>

will display "HelloWorld".

Another place that we will be writing Python code is in a file. When we run Python code from a file instead of interactively, we don't get work printed to the screen for free. We have to tell Python to print the information to the screen. The way we do this is with the <b>print</b> function. Here's how it works:

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

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

The string manipulate is exactly the same as before. The only difference is that you need to use <b>print</b> to print results to the screen:

<code> h + w</code>

becomes

<code> print(h + w)</code>

We'll see more examples of the print function in the next section.


==Python scripts==
==Python scripts==