From c9725ec58927763dd1392f05b318ecb3a636cec1 Mon Sep 17 00:00:00 2001 From: Kamilla Khabibrakhmanova Date: Sun, 11 Nov 2018 17:48:55 -0300 Subject: [PATCH 1/9] add parentheses for print --- class1.html | 30 ++++++++--------- class2.html | 92 ++++++++++++++++++++++++++--------------------------- class3.html | 62 ++++++++++++++++++------------------ class4.html | 14 ++++---- 4 files changed, 99 insertions(+), 99 deletions(-) diff --git a/class1.html b/class1.html index d95ce3b..c5304f2 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".
  • @@ -479,7 +479,7 @@

    User Input

    name = raw_input("What is your name?") age = raw_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) diff --git a/class2.html b/class2.html index 9ae052c..5a2d066 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!"
    +print ("It's your birthday!")
     answer = raw_input("How old are you? ")
     age = int(answer)
     if answer < 21:
    -    print "You may not have a beer, but here's some juice!"
    +    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!"
    +print ("It's your birthday!")
     answer = raw_input("How old are you? ")
     age = int(answer)
     if answer < 21:
    -    print "You may not have a beer, but here's some juice!"
    +    print ("You may not have a beer, but here's some juice!")
     else:
         beers = raw_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."
    +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:")
     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.')
                 
    @@ -307,7 +307,7 @@

    Iteration

    while user_input != 'y': user_input = raw_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.

    @@ -319,9 +319,9 @@

    While loops

    input_value = raw_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,7 +340,7 @@

    For loops

    for price in prices: costs.append(price + shipping_cost) -print costs +print (costs) @@ -366,8 +366,8 @@

    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: