diff --git a/class1.html b/class1.html index d95ce3b..1f0f631 100644 --- a/class1.html +++ b/class1.html @@ -263,16 +263,16 @@

Variables and Arithmetic


 >>> a = 2
 >>> b = 3
->>> print a + b
+>>> print (a + b)
 5
 >>> c = a + b
->>> print c * 2
+>>> print (c * 2)
 10
             

 >>> a = 0
 >>> a = a + .5
->>> print a
+>>> print (a)
 0.5
 >>> a
 0.5
@@ -301,18 +301,18 @@ 

Operators for strings

a = 'Hello ' b = 'World' c = a + b -print c +print (c)

 a = "Spam "
 b = a * 4
-print b
+print (b)
             

 a = 'spam '
 b = 'eggs'
 c = a * 4 + b
-print c
+print (c)
             
@@ -326,14 +326,14 @@

Data Types

  • type() is a function. We call it by using parenthesis and pass it an object by placing the object inside the parenthesis
    
     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"))
                 
  • @@ -349,7 +349,7 @@

    Data types - continued ...

  • What happens if we try to use division or subtraction with a string?
  • 
    ->>> print "Spam" / "eggs"
    +>>> print ("Spam" / "eggs")
     Traceback (most recent call last):
     File "", line 1, in 
     TypeError: unsupported operand type(s) for /: 'str' and 'str'
    @@ -377,7 +377,7 @@ 

    Errors - continued ...

    # 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 @@ -461,7 +461,7 @@

    The Text Editor

  • Click "File", then "Open". Navigate to the gdi-intro-python folder we just created and click "Open"
  • In the text editor, enter the following:
    
    -print 'Hello World!'
    +print ('Hello World!')
                 
  • Click "File", then "Save As...". Type "class1.py" and click "Save".
  • @@ -473,13 +473,13 @@

    The Text Editor

    User Input

    -

    To obtain user input, use raw_input()

    +

    To obtain user input, use input()

    Change the class1.py text to the following and run it again

    
    -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)
    @@ -490,7 +490,7 @@ 

    User Input

    Let's Develop It!

    -

    Write your own program that uses raw_input
    and does something else with the value

    +

    Write your own program that uses input
    and does something else with the value

    You can use float() to treat the input as a number if you need a number,
    or use the input directly as a string.

    diff --git a/class2.html b/class2.html index 9ae052c..f3520bf 100644 --- a/class2.html +++ b/class2.html @@ -66,10 +66,10 @@

    Boolean Expressions

    
     a = 5
     b = 5
    -print a == b
    +print (a == b)
     c = 3
    -print a == c
    -print c < a
    +print (a == c)
    +print (c < a)
                 
    @@ -105,9 +105,9 @@

    Boolean Expressions continued

    
     a = 3
     b = 4
    -print a != b
    -print a <= 3
    -print a >= 4
    +print (a != b)
    +print (a <= 3)
    +print (a >= 4)
                 

    Remember: Equals does not equal "equals equals"

    @@ -138,7 +138,7 @@

    Appending to list

    to_do_list = [] to_do_list.append('buy soy milk') to_do_list.append('learn python') -print to_do_list +print (to_do_list)

    Therefore, lists are mutable

    This means that a list can change values
    during the duration of a program

    @@ -151,7 +151,7 @@

    Indexing

    To index on a list, follow it immediately with [index].

    
     numbers = [10, 20, 30]
    -print numbers[0]
    +print (numbers[0])
                 

    Lists (and other data types) start at the number 0 and count up.

    @@ -166,13 +166,13 @@

    Length and More Indexing

    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])

    An IndexError results if an index exceeds the length of the list minus 1

    @@ -196,7 +196,7 @@

    Conditionals

    We achieve this using if statements

    
         if x == 5:
    -        print 'x is equal to 5'
    +        print ('x is equal to 5')
                 
    @@ -206,9 +206,9 @@

    Conditionals

    We often want a different block to execute if the statement is false.
    This can be accomplished using else

    
     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')
                 
    @@ -220,14 +220,14 @@

    Indentation

    Write this into your text editor and save the file as class2.py in your gdi-intro-python folder.

    
    -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 < 21:
    -    print "You may not have a beer, but here's some juice!"
    +if age < 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!")
                 
    @@ -237,11 +237,11 @@

    Chained conditionals

    Chained conditionals use elif as an additonal check after the preceeding if predicate was False. For example:

    
     if x < 0:
    -    print "x is negative"
    +    print ("x is negative")
     elif x > 0:
    -    print "x is positive"
    +    print ("x is positive")
     else:
    -    print "x is 0"
    +    print ("x is 0")
                 
    @@ -251,21 +251,21 @@

    Nested conditionals

    Nested conditionals occur inside of other conditionals, and are indented over once more. When the code block is complete you move back.

    Edit your python file to contain the following:

    
    -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 < 21:
    -    print "You may not have a beer, but here's some juice!"
    +if age < 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!")
                 
    @@ -276,20 +276,20 @@

    Let's Develop It

    The code below is an example:

    
     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.')
                 
    @@ -302,12 +302,12 @@

    Iteration

    The repeated execution of a set of statements is called iteration

    One way to acheive this, is with the while loop.

    
    -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!')
               

    As long as the while statement evaluates to True, the code block beneath it is repeated.

    @@ -316,12 +316,12 @@

    Iteration

    While loops

    Consider the following example that uses iteration with a while loop

    
    -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.")
                                     

    This implementation does not work for negative numbers. Why?

    @@ -340,34 +340,17 @@

    For loops

    for price in prices: costs.append(price + shipping_cost) -print costs +print (costs) -
    -

    Range

    -

    What do you think the following functions output?

    -
    
    -  range(5)
    -          
    -
    
    -  range(5, 10)
    -          
    -
    
    -  range(10, 20, 2)
    -          
    -
    
    -  range(20, 8, -2)
    -          
    -
    -

    For loops continued

    Let's examine the example carefully

    
     for number in range(5):
    -    print "The current number is:"
    -    print number
    +    print ("The current number is:")
    +    print (number)
                 

    This for loop has three parts:

    diff --git a/class3.html b/class3.html index 1c6210e..1000809 100644 --- a/class3.html +++ b/class3.html @@ -64,7 +64,7 @@

    Functions

    We have already made a function call
    when using the type, int, or float functions

    
     a = '3'
    -print type(a)
    +print (type(a))
     a = float(a)
                 
    @@ -73,7 +73,7 @@

    Functions

    Function calls

    
     a = 3
    -print type(a)
    +print (type(a))
                 

    A function can take arguments

    In the example above, the variable a is passed
    as an argument to the function type

    @@ -91,7 +91,7 @@

    Creating a Function

    The following example is a function definition.
    This allows us to create our own functions

    
     def print_plus_5(x):
    -    print x + 5
    +    print (x + 5)
                 

    The function definition has the following parts