In [7]:
# Some command line function
In [1]:
pwd
Out[1]:
u'/Users/matt/Dropbox/Software_Carpentry/Yale_Oct_2014/SWC_Yale_Oct_2014'
In [9]:
# Math/arithmetic function
1+1
2**4
Out[9]:
16
In [10]:
print 'This is a string in python'
This is a string in python
In [12]:
# Variable assignment
x=12
In [13]:
print x
12
In [14]:
type(x)
Out[14]:
int
In [15]:
id(x)
Out[15]:
4298164432
In [16]:
dir(x)
Out[16]:
['__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']
In [17]:
x.numerator
Out[17]:
12
In [18]:
x.denominator
Out[18]:
1
In [20]:
# Can use help(x) to find out more about how the object "x" works
In [21]:
y = float(x)
In [22]:
print y
12.0
In [25]:
# Can explicitly assign a variable to point to a float object with the
# decimal point.
z = 12.
In [26]:
print type(x)
print type(y)
print type(z)
<type 'int'>
<type 'float'>
<type 'float'>
In [27]:
print x/5
2
In [28]:
print x/5.
2.4
In [29]:
y/5
Out[29]:
2.4
In [31]:
# Boolean comparisons
x == y
Out[31]:
True
In [32]:
# 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: 
In [33]:
# 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']
In [36]:
# 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
In [41]:
mylist.count(12.0)
Out[41]:
3
In [42]:
mylist.sort()
In [43]:
mylist
Out[43]:
[12, 12.0, 12.0, 'Camille', 'Hi', 'I am']
In [44]:
mylist.reverse()
In [45]:
mylist
Out[45]:
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
In [48]:
# 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']
In [55]:
# 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
In [56]:
# Use the function
print_list(mylist)
I am
Hi
Camille
12.0
12.0
12
In [57]:
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: 
In [62]:
# 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
In [63]:
funcwargs(1,2,15,mylist,'hello')
1
2
15
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
hello
In [65]:
# Function with a return variable
def addme(x,y) : 
    return x+y
In [68]:
addedvalue = addme(1,15)
In [69]:
print addedvalue
16
In [73]:
# Assigning a variable to a function with no return value
fw = funcwargs(1,2,3)
1
2
3
In [75]:
type(fw)
Out[75]:
NoneType
In [76]:
# 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
In [77]:
if x==y :
    print addme(x,y)
24.0
In [78]:
# Note:  "=" is an assignment, and "==" a test (boolean statement)
In [79]:
# 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)
        
In [80]:
find_list(1,2,[1,2,3])
3
In [85]:
# 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' }
In [87]:
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}
In [90]:
# 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]
In [95]:
# Sample function with key word arguments
def funcwkwargs(float1, float2, shortlist=[1,2], newint=11) :
    print len(shortlist)
    print newint+1
    print float1+float2
In [98]:
funcwkwargs( newint=15,shortlist=[1,2,'a','b'] , float1=12., float2=15.)
4
16
27.0
In [107]:
# 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
In [108]:
arbkwargs(kwarg1=12, kwarg2='hello')
# This gets unpacked to look like: kwargs = {'kwarg1':12, 'kwarg2': 'hello}
kwarg1 12
12
kwarg2 hello
hello
In [112]:
# Let's take a closer look at the dictionary
mydict.keys()
Out[112]:
[0, 'yfloat', 'mylist', 'xint']
In [115]:
#  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.
In [116]:
print mydict.keys
<built-in method keys of dict object at 0x1039636e0>
In [118]:
# 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
Out[118]:
12
In [123]:
# 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.
In [152]:
def arbitrary_argkwarg(*args,**kwargs) :
    keys = kwargs.keys()
    keys.sort()
    print keys
In [153]:
arbitrary_argkwarg(1,'hello', Matt='teacher2', Camille='teacher1',Wendell='TA', Dan='TA')
['Camille', 'Dan', 'Matt', 'Wendell']
In [142]:
#mydict.keys()
In [149]:
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

In [160]:
cat data1.txt
Matthew 34234 934
Camille 495 42390
Matthew 3294 490
Wendell 9403 3049
Jieming 39240 30249
In [178]:
# Create a file handle
f = open('data1.txt','r') 
# 'r' stands for read, we can also 'a' append, and
# 'w' write
In [179]:
# 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'>
In [186]:
# 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
In [189]:
mystring = 'hi, my name is Camille'
In [191]:
mystring.capitalize()
Out[191]:
'Hi, my name is camille'
In [192]:
mystring
Out[192]:
'hi, my name is Camille'
In [195]:
type(mystring.split('m'))
Out[195]:
list
In [196]:
mystring
Out[196]:
'hi, my name is Camille'
In [199]:
mystring.split()
Out[199]:
['hi,', 'my', 'name', 'is', 'Camille']
In [207]:
# 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)
In [222]:
# 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
In [224]:
# 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)
In [209]:
# Run funcread
funcread('data1.txt')
['Matthew', '34234', '934']
['Camille', '495', '42390']
['Matthew', '3294', '490']
['Wendell', '9403', '3049']
['Jieming', '39240', '30249']
In [214]:
# 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
In [225]:
mylist
Out[225]:
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
In [231]:
for x in mydict:
    print x
0
yfloat
mylist
xint
In [236]:
myset = {2,1,3,3}
In [237]:
myset
Out[237]:
{1, 2, 3}
In [238]:
myset.add(5)
In [239]:
myset
Out[239]:
{1, 2, 3, 5}
In [240]:
myset.add(2)
In [241]:
myset
Out[241]:
{1, 2, 3, 5}
In [242]:
mylist[4]
Out[242]:
12.0
In [243]:
mylist[1:4]
Out[243]:
['Hi', 'Camille', 12.0]
In [244]:
2 in mylist
Out[244]:
False
In [245]:
mylist
Out[245]:
['I am', 'Hi', 'Camille', 12.0, 12.0, 12]
In [246]:
mylist.sort()
In [247]:
mylist
Out[247]:
[12.0, 12.0, 12, 'Camille', 'Hi', 'I am']
In [248]:
mystring
Out[248]:
'hi, my name is Camille'
In [252]:
mystring[1:7]
Out[252]:
'i, my '
In [253]:
for c in mystring:
    print c
h
i
,
 
m
y
 
n
a
m
e
 
i
s
 
C
a
m
i
l
l
e
In [254]:
mystring
Out[254]:
'hi, my name is Camille'
In [255]:
mystring.replace('h','H')
Out[255]:
'Hi, my name is Camille'
In [256]:
mystring
Out[256]:
'hi, my name is Camille'
In [257]:
mylist
Out[257]:
[12.0, 12.0, 12, 'Camille', 'Hi', 'I am']
In [258]:
mylist[0] = 121.234234
In [259]:
mylist
Out[259]:
[121.234234, 12.0, 12, 'Camille', 'Hi', 'I am']
In [260]:
mystring
Out[260]:
'hi, my name is Camille'
In [261]:
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
In [262]:
'H' + mystring[1:]
Out[262]:
'Hi, my name is Camille'
In [263]:
'H' + 'i, my name is Camille'
Out[263]:
'Hi, my name is Camille'
In [264]:
mystring
Out[264]:
'hi, my name is Camille'
In [265]:
mystring = 'dfkjsadflk'
In [266]:
mystring
Out[266]:
'dfkjsadflk'
In [268]:
mytuple = (1,2,'hello')
In [269]:
mytuple
Out[269]:
(1, 2, 'hello')
In [271]:
mytuple[1]
Out[271]:
2
In [272]:
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
In [275]:
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

In [276]:
'hello\n'.replace('\n','')
Out[276]:
'hello'
In [277]:
'hello\n'.rstrip('\n')
Out[277]:
'hello'
In [278]:
NoneType
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-278-2dc6e476a4de> in <module>()
----> 1 NoneType

NameError: name 'NoneType' is not defined
In [279]:
type(None)
Out[279]:
NoneType
In [282]:
type(set())
Out[282]:
set
In [283]:
type({1,3})
Out[283]:
set
In [284]:
ValueError
Out[284]:
ValueError
In [285]:
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
In [286]:
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

In [289]:
'C' in {'A', 'B'}
Out[289]:
False
In [292]:
{1,2,45}.difference({2,3,4})
Out[292]:
{1, 45}
In [ ]: