Lecture 09

posted Feb 12, 2013, 11:00 AM by Samuel Konstantinovich   [ updated Feb 12, 2013, 11:26 AM ]
Do now:
Test operations + - / * on different types of data:  integers, floats(decimal), string, and imaginary/complex numbers (3j  or 2+5j)
*we won’t use imaginary or complex but you should be aware they exist in case you see one.

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

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.



Leftover time:
Permission issues
Go over some of yesterday’s python expressions.

Comments