2014-02-26 + HW

posted Feb 25, 2014, 7:54 AM by Samuel Konstantinovich   [ updated Feb 26, 2014, 5:54 AM ]
Goal: Strings and If statements in python.

Homework: ON BOTTOM OF PAGE

Do now: (ON PAPER! NOT ON THE COMPUTER)
Evaluate on paper: Then compare with your neigbors.
1. 5 + 2 ** 3
2. 8 - 2 * 2
3. 4.0 / 10.0
3.5. 3 / 2
4. 2 / 3
5. 8 % 3 ( % is the remainder operator)
6. 3 % 8
7.  6 < 13 / 2
8.  9 < 2 or 2 < 9
9.  2 != 1.0*(5/2) and 2 == 4/2

save/load files
DO NOT save the python shell!!
To make a new file: Open NEW Window -> save as
To open your old files: Open File

Beware of strings
strings are a type of information that is text, 123 is an integer "123" is a string.
123 + 123  → 246   (the + operator adds numbers)
"123" 
+ "123" "123123"   (the + operator joins strings)

strings start with single or double quotes, and they end with a matching character.
"this is a string"
'this is a string'
"this is bob's string" (note the single quote does not end the string)

new lines break strings:
"this is a string
but this is broken!!!" (notice the 2nd line is not green string text)

We have a way around that!
"""This is a string
that is allowed on multiple lines
normally a string cannot have the new line
but the triple quote allows it"""

Strings can be multiplied by integers
"a" * 5 evaluates to: "aaaaa"
"abc" * 3 evaluates to: "abcabcabc"


Comments:
#this is a comment
"""we can make a multi-line comment 
you just have to use 
multi-line string notation(triple quotes)"""

if statements

Preface: There are no brackets and parenthesis around logical blocks of code, instead python uses indentation!!!

if boolean:
  code inside of the if
  code inside of the if
code outside of if


Real code example: (change the value of x to get different results)

x=3
if x < 10:
  print "inside
"
  print "the if
"
print "outside the if"


if can have else:
x=3
if x < 10:
  print "inside"
  print "the true part of the if
"
else:
  print "alternate"
  print "text!
"
print "outside the if"

Now we can also try to have multiple if/else:  (but it gets ugly)
if x < 10:
  print "inside"
  print "the true part of the if 1
"
else:
  if x < 20:
     print "inside the 2nd if true part"
  else:
     if x < 30:
        print "inside the 3rd if true part"
else:
        print "whoa... false!"

Now we can clean up that code using the elif command (else + if at the same time)
if x < 10:
  print "inside"
  print "the true part of the if 1
"
elif x < 20:
  print "inside the 2nd if true part"
elif x < 30:
  print "inside the 3rd if true part"
else:
  print "whoa... false!"

Note: if, else, and elif require colons at the end of the line.


Homework:

READ THIS FIRST: (there is more sample code on codingbat)
You will be writing functions like this:
def a_bigger(a, b):
  if a > b and (a - b) >= 2:
    return True
  else:
    return False
def is like to in netlogo, it tells python you are defining a function
a_bigger is the function name 
Both a and b are parameters which are used later in the function.
return sends the result back. This is like a to-report function in netlogo.
WHAT YOU WILL DO:
Make an account on codingbat if you have not already done so.

http://codingbat.com/python (look at the sample code for more help/explanations)

Complete
Warmup-1 (sleep_in , diff21 , monkey_trouble)
String-1 ( hello_name , make_abba)
Comments