Skip to content

Update Workshop slides to be Python3-compatible #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions class1.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,16 @@ <h3>Variables and Arithmetic</h3>
<div class='box--small'><pre><code contenteditable class="python" style="min-height: 50px;">
>>> a = 2
>>> b = 3
>>> print a + b
>>> print (a + b)
5
>>> c = a + b
>>> print c * 2
>>> print (c * 2)
10
</code></pre></div>
<div class='box--small'><pre><code contenteditable class="python" style="min-height: 50px;">
>>> a = 0
>>> a = a + .5
>>> print a
>>> print (a)
0.5
>>> a
0.5
Expand Down Expand Up @@ -301,18 +301,18 @@ <h3>Operators for strings</h3>
a = 'Hello '
b = 'World'
c = a + b
print c
print (c)
</code></pre></div>
<div class="box--small"><pre><code contenteditable class="python" style="min-height: 50px;">
a = "Spam "
b = a * 4
print b
print (b)
</code></pre></div>
<div class="box--small"><pre><code contenteditable class="python" style="min-height: 50px;">
a = 'spam '
b = 'eggs'
c = a * 4 + b
print c
print (c)
</code></pre></div>
</section>

Expand All @@ -326,14 +326,14 @@ <h3>Data Types</h3>
<li class="fragment"><code>type()</code> is a <strong>function</strong>. We <em>call</em> it by using parenthesis and pass it an object by placing the object inside the parenthesis
<pre><code contenteditable class="python" style="min-height: 50px;">
a = 4
print type(a)
print type(4)
print (type(a))
print (type(4))

print type(3.14)
print (type(3.14))

b = 'spam, again'
print type(b)
print type("But I don't like spam")
print (type(b))
print (type("But I don't like spam"))
</code></pre>

</li>
Expand All @@ -349,7 +349,7 @@ <h3>Data types - continued ...</h3>
<li>What happens if we try to use division or subtraction with a string?</li>
</ul>
<pre><code contenteditable class="python" style="min-height: 50px;">
>>> print "Spam" / "eggs"
>>> print ("Spam" / "eggs")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'str'
Expand Down Expand Up @@ -377,7 +377,7 @@ <h3>Errors - continued ...</h3>

# NameError - Using a name that hasn't been defined yet
a = 5
print b
print (b)
b = 10

# TypeError - Using an object in a way that its type does not support
Expand Down Expand Up @@ -461,7 +461,7 @@ <h3>The Text Editor</h3>
<li class="fragment">Click "File", then "Open". Navigate to the <code>gdi-intro-python</code> <strong>folder</strong> we just created and click "Open"</li>
<li class="fragment">In the text editor, enter the following:
<pre><code contenteditable class="python" style="min-height: 50px;">
print 'Hello World!'
print ('Hello World!')
</code></pre>
</li>
<li class="fragment">Click "File", then "Save As...". Type "class1.py" and click "Save".</li>
Expand All @@ -473,13 +473,13 @@ <h3>The Text Editor</h3>

<section>
<h3>User Input</h3>
<p class="copy--small box--small">To obtain user input, use <code>raw_input()</code></p>
<p class="copy--small box--small">To obtain user input, use <code>input()</code></p>
<p class="copy--small box--small">Change the class1.py text to the following and run it again</p>
<pre><code contenteditable class="python" style="min-height: 50px;">
name = raw_input("What is your name?")
age = raw_input("How old are you?")
name = input("What is your name?")
age = input("How old are you?")
age_int = int(age)
print "Your name is" + name + " and you are " age + " years old."
print ("Your name is " + name + " and you are " + age + " years old.")

type(age)
type(age_int)
Expand All @@ -490,7 +490,7 @@ <h3>User Input</h3>
<!-- Let's develop it: 15 minutes -->
<section>
<h3>Let's Develop It!</h3>
<p class="copy--small box">Write your own program that uses raw_input<br/>and does something else with the value</p>
<p class="copy--small box">Write your own program that uses input<br/>and does something else with the value</p>
<p class="copy--xsmall box">You can use float() to treat the input as a number if you need a number,<br/>or use the input directly as a string.</p>
</section>

Expand Down
127 changes: 55 additions & 72 deletions class2.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ <h3>Boolean Expressions</h3>
<pre class="fragment"><code contenteditable class="python" style="min-height: 50px;">
a = 5
b = 5
print a == b
print (a == b)
c = 3
print a == c
print c &lt; a
print (a == c)
print (c &lt; a)
</code></pre>
</section>

Expand Down Expand Up @@ -105,9 +105,9 @@ <h3>Boolean Expressions continued</h3>
<pre class=""><code contenteditable class="python" style="min-height: 50px;">
a = 3
b = 4
print a != b
print a <= 3
print a >= 4
print (a != b)
print (a <= 3)
print (a >= 4)
</code></pre>
<p class="copy--xsmall">Remember: Equals does not equal "equals equals"</p>
</section>
Expand Down Expand Up @@ -138,7 +138,7 @@ <h3>Appending to list</h3>
to_do_list = []
to_do_list.append('buy soy milk')
to_do_list.append('learn python')
print to_do_list
print (to_do_list)
</code></pre></div>
<p class="copy--small box--small fragment">Therefore, lists are <strong>mutable</strong></p>
<p class="copy--small box--small fragment">This means that a list can change values<br/>during the duration of a program</p>
Expand All @@ -151,7 +151,7 @@ <h3>Indexing</h3>
<div class="fragment"><p class="copy--small box--small">To index on a list, follow it immediately with <code>[index]</code>.</p>
<pre><code contenteditable class="small python">
numbers = [10, 20, 30]
print numbers[0]
print (numbers[0])
</code></pre></div>
<p class="copy--small box--small fragment">Lists (and other data types) start at the number 0 and count up.</p>
</section>
Expand All @@ -166,13 +166,13 @@ <h3>Length and More Indexing</h3>
to_do_list = [
'learn python', 'read email', 'make lunch',
]
print len(to_do_list)
print (len(to_do_list))

print to_do_list[0]
print to_do_list[2]
print (to_do_list[0])
print (to_do_list[2])

print to_do_list[len(to_do_list)]
print to_do_list[len(to_do_list) - 1]
print (to_do_list[len(to_do_list)])
print (to_do_list[len(to_do_list) - 1])
</code></pre>
<p class="box--small copy--xsmall fragment">An IndexError results if an index exceeds the length of the list minus 1</p>
</section>
Expand All @@ -196,7 +196,7 @@ <h3>Conditionals</h3>
<p class="copy--xsmall">We achieve this using <strong>if</strong> statements</p>
<pre><code contenteditable class="small python">
if x == 5:
print 'x is equal to 5'
print ('x is equal to 5')
</code></pre></div>

</section>
Expand All @@ -206,9 +206,9 @@ <h3>Conditionals</h3>
<p class="copy--xsmall">We often want a different block to execute if the statement is false.<br/>This can be accomplished using <strong>else</strong></p>
<pre><code contenteditable class="small python">
if x == 5:
print 'x is equal to 5'
print ('x is equal to 5')
else:
print 'x is not equal to 5'
print ('x is not equal to 5')
</code></pre>
</div>
</section>
Expand All @@ -220,14 +220,14 @@ <h3>Indentation</h3>
<div class="fragment">
<p class="copy--small box--small">Write this into your text editor and save the file as class2.py in your gdi-intro-python folder.
<pre><code contenteditable class=" python">
print "It's your birthday!"
answer = raw_input("How old are you? ")
print ("It's your birthday!")
answer = input("How old are you? ")
age = int(answer)
if answer &lt; 21:
print "You may not have a beer, but here's some juice!"
if age &lt; 21:
print ("You may not have a beer, but here's some juice!")
else:
print "Here's some beer!"
print "Happy birthday!"
print ("Here's some beer!")
print ("Happy birthday!")
</code></pre></div>
</section>

Expand All @@ -237,11 +237,11 @@ <h3>Chained conditionals</h3>
<div class="fragment"><p class="box--small copy--small ">Chained conditionals use <code>elif</code> as an additonal check after the preceeding <code>if</code> predicate was False. For example:</p>
<pre><code contenteditable class=" python small">
if x &lt; 0:
print "x is negative"
print ("x is negative")
elif x &gt; 0:
print "x is positive"
print ("x is positive")
else:
print "x is 0"
print ("x is 0")
</code></pre></div>
</section>

Expand All @@ -251,21 +251,21 @@ <h3>Nested conditionals</h3>
<p class="box--small copy--small fragment">Nested conditionals occur inside of other conditionals, and are indented over once more. When the code block is complete you move back.</p>
<div class="fragment"><p class="box--small copy--small">Edit your python file to contain the following:</p>
<pre><code contenteditable class="small python">
print "It's your birthday!"
answer = raw_input("How old are you? ")
print ("It's your birthday!")
answer = input("How old are you? ")
age = int(answer)
if answer &lt; 21:
print "You may not have a beer, but here's some juice!"
if age &lt; 21:
print ("You may not have a beer, but here's some juice!")
else:
beers = raw_input("How many beers do you want?")
beers = input("How many beers do you want?")
beers = int(beers)
if beers > 3:
print "Oops, you're drunk!"
print ("Oops, you're drunk!")
elif beers > 1:
print "You got a little tipsy"
print ("You got a little tipsy")
else:
print "Looks like you're the designated driver"
print "Happy birthday!"
print ("Looks like you're the designated driver")
print ("Happy birthday!")
</code></pre></div>
</section>

Expand All @@ -276,20 +276,20 @@ <h3>Let's Develop It</h3>
<p class="box--small copy--small">The code below is an example:</p>
<pre><code contenteditable class=" python">
health = 100
print "A vicious warg is chasing you."
print "Options:"
print "1 - Hide in the cave."
print "2 - Climb a tree."
input_value = raw_input("Enter choice:")
print ("A vicious warg is chasing you.")
print ("Options:")
print ("1 - Hide in the cave.")
print ("2 - Climb a tree.")
input_value = input("Enter choice:")
if input_value == '1':
print 'You hide in a cave.'
print 'The warg finds you and injures your leg with its claws'
print ('You hide in a cave.')
print ('The warg finds you and injures your leg with its claws')
health = health - 10
elif input_value == '2':
print 'You climb a tree.'
print 'The warg eventually looses interest and wanders off'
print ('You climb a tree.')
print ('The warg eventually looses interest and wanders off')
else:
print 'Invalid option.'
print ('Invalid option.')
</code></pre>
</section>

Expand All @@ -302,12 +302,12 @@ <h3>Iteration</h3>
<p class="box--small copy--small fragment">The repeated execution of a set of statements is called <strong>iteration</strong></p>
<div class="fragment"><p class="box--small copy--small">One way to acheive this, is with the <strong>while</strong> loop.</p>
<pre><code contenteditable class=" small python">
user_input = raw_input('would you like to quit? (y/n) ')
user_input = input('would you like to quit? (y/n) ')

while user_input != 'y':
user_input = raw_input('would you like to quit? (y/n) ')
user_input = input('would you like to quit? (y/n) ')

print 'quitters never win!'
print ('quitters never win!')
</code></pre></div>
<p class="box--small copy--small fragment">As long as the while statement evaluates to True, the code block beneath it is repeated.</p>
</section>
Expand All @@ -316,12 +316,12 @@ <h3>Iteration</h3>
<h3>While loops</h3>
<p class="box--small copy--small">Consider the following example that uses iteration with a while loop</p>
<div class="fragment"><pre><code contenteditable class="small python">
input_value = raw_input('Enter a positive integer:')
input_value = input('Enter a positive integer:')
user_number = int(input_value)
while user_number > 0:
print "The user's number is still positive, let's subtract 1"
print ("The user's number is still positive, let's subtract 1")
user_number = user_number - 1
print "User's number is no longer positive."
print ("User's number is no longer positive.")
</code></pre></div>
<p class="copy--small box--small fragment">This implementation does not work for negative numbers. Why?</p>
</section>
Expand All @@ -340,34 +340,17 @@ <h3>For loops</h3>
for price in prices:
costs.append(price + shipping_cost)

print costs
print (costs)
</code></pre></div>
</section>

<section>
<h3>Range</h3>
<p class="fragment box--small copy--small">What do you think the following functions output?</p>
<pre><code contenteditable class="small fragment python">
range(5)
</code></pre>
<pre><code contenteditable class="small fragment python">
range(5, 10)
</code></pre>
<pre><code contenteditable class="small fragment python">
range(10, 20, 2)
</code></pre>
<pre><code contenteditable class="small fragment python">
range(20, 8, -2)
</code></pre>
</section>

<section>
<h3>For loops continued</h3>
<p class="copy--small box--small">Let's examine the example carefully</p>
<pre><code contenteditable class="small python">
for number in range(5):
print "The current number is:"
print number
print ("The current number is:")
print (number)
</code></pre>
<p class="fragment copy--small box--small">This for loop has three parts:</p>
<ul class="list--tall copy--xsmall">
Expand All @@ -385,8 +368,8 @@ <h3>Variable name in for loops</h3>
<em>processing,</em> not <em>understanding.</em></p>
<pre><code contenteditable class="small python">
for moose in range(5):
print "The current number is:"
print moose
print ("The current number is:")
print (moose)
</code></pre></div>
</section>

Expand Down
Loading