# Some command line function
pwd
u'/Users/matt/Dropbox/Software_Carpentry/Yale_Oct_2014/SWC_Yale_Oct_2014'
# Math/arithmetic function
1+1
2**4
16
print 'This is a string in python'
This is a string in python
# Variable assignment
x=12
print x
12
type(x)
int
id(x)
4298164432
dir(x)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
x.numerator
12
x.denominator
1
# Can use help(x) to find out more about how the object "x" works
y = float(x)
print y
12.0
# Can explicitly assign a variable to point to a float object with the
# decimal point.
z = 12.
print type(x)
print type(y)
print type(z)
<type 'int'> <type 'float'> <type 'float'>
print x/5
2
print x/5.
2.4
y/5
2.4
# Boolean comparisons
x == y
True
# This is an assertion statement: raises an error if the enclosed statement
# is not true.
assert( x/5 == y/5 )
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-32-2416701d0ac0> in <module>() ----> 1 assert( x/5 == y/5 ) AssertionError:
# Python lists can be iterated over, and are defined []
mylist = [x, y, z, 'Hi', 'I am', 'Camille']
print mylist
[12, 12.0, 12.0, 'Hi', 'I am', 'Camille']
# for loop in python -- Note, python is whitespace VERY sensitive
for ml in mylist :
print ml
12 12.0 12.0 Hi I am Camille
mylist.count(12.0)
3
mylist.sort()
mylist
[12, 12.0, 12.0, 'Camille', 'Hi', 'I am']
mylist.reverse()
mylist
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
# List accessing and slicing, slices m:n is m through and NOT including n
print mylist[0], mylist[3], mylist[3:5], mylist[1:], mylist[:3]
I am 12.0 [12.0, 12.0] ['Hi', 'Camille', 12.0, 12.0, 12] ['I am', 'Hi', 'Camille']
# Functions in python
# This is a function that takes a list, and prints each element
def print_list(samplelist) :
# Sample use of an assertion to help you check for bugs
assert( type(samplelist) == list )
for sl in samplelist :
print sl
# Use the function
print_list(mylist)
I am Hi Camille 12.0 12.0 12
print_list(x)
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-57-9a0fd28c8245> in <module>() ----> 1 print_list(x) <ipython-input-55-adf8bf1005ed> in print_list(samplelist) 3 def print_list(samplelist) : 4 # Sample use of an assertion to help you check for bugs ----> 5 assert( type(samplelist) == list ) 6 for sl in samplelist : 7 print sl AssertionError:
# Functions in python are actually pretty cool and flexible
def funcwargs(*args) :
# args is now treated as a list inside this function
for arg in args :
print arg
funcwargs(1,2,15,mylist,'hello')
1 2 15 ['I am', 'Hi', 'Camille', 12.0, 12.0, 12] hello
# Function with a return variable
def addme(x,y) :
return x+y
addedvalue = addme(1,15)
print addedvalue
16
# Assigning a variable to a function with no return value
fw = funcwargs(1,2,3)
1 2 3
type(fw)
NoneType
# Quick exercise: Write a function that takes in any arbitrary arguments,
# prints the type of each argument, and if the type is a list, print the
# length of the list
if x==y :
print addme(x,y)
24.0
# Note: "=" is an assignment, and "==" a test (boolean statement)
# Exercise solution:
def find_list(*args) :
# args is now a list of everything you fed into this function that was
# comma delimeted i.e. find_list(arg1,arg2,....) --> args = [arg1,arg2...]
for arg in args :
if type(arg) == list :
print len(arg)
find_list(1,2,[1,2,3])
3
# Dictionaries - YAY! Defined within {}, and these map some key word to a
# value of some other type
mydict = {'xint':x, 'yfloat':y, 'mylist': mylist, 0: 'The 0th key word' }
print mydict['xint']
print mydict[0]
print mydict
12 The 0th key word {0: 'The 0th key word', 'yfloat': 12.0, 'mylist': ['I am', 'Hi', 'Camille', 12.0, 12.0, 12], 'xint': 12}
# A difference between dictionaries and lists is that dictionaries do NOT
# preserve order, and lists do.
print mylist
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
# Sample function with key word arguments
def funcwkwargs(float1, float2, shortlist=[1,2], newint=11) :
print len(shortlist)
print newint+1
print float1+float2
funcwkwargs( newint=15,shortlist=[1,2,'a','b'] , float1=12., float2=15.)
4 16 27.0
# Sample function with arbitrary key word arguments
def arbkwargs(**kwargs) :
# Here, kwargs gets "unpacked" as a dictionary
for kw,arg in kwargs.iteritems() :
print kw, arg
print arg
arbkwargs(kwarg1=12, kwarg2='hello')
# This gets unpacked to look like: kwargs = {'kwarg1':12, 'kwarg2': 'hello}
kwarg1 12 12 kwarg2 hello hello
# Let's take a closer look at the dictionary
mydict.keys()
[0, 'yfloat', 'mylist', 'xint']
# Any dictionary object, such as mydict, will have built in "methods"
# which are really just functions that are internal to this object.
# Methods have associated () when you call them.
print mydict.keys
<built-in method keys of dict object at 0x1039636e0>
# This is an attribute of the object x (which is an int object). Note,
# no parenthesis needed here, because x.numerator simply already points
# to the value 12.
x.numerator
12
# Exercise: Create a function that takes in an arbitrary number of args
# AND kwargs, and print a sorted list of the key words.
# If I use your function
# arbitrary_argkwarg(1, 'hello', Matt='teacher2', Camille='teacher1')
# I should get back
# Camille
# Matt
# Bonus : Print a list of the key words in order of the sorted values.
def arbitrary_argkwarg(*args,**kwargs) :
keys = kwargs.keys()
keys.sort()
print keys
arbitrary_argkwarg(1,'hello', Matt='teacher2', Camille='teacher1',Wendell='TA', Dan='TA')
['Camille', 'Dan', 'Matt', 'Wendell']
#mydict.keys()
help(list.sort)
Help on method_descriptor: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1
cat data1.txt
Matthew 34234 934 Camille 495 42390 Matthew 3294 490 Wendell 9403 3049 Jieming 39240 30249
# Create a file handle
f = open('data1.txt','r')
# 'r' stands for read, we can also 'a' append, and
# 'w' write
# Iterate over a file
for line in f :
print line
print type(line)
Matthew 34234 934 <type 'str'> Camille 495 42390 <type 'str'> Matthew 3294 490 <type 'str'> Wendell 9403 3049 <type 'str'> Jieming 39240 30249 <type 'str'>
# Note, there is an implied newline character at the end of each line
# in the file that is read in
print 'hi\n'
print 'camille'
hi camille
mystring = 'hi, my name is Camille'
mystring.capitalize()
'Hi, my name is camille'
mystring
'hi, my name is Camille'
type(mystring.split('m'))
list
mystring
'hi, my name is Camille'
mystring.split()
['hi,', 'my', 'name', 'is', 'Camille']
# Let's make a function to read in a file and do stuff with it
def funcread(fname) :
# Create a file handle so I can read the file
f = open(fname,'r')
for line in f :
# Here's where I do stuff...
manage_line(line)
# Let's create a function to do stuff to the line
def manage_line(line) :
# Splitting the line returns a list of strings that have been split
# by some value (default white space), then print if the second value
# is greater than the first
splitline = line.split()
if int(splitline[1]) > int(splitline[2]) :
print line
return line
# Testing out manage_line
def test_manage_line() :
# A good reason for manage_line to have a return value is to make sure
# that the function does what you think it should do when you give it
# a known argument
assert(manage_line('Matthew 34234 934') != NoneType)
# Run funcread
funcread('data1.txt')
['Matthew', '34234', '934'] ['Camille', '495', '42390'] ['Matthew', '3294', '490'] ['Wendell', '9403', '3049'] ['Jieming', '39240', '30249']
# Exercise : Read in data1.txt, print if the second column is greater than
# the third column. Hint: You can individually access each item in the
# line using the split method, and you will need to recognize that you now
# have a list of strings once you've split that line. Double hint: Modify
# the manage_line function, and manage_line needs to be (re-)defined
# before the funcread function is (re-)defined
mylist
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
for x in mydict:
print x
0 yfloat mylist xint
myset = {2,1,3,3}
myset
{1, 2, 3}
myset.add(5)
myset
{1, 2, 3, 5}
myset.add(2)
myset
{1, 2, 3, 5}
mylist[4]
12.0
mylist[1:4]
['Hi', 'Camille', 12.0]
2 in mylist
False
mylist
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
mylist.sort()
mylist
[12.0, 12.0, 12, 'Camille', 'Hi', 'I am']
mystring
'hi, my name is Camille'
mystring[1:7]
'i, my '
for c in mystring:
print c
h i , m y n a m e i s C a m i l l e
mystring
'hi, my name is Camille'
mystring.replace('h','H')
'Hi, my name is Camille'
mystring
'hi, my name is Camille'
mylist
[12.0, 12.0, 12, 'Camille', 'Hi', 'I am']
mylist[0] = 121.234234
mylist
[121.234234, 12.0, 12, 'Camille', 'Hi', 'I am']
mystring
'hi, my name is Camille'
mystring[0] = 'H'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-261-ce25d5188d9f> in <module>() ----> 1 mystring[0] = 'H' TypeError: 'str' object does not support item assignment
'H' + mystring[1:]
'Hi, my name is Camille'
'H' + 'i, my name is Camille'
'Hi, my name is Camille'
mystring
'hi, my name is Camille'
mystring = 'dfkjsadflk'
mystring
'dfkjsadflk'
mytuple = (1,2,'hello')
mytuple
(1, 2, 'hello')
mytuple[1]
2
mytuple[1] = 234
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-272-6f2b47d2a04c> in <module>() ----> 1 mytuple[1] = 234 TypeError: 'tuple' object does not support item assignment
help(str)
Help on class str in module __builtin__: class str(basestring) | str(object='') -> string | | Return a nice string representation of the object. | If the argument is a string, the return value is the same object. | | Method resolution order: | str | basestring | object | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y | | __contains__(...) | x.__contains__(y) <==> y in x | | __eq__(...) | x.__eq__(y) <==> x==y | | __format__(...) | S.__format__(format_spec) -> string | | Return a formatted version of S as described by format_spec. | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getnewargs__(...) | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __gt__(...) | x.__gt__(y) <==> x>y | | __hash__(...) | x.__hash__() <==> hash(x) | | __le__(...) | x.__le__(y) <==> x<=y | | __len__(...) | x.__len__() <==> len(x) | | __lt__(...) | x.__lt__(y) <==> x<y | | __mod__(...) | x.__mod__(y) <==> x%y | | __mul__(...) | x.__mul__(n) <==> x*n | | __ne__(...) | x.__ne__(y) <==> x!=y | | __repr__(...) | x.__repr__() <==> repr(x) | | __rmod__(...) | x.__rmod__(y) <==> y%x | | __rmul__(...) | x.__rmul__(n) <==> n*x | | __sizeof__(...) | S.__sizeof__() -> size of S in memory, in bytes | | __str__(...) | x.__str__() <==> str(x) | | capitalize(...) | S.capitalize() -> string | | Return a copy of the string S with only its first character | capitalized. | | center(...) | S.center(width[, fillchar]) -> string | | Return S centered in a string of length width. Padding is | done using the specified fill character (default is a space) | | count(...) | S.count(sub[, start[, end]]) -> int | | Return the number of non-overlapping occurrences of substring sub in | string S[start:end]. Optional arguments start and end are interpreted | as in slice notation. | | decode(...) | S.decode([encoding[,errors]]) -> object | | Decodes S using the codec registered for encoding. encoding defaults | to the default encoding. errors may be given to set a different error | handling scheme. Default is 'strict' meaning that encoding errors raise | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' | as well as any other name registered with codecs.register_error that is | able to handle UnicodeDecodeErrors. | | encode(...) | S.encode([encoding[,errors]]) -> object | | Encodes S using the codec registered for encoding. encoding defaults | to the default encoding. errors may be given to set a different error | handling scheme. Default is 'strict' meaning that encoding errors raise | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and | 'xmlcharrefreplace' as well as any other name registered with | codecs.register_error that is able to handle UnicodeEncodeErrors. | | endswith(...) | S.endswith(suffix[, start[, end]]) -> bool | | Return True if S ends with the specified suffix, False otherwise. | With optional start, test S beginning at that position. | With optional end, stop comparing S at that position. | suffix can also be a tuple of strings to try. | | expandtabs(...) | S.expandtabs([tabsize]) -> string | | Return a copy of S where all tab characters are expanded using spaces. | If tabsize is not given, a tab size of 8 characters is assumed. | | find(...) | S.find(sub [,start [,end]]) -> int | | Return the lowest index in S where substring sub is found, | such that sub is contained within S[start:end]. Optional | arguments start and end are interpreted as in slice notation. | | Return -1 on failure. | | format(...) | S.format(*args, **kwargs) -> string | | Return a formatted version of S, using substitutions from args and kwargs. | The substitutions are identified by braces ('{' and '}'). | | index(...) | S.index(sub [,start [,end]]) -> int | | Like S.find() but raise ValueError when the substring is not found. | | isalnum(...) | S.isalnum() -> bool | | Return True if all characters in S are alphanumeric | and there is at least one character in S, False otherwise. | | isalpha(...) | S.isalpha() -> bool | | Return True if all characters in S are alphabetic | and there is at least one character in S, False otherwise. | | isdigit(...) | S.isdigit() -> bool | | Return True if all characters in S are digits | and there is at least one character in S, False otherwise. | | islower(...) | S.islower() -> bool | | Return True if all cased characters in S are lowercase and there is | at least one cased character in S, False otherwise. | | isspace(...) | S.isspace() -> bool | | Return True if all characters in S are whitespace | and there is at least one character in S, False otherwise. | | istitle(...) | S.istitle() -> bool | | Return True if S is a titlecased string and there is at least one | character in S, i.e. uppercase characters may only follow uncased | characters and lowercase characters only cased ones. Return False | otherwise. | | isupper(...) | S.isupper() -> bool | | Return True if all cased characters in S are uppercase and there is | at least one cased character in S, False otherwise. | | join(...) | S.join(iterable) -> string | | Return a string which is the concatenation of the strings in the | iterable. The separator between elements is S. | | ljust(...) | S.ljust(width[, fillchar]) -> string | | Return S left-justified in a string of length width. Padding is | done using the specified fill character (default is a space). | | lower(...) | S.lower() -> string | | Return a copy of the string S converted to lowercase. | | lstrip(...) | S.lstrip([chars]) -> string or unicode | | Return a copy of the string S with leading whitespace removed. | If chars is given and not None, remove characters in chars instead. | If chars is unicode, S will be converted to unicode before stripping | | partition(...) | S.partition(sep) -> (head, sep, tail) | | Search for the separator sep in S, and return the part before it, | the separator itself, and the part after it. If the separator is not | found, return S and two empty strings. | | replace(...) | S.replace(old, new[, count]) -> string | | Return a copy of string S with all occurrences of substring | old replaced by new. If the optional argument count is | given, only the first count occurrences are replaced. | | rfind(...) | S.rfind(sub [,start [,end]]) -> int | | Return the highest index in S where substring sub is found, | such that sub is contained within S[start:end]. Optional | arguments start and end are interpreted as in slice notation. | | Return -1 on failure. | | rindex(...) | S.rindex(sub [,start [,end]]) -> int | | Like S.rfind() but raise ValueError when the substring is not found. | | rjust(...) | S.rjust(width[, fillchar]) -> string | | Return S right-justified in a string of length width. Padding is | done using the specified fill character (default is a space) | | rpartition(...) | S.rpartition(sep) -> (head, sep, tail) | | Search for the separator sep in S, starting at the end of S, and return | the part before it, the separator itself, and the part after it. If the | separator is not found, return two empty strings and S. | | rsplit(...) | S.rsplit([sep [,maxsplit]]) -> list of strings | | Return a list of the words in the string S, using sep as the | delimiter string, starting at the end of the string and working | to the front. If maxsplit is given, at most maxsplit splits are | done. If sep is not specified or is None, any whitespace string | is a separator. | | rstrip(...) | S.rstrip([chars]) -> string or unicode | | Return a copy of the string S with trailing whitespace removed. | If chars is given and not None, remove characters in chars instead. | If chars is unicode, S will be converted to unicode before stripping | | split(...) | S.split([sep [,maxsplit]]) -> list of strings | | Return a list of the words in the string S, using sep as the | delimiter string. If maxsplit is given, at most maxsplit | splits are done. If sep is not specified or is None, any | whitespace string is a separator and empty strings are removed | from the result. | | splitlines(...) | S.splitlines(keepends=False) -> list of strings | | Return a list of the lines in S, breaking at line boundaries. | Line breaks are not included in the resulting list unless keepends | is given and true. | | startswith(...) | S.startswith(prefix[, start[, end]]) -> bool | | Return True if S starts with the specified prefix, False otherwise. | With optional start, test S beginning at that position. | With optional end, stop comparing S at that position. | prefix can also be a tuple of strings to try. | | strip(...) | S.strip([chars]) -> string or unicode | | Return a copy of the string S with leading and trailing | whitespace removed. | If chars is given and not None, remove characters in chars instead. | If chars is unicode, S will be converted to unicode before stripping | | swapcase(...) | S.swapcase() -> string | | Return a copy of the string S with uppercase characters | converted to lowercase and vice versa. | | title(...) | S.title() -> string | | Return a titlecased version of S, i.e. words start with uppercase | characters, all remaining cased characters have lowercase. | | translate(...) | S.translate(table [,deletechars]) -> string | | Return a copy of the string S, where all characters occurring | in the optional argument deletechars are removed, and the | remaining characters have been mapped through the given | translation table, which must be a string of length 256 or None. | If the table argument is None, no translation is applied and | the operation simply removes the characters in deletechars. | | upper(...) | S.upper() -> string | | Return a copy of the string S converted to uppercase. | | zfill(...) | S.zfill(width) -> string | | Pad a numeric string S with zeros on the left, to fill a field | of the specified width. The string S is never truncated. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T
'hello\n'.replace('\n','')
'hello'
'hello\n'.rstrip('\n')
'hello'
NoneType
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-278-2dc6e476a4de> in <module>() ----> 1 NoneType NameError: name 'NoneType' is not defined
type(None)
NoneType
type(set())
set
type({1,3})
set
ValueError
ValueError
float('dskfjsdlfk')
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-285-1a9e04a723bc> in <module>() ----> 1 float('dskfjsdlfk') ValueError: could not convert string to float: dskfjsdlfk
help(set)
Help on class set in module __builtin__: class set(object) | set() -> new empty set object | set(iterable) -> new set object | | Build an unordered collection of unique elements. | | Methods defined here: | | __and__(...) | x.__and__(y) <==> x&y | | __cmp__(...) | x.__cmp__(y) <==> cmp(x,y) | | __contains__(...) | x.__contains__(y) <==> y in x. | | __eq__(...) | x.__eq__(y) <==> x==y | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __gt__(...) | x.__gt__(y) <==> x>y | | __iand__(...) | x.__iand__(y) <==> x&=y | | __init__(...) | x.__init__(...) initializes x; see help(type(x)) for signature | | __ior__(...) | x.__ior__(y) <==> x|=y | | __isub__(...) | x.__isub__(y) <==> x-=y | | __iter__(...) | x.__iter__() <==> iter(x) | | __ixor__(...) | x.__ixor__(y) <==> x^=y | | __le__(...) | x.__le__(y) <==> x<=y | | __len__(...) | x.__len__() <==> len(x) | | __lt__(...) | x.__lt__(y) <==> x<y | | __ne__(...) | x.__ne__(y) <==> x!=y | | __or__(...) | x.__or__(y) <==> x|y | | __rand__(...) | x.__rand__(y) <==> y&x | | __reduce__(...) | Return state information for pickling. | | __repr__(...) | x.__repr__() <==> repr(x) | | __ror__(...) | x.__ror__(y) <==> y|x | | __rsub__(...) | x.__rsub__(y) <==> y-x | | __rxor__(...) | x.__rxor__(y) <==> y^x | | __sizeof__(...) | S.__sizeof__() -> size of S in memory, in bytes | | __sub__(...) | x.__sub__(y) <==> x-y | | __xor__(...) | x.__xor__(y) <==> x^y | | add(...) | Add an element to a set. | | This has no effect if the element is already present. | | clear(...) | Remove all elements from this set. | | copy(...) | Return a shallow copy of a set. | | difference(...) | Return the difference of two or more sets as a new set. | | (i.e. all elements that are in this set but not the others.) | | difference_update(...) | Remove all elements of another set from this set. | | discard(...) | Remove an element from a set if it is a member. | | If the element is not a member, do nothing. | | intersection(...) | Return the intersection of two or more sets as a new set. | | (i.e. elements that are common to all of the sets.) | | intersection_update(...) | Update a set with the intersection of itself and another. | | isdisjoint(...) | Return True if two sets have a null intersection. | | issubset(...) | Report whether another set contains this set. | | issuperset(...) | Report whether this set contains another set. | | pop(...) | Remove and return an arbitrary set element. | Raises KeyError if the set is empty. | | remove(...) | Remove an element from a set; it must be a member. | | If the element is not a member, raise a KeyError. | | symmetric_difference(...) | Return the symmetric difference of two sets as a new set. | | (i.e. all elements that are in exactly one of the sets.) | | symmetric_difference_update(...) | Update a set with the symmetric difference of itself and another. | | union(...) | Return the union of sets as a new set. | | (i.e. all elements that are in either set.) | | update(...) | Update a set with the union of itself and others. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T
'C' in {'A', 'B'}
False
{1,2,45}.difference({2,3,4})
{1, 45}