Functions


In [1]:
def my_function():
    print "Hello From My Function!"

In [2]:
def my_function_with_args(username, greeting):
    print "Hello, %s , From My Function!, I wish you %s"%(username, greeting)

In [5]:
def sum_two_numbers(a, b):
    return a + b

In [6]:
# print a simple greeting 
my_function()

#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
my_function_with_args("John Doe", "a great year!")

# after this line x will hold the value 3!
x = sum_two_numbers(1,2)


Hello From My Function!
Hello, John Doe , From My Function!, I wish you a great year!

In [24]:
x


Out[24]:
3

In [40]:
def foo(first, second, third, *therest):
    print "First: %s" % first
    print "Second: %s" % second
    print "Third: %s" % third
    print "And all the rest... %s" % list(therest)

In [41]:
foo(1,2,3,4,5,6,7,8)


First: 1
Second: 2
Third: 3
And all the rest... [4, 5, 6, 7, 8]

In [23]:
def bar(first, second, third, **options):
    if options.get("action") == "sum":
        print "The sum is: %d" % (first + second + third)

    if options.get("number") == "first":
        return first

result = bar(1, 2, 3, action = "sum", number = "first")
print "Result: %s" % result


The sum is: 6
Result: 1

In [22]:
result = bar(1, 2, 3, action = "sum")
print "Result: %s" % result


The sum is: 6
Result: None

In [ ]:

import Modules


In [ ]:


In [13]:
import urllib

In [14]:
help(urllib.urlopen)


Help on function urlopen in module urllib:

urlopen(url, data=None, proxies=None)
    Create a file-like object for the specified URL to read from.


In [12]:
urllib.urlopen("http://www.pchome.com.tw")


Out[12]:
<addinfourl at 4519164384 whose fp = <socket._fileobject object at 0x10d5b1f50>>

In [15]:
import urllib
dir(urllib)
['ContentTooShortError', 'FancyURLopener', 'MAXFTPCACHE', 'URLopener', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_ftperrors', '_get_proxies', '_get_proxy_settings', '_have_ssl', '_hexdig', '_hextochr', '_hostprog', '_is_unicode', '_localhost', '_noheaders', '_nportprog', '_passwdprog', '_portprog', '_queryprog', '_safe_map', '_safe_quoters', '_tagprog', '_thishost', '_typeprog', '_urlopener', '_userprog', '_valueprog', 'addbase', 'addclosehook', 'addinfo', 'addinfourl', 'always_safe', 'basejoin', 'c', 'ftpcache', 'ftperrors', 'ftpwrapper', 'getproxies', 'getproxies_environment', 'getproxies_macosx_sysconf', 'i', 'localhost', 'main', 'noheaders', 'os', 'pathname2url', 'proxy_bypass', 'proxy_bypass_environment', 'proxy_bypass_macosx_sysconf', 'quote', 'quote_plus', 'reporthook', 'socket', 'splitattr', 'splithost', 'splitnport', 'splitpasswd', 'splitport', 'splitquery', 'splittag', 'splittype', 'splituser', 'splitvalue', 'ssl', 'string', 'sys', 'test', 'test1', 'thishost', 'time', 'toBytes', 'unquote', 'unquote_plus', 'unwrap', 'url2pathname', 'urlcleanup', 'urlencode', 'urlopen', 'urlretrieve']


Out[15]:
['ContentTooShortError',
 'FancyURLopener',
 'MAXFTPCACHE',
 'URLopener',
 '__all__',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__version__',
 '_ftperrors',
 '_get_proxies',
 '_get_proxy_settings',
 '_have_ssl',
 '_hexdig',
 '_hextochr',
 '_hostprog',
 '_is_unicode',
 '_localhost',
 '_noheaders',
 '_nportprog',
 '_passwdprog',
 '_portprog',
 '_queryprog',
 '_safe_map',
 '_safe_quoters',
 '_tagprog',
 '_thishost',
 '_typeprog',
 '_urlopener',
 '_userprog',
 '_valueprog',
 'addbase',
 'addclosehook',
 'addinfo',
 'addinfourl',
 'always_safe',
 'basejoin',
 'c',
 'ftpcache',
 'ftperrors',
 'ftpwrapper',
 'getproxies',
 'getproxies_environment',
 'getproxies_macosx_sysconf',
 'i',
 'localhost',
 'main',
 'noheaders',
 'os',
 'pathname2url',
 'proxy_bypass',
 'proxy_bypass_environment',
 'proxy_bypass_macosx_sysconf',
 'quote',
 'quote_plus',
 'reporthook',
 'socket',
 'splitattr',
 'splithost',
 'splitnport',
 'splitpasswd',
 'splitport',
 'splitquery',
 'splittag',
 'splittype',
 'splituser',
 'splitvalue',
 'ssl',
 'string',
 'sys',
 'test',
 'test1',
 'thishost',
 'time',
 'toBytes',
 'unquote',
 'unquote_plus',
 'unwrap',
 'url2pathname',
 'urlcleanup',
 'urlencode',
 'urlopen',
 'urlretrieve']

In [16]:
from urllib import urlopen

In [17]:
urlopen("http://www.pchome.com.tw")


Out[17]:
<addinfourl at 4519164960 whose fp = <socket._fileobject object at 0x10bb9a950>>