常用内建模块
from datetime import datetime
m = datetime.now()
class ss(object):
def __init__(cls, name):
cls.name = name
@classmethod
def fn(self):
return 1
m=ss('aa')
m.name
'aa'
from collections import defaultdict
dd = defaultdict(lambda: 'N/A')
dd['key1'] = 'a'
dd['key1']
'a'
dd['key2']
'N/A'
dd
defaultdict(<function __main__.<lambda>>, {'key1': 'a', 'key2': 'N/A'})
FIFO的dict
from collections import OrderedDict
class LastUpdatedOrderedDict(OrderedDict):
def __init__(self, capacity):
self._capacity = capacity
super(LastUpdatedOrderedDict, self).__init__([('a',1),('b',2)]) #初始化后自动调用__setitem__
def __setitem__(self, key, value):
containsKey = 1 if key in self else 0
if len(self) - containsKey >= self._capacity:
last = self.popitem(last=False)
print('remove:', last)
if containsKey:
del self[key]
print('set:', (key, value))
else:
print('add:', (key, value))
OrderedDict.__setitem__(self, key, value)
dd = LastUpdatedOrderedDict(5)
add: ('a', 1)
add: ('b', 2)
dd['c']=3
add: ('c', 3)
dd['d']=4
dd['e']=5
add: ('d', 4)
add: ('e', 5)
dd['f']=6
remove: ('a', 1)
add: ('f', 6)
#统计字符个数
from collections import Counter
c = Counter()
for ch in 'programming':
c[ch] = c[ch] + 1
c
Counter({'a': 1, 'g': 2, 'i': 1, 'm': 2, 'n': 1, 'o': 1, 'p': 1, 'r': 2})
contextlib
$ 任何对象只要实现了上下文管理都是可以用到with语句的 $
class Query(object):
def __init__(self, name):
self.name = name
def __enter__(self):
print('Begin')
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type:
print('Error')
else:
print('End')
def query(self):
print('Query info about %s' % self.name)
with Query('lovelyfrog') as q:
q.query()
Begin
Query info about lovelyfrog
End
@contextmanager
$ 编写enter和exit仍然很繁琐,因此Python的标准库contextlib提供了更简单的写法,上面的代码可以改写如下:$
from contextlib import contextmanager
class Query(object):
def __init__(self, name):
self.name = name
def query(self):
print('Query info about %s...' % self.name)
@contextmanager
def create_query(name):
print('Begin')
q = Query(name)
# 变成generator的函数,在每次调用next()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行。
yield q #返回的当作as 后面的变量
print('End')
with create_query('lovelyfrog') as q:
q.query()
Begin
Query info about lovelyfrog...
End
代码的执行顺序是:
- with语句首先执行yield之前的语句;
- yield调用会执行with语句内部的所有语句;
- 最后执行yield之后的语句。
@closing
如果一个对象没有实现上下文,我们就不能把它用于with语句。这个时候,可以用closing()来把该对象变为上下文对象
from contextlib import closing
from urllib.request import urlopen
with closing(urlopen('https://www.python.org')) as page:
for line in page:
print(line)
b'<!doctype html>\n'
b'<!--[if lt IE 7]> <html class="no-js ie6 lt-ie7 lt-ie8 lt-ie9"> <![endif]-->\n'
b'<!--[if IE 7]> <html class="no-js ie7 lt-ie8 lt-ie9"> <![endif]-->\n'
b'<!--[if IE 8]> <html class="no-js ie8 lt-ie9"> <![endif]-->\n'
b'<!--[if gt IE 8]><!--><html class="no-js" lang="en" dir="ltr"> <!--<![endif]-->\n'
b'\n'
b'<head>\n'
b' <meta charset="utf-8">\n'
b' <meta http-equiv="X-UA-Compatible" content="IE=edge">\n'
b'\n'
b' <link rel="prefetch" href="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">\n'
b'\n'
b' <meta name="application-name" content="Python.org">\n'
b' <meta name="msapplication-tooltip" content="The official home of the Python Programming Language">\n'
b' <meta name="apple-mobile-web-app-title" content="Python.org">\n'
b' <meta name="apple-mobile-web-app-capable" content="yes">\n'
b' <meta name="apple-mobile-web-app-status-bar-style" content="black">\n'
b'\n'
b' <meta name="viewport" content="width=device-width, initial-scale=1.0">\n'
b' <meta name="HandheldFriendly" content="True">\n'
b' <meta name="format-detection" content="telephone=no">\n'
b' <meta http-equiv="cleartype" content="on">\n'
b' <meta http-equiv="imagetoolbar" content="false">\n'
b'\n'
b' <script src="/static/js/libs/modernizr.js"></script>\n'
b'\n'
b' <link href="/static/stylesheets/style.css" rel="stylesheet" type="text/css" title="default" />\n'
b' <link href="/static/stylesheets/mq.css" rel="stylesheet" type="text/css" media="not print, braille, embossed, speech, tty" />\n'
b' \n'
b'\n'
b' <!--[if (lte IE 8)&(!IEMobile)]>\n'
b' <link href="/static/stylesheets/no-mq.css" rel="stylesheet" type="text/css" media="screen" />\n'
b' \n'
b' \n'
b' <![endif]-->\n'
b'\n'
b' \n'
b' <link rel="icon" type="image/x-icon" href="/static/favicon.ico">\n'
b' <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/apple-touch-icon-144x144-precomposed.png">\n'
b' <link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/apple-touch-icon-114x114-precomposed.png">\n'
b' <link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/apple-touch-icon-72x72-precomposed.png">\n'
b' <link rel="apple-touch-icon-precomposed" href="/static/apple-touch-icon-precomposed.png">\n'
b' <link rel="apple-touch-icon" href="/static/apple-touch-icon-precomposed.png">\n'
b'\n'
b' \n'
b' <meta name="msapplication-TileImage" content="/static/metro-icon-144x144-precomposed.png"><!-- white shape -->\n'
b' <meta name="msapplication-TileColor" content="#3673a5"><!-- python blue -->\n'
b' <meta name="msapplication-navbutton-color" content="#3673a5">\n'
b'\n'
b' <title>Welcome to Python.org</title>\n'
b'\n'
b' <meta name="description" content="The official home of the Python Programming Language">\n'
b' <meta name="keywords" content="Python programming language object oriented web free open source software license documentation download community">\n'
b'\n'
b' \n'
b' <meta property="og:type" content="website">\n'
b' <meta property="og:site_name" content="Python.org">\n'
b' <meta property="og:title" content="Welcome to Python.org">\n'
b' <meta property="og:description" content="The official home of the Python Programming Language">\n'
b' \n'
b' <meta property="og:image" content="https://www.python.org/static/opengraph-icon-200x200.png">\n'
b' <meta property="og:image:secure_url" content="https://www.python.org/static/opengraph-icon-200x200.png">\n'
b' \n'
b' <meta property="og:url" content="https://www.python.org/">\n'
b'\n'
b' <link rel="author" href="/static/humans.txt">\n'
b'\n'
b' <link rel="alternate" type="application/rss+xml" title="Python Enhancement Proposals"\n'
b' href="https://www.python.org/dev/peps/peps.rss/">\n'
b' <link rel="alternate" type="application/rss+xml" title="Python Job Opportunities"\n'
b' href="https://www.python.org/jobs/feed/rss/">\n'
b' <link rel="alternate" type="application/rss+xml" title="Python Software Foundation News"\n'
b' href="https://feeds.feedburner.com/PythonSoftwareFoundationNews">\n'
b' <link rel="alternate" type="application/rss+xml" title="Python Insider"\n'
b' href="https://feeds.feedburner.com/PythonInsider">\n'
b'\n'
b' \n'
b'\n'
b' \n'
b' <script type="application/ld+json">\n'
b' {\n'
b' "@context": "http://schema.org",\n'
b' "@type": "WebSite",\n'
b' "url": "https://www.python.org/",\n'
b' "potentialAction": {\n'
b' "@type": "SearchAction",\n'
b' "target": "https://www.python.org/search/?q={search_term_string}",\n'
b' "query-input": "required name=search_term_string"\n'
b' }\n'
b' }\n'
b' </script>\n'
b'\n'
b' \n'
b' <script type="text/javascript">\n'
b' var _gaq = _gaq || [];\n'
b" _gaq.push(['_setAccount', 'UA-39055973-1']);\n"
b" _gaq.push(['_trackPageview']);\n"
b'\n'
b' (function() {\n'
b" var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n"
b" ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n"
b" var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n"
b' })();\n'
b' </script>\n'
b' \n'
b'</head>\n'
b'\n'
b'<body class="python home" id="homepage">\n'
b'\n'
b' <div id="touchnav-wrapper">\n'
b'\n'
b' <div id="nojs" class="do-not-print">\n'
b' <p><strong>Notice:</strong> While Javascript is not essential for this website, your interaction with the content will be limited. Please turn Javascript on for the full experience. </p>\n'
b' </div>\n'
b'\n'
b' <!--[if lt IE 8]>\n'
b' <div id="oldie-warning" class="do-not-print">\n'
b' <p><strong>Notice:</strong> Your browser is <em>ancient</em> and <a href="http://www.ie6countdown.com/">Microsoft agrees</a>. <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience a better web.</p>\n'
b' </div>\n'
b' <![endif]-->\n'
b'\n'
b' <!-- Sister Site Links -->\n'
b' <div id="top" class="top-bar do-not-print">\n'
b'\n'
b' <nav class="meta-navigation container" role="navigation">\n'
b'\n'
b' \n'
b' <div class="skip-link screen-reader-text">\n'
b' <a href="#content" title="Skip to content">Skip to content</a>\n'
b' </div>\n'
b'\n'
b' \n'
b' <a id="close-python-network" class="jump-link" href="#python-network" aria-hidden="true">\n'
b' <span aria-hidden="true" class="icon-arrow-down"><span>▼</span></span> Close\n'
b' </a>\n'
b'\n'
b' \n'
b'\n'
b'<ul class="menu" role="tree">\n'
b' \n'
b' <li class="python-meta current_item selectedcurrent_branch selected">\n'
b' <a href="/" title="The Python Programming Language" class="current_item selectedcurrent_branch selected">Python</a>\n'
b' </li>\n'
b' \n'
b' <li class="psf-meta ">\n'
b' <a href="/psf-landing/" title="The Python Software Foundation" >PSF</a>\n'
b' </li>\n'
b' \n'
b' <li class="docs-meta ">\n'
b' <a href="https://docs.python.org" title="Python Documentation" >Docs</a>\n'
b' </li>\n'
b' \n'
b' <li class="pypi-meta ">\n'
b' <a href="https://pypi.python.org/" title="Python Package Index" >PyPI</a>\n'
b' </li>\n'
b' \n'
b' <li class="jobs-meta ">\n'
b' <a href="/jobs/" title="Python Job Board" >Jobs</a>\n'
b' </li>\n'
b' \n'
b' <li class="shop-meta ">\n'
b' <a href="/community/" title="Python Community" >Community</a>\n'
b' </li>\n'
b' \n'
b'</ul>\n'
b'\n'
b'\n'
b' <a id="python-network" class="jump-link" href="#top" aria-hidden="true">\n'
b' <span aria-hidden="true" class="icon-arrow-up"><span>▲</span></span> The Python Network\n'
b' </a>\n'
b'\n'
b' </nav>\n'
b'\n'
b' </div>\n'
b'\n'
b' <!-- Header elements -->\n'
b' <header class="main-header" role="banner">\n'
b' <div class="container">\n'
b'\n'
b' <h1 class="site-headline">\n'
b' <a href="/"><img class="python-logo" src="/static/img/python-logo.png" alt="python™"></a>\n'
b' </h1>\n'
b'\n'
b' <div class="options-bar do-not-print">\n'
b'\n'
b' \n'
b' <a id="site-map-link" class="jump-to-menu" href="#site-map"><span class="menu-icon">≡</span> Menu</a><form class="search-the-site" action="/search/" method="get">\n'
b' <fieldset title="Search Python.org">\n'
b'\n'
b' <span aria-hidden="true" class="icon-search"></span>\n'
b'\n'
b' <label class="screen-reader-text" for="id-search-field">Search This Site</label>\n'
b' <input id="id-search-field" name="q" type="search" role="textbox" class="search-field" placeholder="Search" value="" tabindex="1">\n'
b'\n'
b' <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">\n'
b' GO\n'
b' </button>\n'
b'\n'
b' \n'
b' <!--[if IE]><input type="text" style="display: none;" disabled="disabled" size="1" tabindex="4"><![endif]-->\n'
b'\n'
b' </fieldset>\n'
b' </form><span class="breaker"></span><div class="adjust-font-size" aria-hidden="true">\n'
b' <ul class="navigation menu" aria-label="Adjust Text Size on Page">\n'
b' <li class="tier-1 last" aria-haspopup="true">\n'
b' <a href="#" class="action-trigger"><strong><small>A</small> A</strong></a>\n'
b' <ul class="subnav menu">\n'
b' <li class="tier-2 element-1" role="treeitem"><a class="text-shrink" title="Make Text Smaller" href="javascript:;">Smaller</a></li>\n'
b' <li class="tier-2 element-2" role="treeitem"><a class="text-grow" title="Make Text Larger" href="javascript:;">Larger</a></li>\n'
b' <li class="tier-2 element-3" role="treeitem"><a class="text-reset" title="Reset any font size changes I have made" href="javascript:;">Reset</a></li>\n'
b' </ul>\n'
b' </li>\n'
b' </ul>\n'
b' </div><div class="winkwink-nudgenudge">\n'
b' <ul class="navigation menu" aria-label="Social Media Navigation">\n'
b' <li class="tier-1 last" aria-haspopup="true">\n'
b' <a href="#" class="action-trigger">Socialize</a>\n'
b' <ul class="subnav menu">\n'
b' <li class="tier-2 element-1" role="treeitem"><a href="http://plus.google.com/+Python"><span aria-hidden="true" class="icon-google-plus"></span>Google+</a></li>\n'
b' <li class="tier-2 element-2" role="treeitem"><a href="http://www.facebook.com/pythonlang?fref=ts"><span aria-hidden="true" class="icon-facebook"></span>Facebook</a></li>\n'
b' <li class="tier-2 element-3" role="treeitem"><a href="http://twitter.com/ThePSF"><span aria-hidden="true" class="icon-twitter"></span>Twitter</a></li>\n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/community/irc/"><span aria-hidden="true" class="icon-freenode"></span>Chat on IRC</a></li>\n'
b' </ul>\n'
b' </li>\n'
b' </ul>\n'
b' </div><div class="account-signin">\n'
b' <ul class="navigation menu" aria-label="Social Media Navigation">\n'
b' <li class="tier-1 last" aria-haspopup="true">\n'
b' \n'
b' <a href="/accounts/login/" title="Sign Up or Sign In to Python.org">Sign In</a>\n'
b' <ul class="subnav menu">\n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/accounts/signup/">Sign Up / Register</a></li>\n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/accounts/login/">Sign In</a></li>\n'
b' </ul>\n'
b' \n'
b' </li>\n'
b' </ul>\n'
b' </div>\n'
b'\n'
b' </div><!-- end options-bar -->\n'
b'\n'
b' <nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">\n'
b' \n'
b' \n'
b'<ul class="navigation menu" role="menubar" aria-label="Main Navigation">\n'
b' \n'
b' \n'
b' \n'
b' <li id="about" class="tier-1 element-1 " aria-haspopup="true">\n'
b' <a href="/about/" title="" class="">About</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu" role="menu" aria-hidden="true">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/about/apps/" title="">Applications</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/about/quotes/" title="">Quotes</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/about/gettingstarted/" title="">Getting Started</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/about/help/" title="">Help</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="http://brochure.getpython.info/" title="">Python Brochure</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' \n'
b' \n'
b' <li id="downloads" class="tier-1 element-2 " aria-haspopup="true">\n'
b' <a href="/downloads/" title="" class="">Downloads</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu" role="menu" aria-hidden="true">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/downloads/" title="">All releases</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/downloads/source/" title="">Source code</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/downloads/windows/" title="">Windows</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/downloads/mac-osx/" title="">Mac OS X</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="/download/other/" title="">Other Platforms</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="https://docs.python.org/3/license.html" title="">License</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="/download/alternatives" title="">Alternative Implementations</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' \n'
b' \n'
b' <li id="documentation" class="tier-1 element-3 " aria-haspopup="true">\n'
b' <a href="/doc/" title="" class="">Documentation</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu" role="menu" aria-hidden="true">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/doc/" title="">Docs</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/doc/av" title="">Audio/Visual Talks</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="https://wiki.python.org/moin/BeginnersGuide" title="">Beginner's Guide</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="https://docs.python.org/devguide/" title="">Developer's Guide</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="https://docs.python.org/faq/" title="">FAQ</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="http://wiki.python.org/moin/Languages" title="">Non-English Docs</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="http://python.org/dev/peps/" title="">PEP Index</a></li>\n'
b' \n'
b' <li class="tier-2 element-8" role="treeitem"><a href="https://wiki.python.org/moin/PythonBooks" title="">Python Books</a></li>\n'
b' \n'
b' <li class="tier-2 element-9" role="treeitem"><a href="/doc/essays/" title="">Python Essays</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' \n'
b' \n'
b' <li id="community" class="tier-1 element-4 " aria-haspopup="true">\n'
b' <a href="/community/" title="" class="">Community</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu" role="menu" aria-hidden="true">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/community/diversity/" title="">Diversity</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/community/lists/" title="">Mailing Lists</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/community/irc/" title="">IRC</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/community/forums/" title="">Forums</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="/community/workshops/" title="">Python Conferences</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="/community/sigs/" title="">Special Interest Groups</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="https://wiki.python.org/moin/" title="">Python Wiki</a></li>\n'
b' \n'
b' <li class="tier-2 element-8" role="treeitem"><a href="/community/logos/" title="">Python Logo</a></li>\n'
b' \n'
b' <li class="tier-2 element-9" role="treeitem"><a href="/community/merchandise/" title="">Merchandise</a></li>\n'
b' \n'
b' <li class="tier-2 element-10" role="treeitem"><a href="/community/awards" title="">Community Awards</a></li>\n'
b' \n'
b' <li class="tier-2 element-11" role="treeitem"><a href="https://www.python.org/psf/codeofconduct/" title="">Code of Conduct</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' \n'
b' \n'
b' <li id="success-stories" class="tier-1 element-5 " aria-haspopup="true">\n'
b' <a href="/about/success/" title="success-stories" class="">Success Stories</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu" role="menu" aria-hidden="true">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/about/success/#arts" title="">Arts</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/about/success/#business" title="">Business</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/about/success/#education" title="">Education</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/about/success/#engineering" title="">Engineering</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="/about/success/#government" title="">Government</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="/about/success/#scientific" title="">Scientific</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="/about/success/#software-development" title="">Software Development</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' \n'
b' \n'
b' <li id="news" class="tier-1 element-6 " aria-haspopup="true">\n'
b' <a href="/blogs/" title="News from around the Python world" class="">News</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu" role="menu" aria-hidden="true">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/blogs/" title="Python Insider Blog Posts">Python News</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="http://planetpython.org/" title="Planet Python">Community News</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="http://pyfound.blogspot.com/" title="PSF Blog">PSF News</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="http://pycon.blogspot.com/" title="PyCon Blog">PyCon News</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' \n'
b' \n'
b' <li id="events" class="tier-1 element-7 " aria-haspopup="true">\n'
b' <a href="/events/" title="" class="">Events</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu" role="menu" aria-hidden="true">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/events/python-events" title="">Python Events</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/events/python-user-group/" title="">User Group Events</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/events/python-events/past/" title="">Python Events Archive</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/events/python-user-group/past/" title="">User Group Events Archive</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event" title="">Submit an Event</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' \n'
b' \n'
b' \n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </nav>\n'
b'\n'
b' <div class="header-banner "> <!-- for optional "do-not-print" class -->\n'
b' \n'
b' <div id="dive-into-python" class="flex-slideshow slideshow">\n'
b'\n'
b' <ul class="launch-shell menu" id="launch-shell">\n'
b' <li>\n'
b' <a class="button prompt" id="start-shell" data-shell-container="#dive-into-python" href="/shell/">>_\n'
b' <span class="message">Launch Interactive Shell</span>\n'
b' </a>\n'
b' </li>\n'
b' </ul>\n'
b'\n'
b' <ul class="slides menu">\n'
b' \n'
b' <li>\n'
b' <div class="slide-code"><pre><code><span class="comment"># Python 3: Fibonacci series up to n</span>\r\n'
b'>>> def fib(n):\r\n'
b'>>> a, b = 0, 1\r\n'
b'>>> while a < n:\r\n'
b">>> print(a, end=' ')\r\n"
b'>>> a, b = b, a+b\r\n'
b'>>> print()\r\n'
b'>>> fib(1000)\r\n'
b'<span class="output">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</span></code></pre></div>\n'
b' <div class="slide-copy"><h1>Functions Defined</h1>\r\n'
b'<p>The core of extensible programming is defining functions. Python allows mandatory and optional arguments, keyword arguments, and even arbitrary argument lists. <a href="//docs.python.org/3/tutorial/controlflow.html#defining-functions">More about defining functions in Python 3</a></p></div>\n'
b' </li>\n'
b' \n'
b' <li>\n'
b' <div class="slide-code"><pre><code><span class="comment"># Python 3: List comprehensions</span>\r\n'
b">>> fruits = ['Banana', 'Apple', 'Lime']\r\n"
b'>>> loud_fruits = [fruit.upper() for fruit in fruits]\r\n'
b'>>> print(loud_fruits)\r\n'
b'<span class="output">[\'BANANA\', \'APPLE\', \'LIME\']</span>\r\n'
b'\r\n'
b'<span class="comment"># List and the enumerate function</span>\r\n'
b'>>> list(enumerate(fruits))\r\n'
b'<span class="output">[(0, \'Banana\'), (1, \'Apple\'), (2, \'Lime\')]</span></code></pre></div>\n'
b' <div class="slide-copy"><h1>Compound Data Types</h1>\r\n'
b'<p>Lists (known as arrays in other languages) are one of the compound data types that Python understands. Lists can be indexed, sliced and manipulated with other built-in functions. <a href="//docs.python.org/3/tutorial/introduction.html#lists">More about lists in Python 3</a></p></div>\n'
b' </li>\n'
b' \n'
b' <li>\n'
b' <div class="slide-code"><pre><code><span class="comment"># Python 3: Simple arithmetic</span>\r\n'
b'>>> 1 / 2\r\n'
b'<span class="output">0.5</span>\r\n'
b'>>> 2 ** 3\r\n'
b'<span class="output">8</span>\r\n'
b'>>> 17 / 3 <span class="comment"># classic division returns a float</span>\r\n'
b'<span class="output">5.666666666666667</span>\r\n'
b'>>> 17 // 3 <span class="comment"># floor division</span>\r\n'
b'<span class="output">5</span></code></pre></div>\n'
b' <div class="slide-copy"><h1>Intuitive Interpretation</h1>\r\n'
b'<p>Calculations are simple with Python, and expression syntax is straightforward: the operators <code>+</code>, <code>-</code>, <code>*</code> and <code>/</code> work as expected; parentheses <code>()</code> can be used for grouping. <a href="http://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator">More about simple math functions in Python 3</a>.</p></div>\n'
b' </li>\n'
b' \n'
b' <li>\n'
b' <div class="slide-code"><pre><code><span class="comment"># Python 3: Simple output (with Unicode)</span>\r\n'
b'>>> print("Hello, I\'m Python!")\r\n'
b'<span class="output">Hello, I\'m Python!</span>\r\n'
b'\r\n'
b'<span class="comment"># Input, assignment</span>\r\n'
b">>> name = input('What is your name?\\n')\r\n"
b">>> print('Hi, %s.' % name)\r\n"
b'<span class="output">What is your name?\r\n'
b'Python\r\n'
b'Hi, Python.</span></code></pre></div>\n'
b' <div class="slide-copy"><h1>Quick & Easy to Learn</h1>\r\n'
b'<p>Experienced programmers in any other language can pick up Python very quickly, and beginners find the clean syntax and indentation structure easy to learn. <a href="//docs.python.org/3/tutorial/">Whet your appetite</a> with our Python 3 overview.</p>\r\n'
b' </div>\n'
b' </li>\n'
b' \n'
b' <li>\n'
b' <div class="slide-code"><pre><code><span class="comment"># For loop on a list</span>\r\n'
b'>>> numbers = [2, 4, 6, 8]\r\n'
b'>>> product = 1\r\n'
b'>>> for number in numbers:\r\n'
b'... product = product * number\r\n'
b'... \r\n'
b">>> print('The product is:', product)\r\n"
b'<span class="output">The product is: 384</span></code></pre></div>\n'
b' <div class="slide-copy"><h1>All the Flow You’d Expect</h1>\r\n'
b'<p>Python knows the usual control flow statements that other languages speak — <code>if</code>, <code>for</code>, <code>while</code> and <code>range</code> — with some of its own twists, of course. <a href="//docs.python.org/3/tutorial/controlflow.html">More control flow tools in Python 3</a></p></div>\n'
b' </li>\n'
b' \n'
b' </ul>\n'
b' </div>\n'
b'\n'
b'\n'
b' </div>\n'
b'\n'
b' \n'
b' <div class="introduction">\n'
b' <p>Python is a programming language that lets you work quickly <span class="breaker"></span>and integrate systems more effectively. <a class="readmore" href="/doc/">Learn More</a></p>\n'
b' </div>\n'
b'\n'
b'\n'
b' </div><!-- end .container -->\n'
b' </header>\n'
b'\n'
b' <div id="content" class="content-wrapper">\n'
b' <!-- Main Content Column -->\n'
b' <div class="container">\n'
b'\n'
b' <section class="main-content " role="main">\n'
b'\n'
b' \n'
b' \n'
b'\n'
b' \n'
b'\n'
b' \n'
b'\n'
b' <div class="row">\n'
b'\n'
b' <div class="small-widget get-started-widget">\n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Get Started</h2>\r\n'
b"<p>Whether you're new to programming or an experienced developer, it's easy to learn and use Python.</p>\r\n"
b'<p><a href="/about/gettingstarted/">Start with our Beginner’s Guide</a></p>\n'
b' </div>\n'
b'\n'
b' <div class="small-widget download-widget">\n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-download"></span>Download</h2>\n'
b'<p>Python source code and installers are available for download for all versions! Not sure which version to use? <a href="https://wiki.python.org/moin/Python2orPython3">Check here</a>.</p>\n'
b'<p>Latest: <a href="/downloads/release/python-362/">Python 3.6.2</a> - <a href="/downloads/release/python-2713/">Python 2.7.13</a></p>\n'
b' </div>\n'
b'\n'
b' <div class="small-widget documentation-widget">\n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-documentation"></span>Docs</h2>\r\n'
b"<p>Documentation for Python's standard library, along with tutorials and guides, are available online.</p>\r\n"
b'<p><a href="https://docs.python.org">docs.python.org</a></p>\n'
b' </div>\n'
b'\n'
b' <div class="small-widget jobs-widget last">\n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-jobs"></span>Jobs</h2>\r\n'
b"<p>Looking for work or have a Python related position that you're trying to hire for? Our <strong>relaunched community-run job board</strong> is the place to go.</p>\r\n"
b'<p><a href="//jobs.python.org">jobs.python.org</a></p>\n'
b' </div>\n'
b'\n'
b' </div>\n'
b'\n'
b' <div class="list-widgets row">\n'
b'\n'
b' <div class="medium-widget blog-widget">\n'
b' \n'
b' <div class="shrubbery">\n'
b' \n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-news"></span>Latest News</h2>\n'
b' <p class="give-me-more"><a href="http://blog.python.org" title="More News">More</a></p>\n'
b' \n'
b' <ul class="menu">\n'
b' \n'
b' \n'
b' <li>\n'
b'<time datetime="2017-08-27T03:41:00.000006+00:00"><span class="say-no-more">2017-</span>08-27</time>\n'
b' <a href="http://feedproxy.google.com/~r/PythonInsider/~3/pe2Ug4MA0Lg/python-2714-release-candidate-1.html">The first release candidate for Python 2.7.14 is now available ...</a></li>\n'
b' \n'
b' <li>\n'
b'<time datetime="2017-08-09T07:34:00.000002+00:00"><span class="say-no-more">2017-</span>08-09</time>\n'
b' <a href="http://feedproxy.google.com/~r/PythonInsider/~3/vY72b719CGk/python-354-and-python-347-are-now.html">Python 3.5.4 and Python 3.4.7 are now available for download. ...</a></li>\n'
b' \n'
b' <li>\n'
b'<time datetime="2017-07-25T08:40:00.000001+00:00"><span class="say-no-more">2017-</span>07-25</time>\n'
b' <a href="http://feedproxy.google.com/~r/PythonInsider/~3/ry7faTWPZiY/python-354rc1-and-python-347rc1-are-now.html">Python 3.5.4rc1 and Python 3.4.7rc1 are now available for download. ...</a></li>\n'
b' \n'
b' <li>\n'
b'<time datetime="2017-07-17T05:43:00+00:00"><span class="say-no-more">2017-</span>07-17</time>\n'
b' <a href="http://feedproxy.google.com/~r/PythonInsider/~3/xgmAIcE1Wes/python-362-is-now-available.html">Python 3.6.2 is now available. Python 3.6.2 is the ...</a></li>\n'
b' \n'
b' <li>\n'
b'<time datetime="2017-07-08T05:15:00.000005+00:00"><span class="say-no-more">2017-</span>07-08</time>\n'
b' <a href="http://feedproxy.google.com/~r/PythonInsider/~3/u1jESRAmGy4/python-362rc2-is-now-available-for.html">Python 3.6.2rc2 is now available. Python 3.6.2rc2 is the ...</a></li>\n'
b' \n'
b' </ul>\n'
b' </div><!-- end .shrubbery -->\n'
b'\n'
b' </div>\n'
b'\n'
b' <div class="medium-widget event-widget last">\n'
b' \n'
b' <div class="shrubbery">\n'
b' \n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-calendar"></span>Upcoming Events</h2>\n'
b' <p class="give-me-more"><a href="/events/calendars/" title="More Events">More</a></p>\n'
b' \n'
b' <ul class="menu">\n'
b' \n'
b' \n'
b' \n'
b' <li>\n'
b'<time datetime="2017-09-07T00:00:00+00:00"><span class="say-no-more">2017-</span>09-07</time>\n'
b' <a href="/events/python-events/577/">PyCon JP 2017</a></li>\n'
b' \n'
b' \n'
b' \n'
b' <li>\n'
b'<time datetime="2017-09-07T00:00:00+00:00"><span class="say-no-more">2017-</span>09-07</time>\n'
b' <a href="/events/python-user-group/546/">Django Girls Caxias do Sul</a></li>\n'
b' \n'
b' \n'
b' \n'
b' <li>\n'
b'<time datetime="2017-09-08T00:00:00+00:00"><span class="say-no-more">2017-</span>09-08</time>\n'
b' <a href="/events/python-events/544/">Python Sul</a></li>\n'
b' \n'
b' \n'
b' \n'
b' <li>\n'
b'<time datetime="2017-09-15T00:00:00+00:00"><span class="say-no-more">2017-</span>09-15</time>\n'
b' <a href="/events/python-events/551/">PyCon Nigeria 2017</a></li>\n'
b' \n'
b' \n'
b' \n'
b' <li>\n'
b'<time datetime="2017-09-19T00:00:00+00:00"><span class="say-no-more">2017-</span>09-19</time>\n'
b' <a href="/events/python-user-group/580/">Django Girls Tucum\xc3\xa1n</a></li>\n'
b' \n'
b' \n'
b' </ul>\n'
b' </div>\n'
b'\n'
b' </div>\n'
b'\n'
b' </div>\n'
b'\n'
b' <div class="row">\n'
b'\n'
b' <div class="medium-widget success-stories-widget">\n'
b' \n'
b'\n'
b'\n'
b'\n'
b' <div class="shrubbery">\n'
b' \n'
b'\n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Success Stories</h2>\n'
b' <p class="give-me-more"><a href="/success-stories/" title="More Success Stories">More</a></p>\n'
b'\n'
b' \n'
b' <div class="success-story-item" id="success-story-2">\n'
b'\n'
b' <blockquote>\n'
b' <a href="/success-stories/industrial-light-magic-runs-python/">ILM runs a batch processing environment capable of modeling, rendering and compositing tens of thousands of motion picture frames per day. Thousands of machines running Linux, IRIX, Compaq Tru64, OS X, Solaris, and Windows join together to provide a production pipeline used by ~800 users daily. Speed of development is key, and Python was a faster way to code (and re-code) the programs that control this production pipeline.</a>\n'
b' </blockquote>\n'
b'\n'
b' <table cellpadding="0" cellspacing="0" border="0" width="100%" class="quote-from">\n'
b' <tbody>\n'
b' <tr>\n'
b' \n'
b' <td><p><a href="/success-stories/industrial-light-magic-runs-python/">Industrial Light & Magic Runs on Python</a> <em>by Tim Fortenberry</em></p></td>\n'
b' </tr>\n'
b' </tbody>\n'
b' </table>\n'
b' </div>\n'
b' \n'
b'\n'
b' </div><!-- end .shrubbery -->\n'
b'\n'
b' </div>\n'
b'\n'
b' <div class="medium-widget applications-widget last">\n'
b' <div class="shrubbery">\n'
b' <h2 class="widget-title"><span aria-hidden="true" class="icon-python"></span>Use Python for…</h2>\r\n'
b'<p class="give-me-more"><a href="/about/apps" title="More Applications">More</a></p>\r\n'
b'\r\n'
b'<ul class="menu">\r\n'
b' <li><b>Web Development</b>:\r\n'
b' <span class="tag-wrapper"><a class="tag" href="http://www.djangoproject.com/">Django</a>, <a class="tag" href="http://www.pylonsproject.org/">Pyramid</a>, <a class="tag" href="http://bottlepy.org">Bottle</a>, <a class="tag" href="http://tornadoweb.org">Tornado</a>, <a href="http://flask.pocoo.org/" class="tag">Flask</a>, <a class="tag" href="http://www.web2py.com/">web2py</a></span></li>\r\n'
b' <li><b>GUI Development</b>:\r\n'
b' <span class="tag-wrapper"><a class="tag" href="http://wiki.python.org/moin/TkInter">tkInter</a>, <a class="tag" href="https://wiki.gnome.org/Projects/PyGObject">PyGObject</a>, <a class="tag" href="http://www.riverbankcomputing.co.uk/software/pyqt/intro">PyQt</a>, <a class="tag" href="https://wiki.qt.io/PySide">PySide</a>, <a class="tag" href="https://kivy.org/">Kivy</a>, <a class="tag" href="http://www.wxpython.org/">wxPython</a></span></li>\r\n'
b' <li><b>Scientific and Numeric</b>:\r\n'
b' <span class="tag-wrapper">\r\n'
b'<a class="tag" href="http://www.scipy.org">SciPy</a>, <a class="tag" href="http://pandas.pydata.org/">Pandas</a>, <a href="http://ipython.org" class="tag">IPython</a></span></li>\r\n'
b' <li><b>Software Development</b>:\r\n'
b' <span class="tag-wrapper"><a class="tag" href="http://buildbot.net/">Buildbot</a>, <a class="tag" href="http://trac.edgewall.org/">Trac</a>, <a class="tag" href="http://roundup.sourceforge.net/">Roundup</a></span></li>\r\n'
b' <li><b>System Administration</b>:\r\n'
b' <span class="tag-wrapper"><a class="tag" href="http://www.ansible.com">Ansible</a>, <a class="tag" href="http://www.saltstack.com">Salt</a>, <a class="tag" href="https://www.openstack.org">OpenStack</a></span></li>\r\n'
b'</ul>\r\n'
b'\n'
b' </div><!-- end .shrubbery -->\n'
b' </div>\n'
b'\n'
b' </div>\n'
b'\n'
b' \n'
b' <div class="pep-widget">\n'
b'\n'
b' <h2 class="widget-title">\n'
b' <span class="prompt">>>></span> <a href="/dev/peps/">Python Enhancement Proposals<span class="say-no-more"> (PEPs)</span></a>: The future of Python<span class="say-no-more"> is discussed here.</span>\n'
b' <a aria-hidden="true" class="rss-link" href="/dev/peps/peps.rss"><span class="icon-feed"></span> RSS</a>\n'
b' </h2>\n'
b'\n'
b'\n'
b' \n'
b' \n'
b' </div>\n'
b'\n'
b' <div class="psf-widget">\n'
b'\n'
b' <div class="python-logo"></div>\n'
b' \n'
b' <h2 class="widget-title">\r\n'
b' <span class="prompt">>>></span> <a href="/psf/">Python Software Foundation</a>\r\n'
b'</h2>\r\n'
b'<p>The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. <a class="readmore" href="/psf/">Learn more</a> </p>\r\n'
b'<p class="click-these">\r\n'
b' <a class="button" href="/users/membership/">Become a Member</a>\r\n'
b' <a class="button" href="/psf/donations/">Donate to the PSF</a>\r\n'
b'</p>\n'
b' </div>\n'
b'\n'
b'\n'
b'\n'
b'\n'
b' </section>\n'
b'\n'
b' \n'
b' \n'
b'\n'
b' \n'
b' \n'
b'\n'
b'\n'
b' </div><!-- end .container -->\n'
b' </div><!-- end #content .content-wrapper -->\n'
b'\n'
b' <!-- Footer and social media list -->\n'
b' <footer id="site-map" class="main-footer" role="contentinfo">\n'
b' <div class="main-footer-links">\n'
b' <div class="container">\n'
b'\n'
b' \n'
b' <a id="back-to-top-1" class="jump-link" href="#python-network"><span aria-hidden="true" class="icon-arrow-up"><span>▲</span></span> Back to Top</a>\n'
b'\n'
b' \n'
b'\n'
b'<ul class="sitemap navigation menu do-not-print" role="tree" id="container">\n'
b' \n'
b' <li class="tier-1 element-1">\n'
b' <a href="/about/" >About</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/about/apps/" title="">Applications</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/about/quotes/" title="">Quotes</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/about/gettingstarted/" title="">Getting Started</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/about/help/" title="">Help</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="http://brochure.getpython.info/" title="">Python Brochure</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' <li class="tier-1 element-2">\n'
b' <a href="/downloads/" >Downloads</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/downloads/" title="">All releases</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/downloads/source/" title="">Source code</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/downloads/windows/" title="">Windows</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/downloads/mac-osx/" title="">Mac OS X</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="/download/other/" title="">Other Platforms</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="https://docs.python.org/3/license.html" title="">License</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="/download/alternatives" title="">Alternative Implementations</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' <li class="tier-1 element-3">\n'
b' <a href="/doc/" >Documentation</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/doc/" title="">Docs</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/doc/av" title="">Audio/Visual Talks</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="https://wiki.python.org/moin/BeginnersGuide" title="">Beginner's Guide</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="https://docs.python.org/devguide/" title="">Developer's Guide</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="https://docs.python.org/faq/" title="">FAQ</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="http://wiki.python.org/moin/Languages" title="">Non-English Docs</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="http://python.org/dev/peps/" title="">PEP Index</a></li>\n'
b' \n'
b' <li class="tier-2 element-8" role="treeitem"><a href="https://wiki.python.org/moin/PythonBooks" title="">Python Books</a></li>\n'
b' \n'
b' <li class="tier-2 element-9" role="treeitem"><a href="/doc/essays/" title="">Python Essays</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' <li class="tier-1 element-4">\n'
b' <a href="/community/" >Community</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/community/diversity/" title="">Diversity</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/community/lists/" title="">Mailing Lists</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/community/irc/" title="">IRC</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/community/forums/" title="">Forums</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="/community/workshops/" title="">Python Conferences</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="/community/sigs/" title="">Special Interest Groups</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="https://wiki.python.org/moin/" title="">Python Wiki</a></li>\n'
b' \n'
b' <li class="tier-2 element-8" role="treeitem"><a href="/community/logos/" title="">Python Logo</a></li>\n'
b' \n'
b' <li class="tier-2 element-9" role="treeitem"><a href="/community/merchandise/" title="">Merchandise</a></li>\n'
b' \n'
b' <li class="tier-2 element-10" role="treeitem"><a href="/community/awards" title="">Community Awards</a></li>\n'
b' \n'
b' <li class="tier-2 element-11" role="treeitem"><a href="https://www.python.org/psf/codeofconduct/" title="">Code of Conduct</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' <li class="tier-1 element-5">\n'
b' <a href="/about/success/" title="success-stories">Success Stories</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/about/success/#arts" title="">Arts</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/about/success/#business" title="">Business</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/about/success/#education" title="">Education</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/about/success/#engineering" title="">Engineering</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="/about/success/#government" title="">Government</a></li>\n'
b' \n'
b' <li class="tier-2 element-6" role="treeitem"><a href="/about/success/#scientific" title="">Scientific</a></li>\n'
b' \n'
b' <li class="tier-2 element-7" role="treeitem"><a href="/about/success/#software-development" title="">Software Development</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' <li class="tier-1 element-6">\n'
b' <a href="/blogs/" title="News from around the Python world">News</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/blogs/" title="Python Insider Blog Posts">Python News</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="http://planetpython.org/" title="Planet Python">Community News</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="http://pyfound.blogspot.com/" title="PSF Blog">PSF News</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="http://pycon.blogspot.com/" title="PyCon Blog">PyCon News</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' <li class="tier-1 element-7">\n'
b' <a href="/events/" >Events</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="/events/python-events" title="">Python Events</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="/events/python-user-group/" title="">User Group Events</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="/events/python-events/past/" title="">Python Events Archive</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/events/python-user-group/past/" title="">User Group Events Archive</a></li>\n'
b' \n'
b' <li class="tier-2 element-5" role="treeitem"><a href="https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event" title="">Submit an Event</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b' <li class="tier-1 element-8">\n'
b' <a href="/dev/" >Contributing</a>\n'
b' \n'
b' \n'
b'\n'
b'<ul class="subnav menu">\n'
b' \n'
b' <li class="tier-2 element-1" role="treeitem"><a href="http://docs.python.org/devguide/" title="">Developer's Guide</a></li>\n'
b' \n'
b' <li class="tier-2 element-2" role="treeitem"><a href="https://bugs.python.org/" title="">Issue Tracker</a></li>\n'
b' \n'
b' <li class="tier-2 element-3" role="treeitem"><a href="https://mail.python.org/mailman/listinfo/python-dev" title="">python-dev list</a></li>\n'
b' \n'
b' <li class="tier-2 element-4" role="treeitem"><a href="/dev/core-mentorship/" title="">Core Mentorship</a></li>\n'
b' \n'
b'</ul>\n'
b'\n'
b' \n'
b' </li>\n'
b' \n'
b'</ul>\n'
b'\n'
b'\n'
b' <a id="back-to-top-2" class="jump-link" href="#python-network"><span aria-hidden="true" class="icon-arrow-up"><span>▲</span></span> Back to Top</a>\n'
b' \n'
b'\n'
b' </div><!-- end .container -->\n'
b' </div> <!-- end .main-footer-links -->\n'
b'\n'
b' <div class="site-base">\n'
b' <div class="container">\n'
b' \n'
b' <ul class="footer-links navigation menu do-not-print" role="tree">\n'
b' <li class="tier-1 element-1"><a href="/about/help/">Help & <span class="say-no-more">General</span> Contact</a></li>\n'
b' <li class="tier-1 element-2"><a href="/community/diversity/">Diversity <span class="say-no-more">Initiatives</span></a></li>\n'
b' <li class="tier-1 element-3"><a href="https://github.com/python/pythondotorg/issues">Submit Website Bug</a></li>\n'
b' <li class="tier-1 element-4">\n'
b' <a href="https://status.python.org/">Status <span class="python-status-indicator-default" id="python-status-indicator"></span></a>\n'
b' </li>\n'
b' </ul>\n'
b'\n'
b' <div class="copyright">\n'
b' <p><small>\n'
b' <span class="pre">Copyright ©2001-2017.</span>\n'
b' <span class="pre"><a href="/psf-landing/">Python Software Foundation</a></span>\n'
b' <span class="pre"><a href="/about/legal/">Legal Statements</a></span>\n'
b' <span class="pre"><a href="/privacy/">Privacy Policy</a></span>\n'
b' </small></p>\n'
b' </div>\n'
b'\n'
b' </div><!-- end .container -->\n'
b' </div><!-- end .site-base -->\n'
b'\n'
b' </footer>\n'
b'\n'
b' </div><!-- end #touchnav-wrapper -->\n'
b'\n'
b' \n'
b' <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>\n'
b' <script>window.jQuery || document.write(\'<script src="/static/js/libs/jquery-1.8.2.min.js"><\\/script>\')</script>\n'
b'\n'
b' <script src="/static/js/libs/masonry.pkgd.min.js"></script>\n'
b'\n'
b' <script type="text/javascript" src="/static/js/main-min.js" charset="utf-8"></script>\n'
b' \n'
b'\n'
b' <!--[if lte IE 7]>\n'
b' <script type="text/javascript" src="/static/js/plugins/IE8-min.js" charset="utf-8"></script>\n'
b' \n'
b' \n'
b' <![endif]-->\n'
b'\n'
b' <!--[if lte IE 8]>\n'
b' <script type="text/javascript" src="/static/js/plugins/getComputedStyle-min.js" charset="utf-8"></script>\n'
b' \n'
b' \n'
b' <![endif]-->\n'
b'\n'
b' \n'
b'\n'
b' \n'
b' \n'
b'\n'
b'</body>\n'
b'</html>\n'
其实closing也是一个经过@contextmanager装饰的generator
@contextmanager
def closing(thing):
try:
yield thing
finally:
thing.close()
from xml.parsers.expat import ParserCreate
class DefaultSaxHandler(object):
def start_element(self, name, attrs):
print('sax:start_element: %s, attrs: %s' % (name, str(attrs)))
def end_element(self, name):
print('sax:end_element: %s' % name)
def char_data(self, text):
print('sax:char_data: %s' % text)
xml = r'''<?xml version="1.0"?>
<ol>
<li><a href="/python">Python</a></li>
<li><a href="/ruby">Ruby</a></li>
</ol>
'''
handler = DefaultSaxHandler()
parser = ParserCreate()
parser.StartElementHandler = handler.start_element
parser.EndElementHandler = handler.end_element
parser.CharacterDataHandler = handler.char_data
parser.Parse(xml)
sax:start_element: ol, attrs: {}
sax:char_data:
sax:char_data:
sax:start_element: li, attrs: {}
sax:start_element: a, attrs: {'href': '/python'}
sax:char_data: Python
sax:end_element: a
sax:end_element: li
sax:char_data:
sax:char_data:
sax:start_element: li, attrs: {}
sax:start_element: a, attrs: {'href': '/ruby'}
sax:char_data: Ruby
sax:end_element: a
sax:end_element: li
sax:char_data:
sax:end_element: ol
1
class test(object):
def __init__(self,name):
self.name = name
def fn(self):
self.age = 5
a = test('lovelyfrog')
a.fn = fn
a.fn(a)
a.age
5
from html.parser import HTMLParser
from html.entities import name2codepoint
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print('<%s>' % tag)
def handle_endtag(self, tag):
print('</%s>' % tag)
def handle_startendtag(self, tag, attrs):
print('<%s/>' % tag)
def handle_data(self, data):
print(data)
def handle_comment(self, data):
print('<!--', data, '-->')
def handle_entityref(self, name):
print('&%s;' % name)
def handle_charref(self, name):
print('&#%s;' % name)
parser = MyHTMLParser()
parser.feed('''<html>
<head></head>
<body>
<!-- test html parser -->
<p>Some <a href=\"#\">html</a> HTML tutorial...<br>END</p>
</body></html>''')
<html>
<head>
</head>
<body>
<!-- test html parser -->
<p>
Some
<a>
html
</a>
HTML tutorial...
<br>
END
</p>
</body>
</html>
# -*- coding: utf-8 -*-
#经验1:数组下标取值前一定要判断长度
#经验2:子类在重载构造函数时不要忘了调用父类的构造函数
#经验3:HTMLParser的attrs是一个元素为tuple的list
#经验4:调用类属性时一定要加self,否则相当于申明了新的临时变量
from html.parser import HTMLParser
from html.entities import name2codepoint
from urllib import request
class MyHTMLParser(HTMLParser):
def __init__(self):
super().__init__()
self._event_title = []
self._event_location = []
self._ecent_time = []
self._reading_title = False
self._reading_time = False
self._reading_location = False
#解析头标签
def handle_starttag(self, tag, attrs):
if tag == 'time':
self._reading_time = True
if len(attrs) >= 1:
if tag == 'h3' and attrs[0][1] == 'event-title':
self._reading_title = True
if tag == 'span' and attrs[0][1] == 'event-location':
self._reading_location = True
#解析内容
def handle_data(self, data):
if self._reading_title:
self._event_title.append(data)
self._reading_title = False
if self._reading_time:
self._ecent_time.append(data)
self._reading_time = False
if self._reading_location:
self._event_location.append(data)
self._reading_location = False
@property
def data(self):
self._data = []
for i in range(len(self._event_title)):
dic = {}
dic["title"] = self._event_title[i]
dic["time"] = self._ecent_time[i]
dic["location"] = self._event_location[i]
self._data.append(dic)
return self._data
def getHtml():
with request.urlopen('https://www.python.org/events/python-events/') as f:
data = f.read().decode('utf-8')
return data
parser = MyHTMLParser()
parser.feed(getHtml())
for item in parser.data:
print(str(item))
{'title': 'PyCon JP 2017', 'location': 'Waseda University, Nishi-Waseda Campus, Building 63, Tokyo, Japan', 'time': '07 Sept. – 11 Sept. '}
{'title': 'Python Sul', 'location': 'Universidade de Caxias do Sul (UCS), Caxias do Sul, Rio Grande do Sul, Brazil', 'time': '08 Sept. – 11 Sept. '}
{'title': 'PyCon Nigeria 2017', 'location': 'Lagos, Nigeria', 'time': '15 Sept. – 17 Sept. '}
{'title': 'PyCon FR 2017', 'location': 'Toulouse, France', 'time': '21 Sept. – 25 Sept. '}
{'title': 'PyConES17', 'location': 'Caceres, Spain', 'time': '22 Sept. – 25 Sept. '}
{'title': 'PyConKE 2017', 'location': 'United States International University, Nairobi, Kenya', 'time': '28 Sept. – 30 Sept. '}
{'title': 'PyData Delhi 2017', 'location': 'New Delhi, India', 'time': '02 Sept. – 04 Sept. '}
{'title': 'EuroSciPy 2017', 'location': 'Erlangen, Germany', 'time': '28 Aug. – 02 Sept. '}
urllib
urllib的request模块可以非常方便地抓取URL内容,也就是发送一个GET请求到指定的页面,然后返回HTTP的响应:
from urllib import request
with request.urlopen('https://api.douban.com/v2/book/2129650') as f:
data = f.read()
print('Status:', f.status, f.reason)
for k, v in f.getheaders():
print('%s: %s' % (k, v))
print('Data:', data.decode('utf-8'))
Status: 200 OK
Date: Tue, 05 Sep 2017 02:40:34 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2058
Connection: close
Vary: Accept-Encoding
X-Ratelimit-Remaining2: 99
X-Ratelimit-Limit2: 100
Expires: Sun, 1 Jan 2006 01:00:00 GMT
Pragma: no-cache
Cache-Control: must-revalidate, no-cache, private
Set-Cookie: bid=DDvtiS0mipo; Expires=Wed, 05-Sep-18 02:40:34 GMT; Domain=.douban.com; Path=/
X-DOUBAN-NEWBID: DDvtiS0mipo
X-DAE-Node: sindar10c
X-DAE-App: book
Server: dae
Data: {"rating":{"max":10,"numRaters":16,"average":"7.4","min":0},"subtitle":"","author":["廖雪峰"],"pubdate":"2007","tags":[{"count":21,"name":"spring","title":"spring"},{"count":13,"name":"Java","title":"Java"},{"count":6,"name":"javaee","title":"javaee"},{"count":5,"name":"j2ee","title":"j2ee"},{"count":4,"name":"计算机","title":"计算机"},{"count":3,"name":"藏书","title":"藏书"},{"count":3,"name":"编程","title":"编程"},{"count":3,"name":"POJO","title":"POJO"}],"origin_title":"","image":"https://img3.doubanio.com\/mpic\/s2552283.jpg","binding":"平装","translator":[],"catalog":"","pages":"509","images":{"small":"https://img3.doubanio.com\/spic\/s2552283.jpg","large":"https://img3.doubanio.com\/lpic\/s2552283.jpg","medium":"https://img3.doubanio.com\/mpic\/s2552283.jpg"},"alt":"https:\/\/book.douban.com\/subject\/2129650\/","id":"2129650","publisher":"电子工业出版社","isbn10":"7121042622","isbn13":"9787121042621","title":"Spring 2.0核心技术与最佳实践","url":"https:\/\/api.douban.com\/v2\/book\/2129650","alt_title":"","author_intro":"","summary":"本书注重实践而又深入理论,由浅入深且详细介绍了Spring 2.0框架的几乎全部的内容,并重点突出2.0版本的新特性。本书将为读者展示如何应用Spring 2.0框架创建灵活高效的JavaEE应用,并提供了一个真正可直接部署的完整的Web应用程序——Live在线书店(http:\/\/www.livebookstore.net)。\n在介绍Spring框架的同时,本书还介绍了与Spring相关的大量第三方框架,涉及领域全面,实用性强。本书另一大特色是实用性强,易于上手,以实际项目为出发点,介绍项目开发中应遵循的最佳开发模式。\n本书还介绍了大量实践性极强的例子,并给出了完整的配置步骤,几乎覆盖了Spring 2.0版本的新特性。\n本书适合有一定Java基础的读者,对JavaEE开发人员特别有帮助。本书既可以作为Spring 2.0的学习指南,也可以作为实际项目开发的参考手册。","price":"59.8"}
如果我们要想模拟浏览器发送GET请求,就需要使用Request对象,通过往Request对象添加HTTP头,我们就可以把请求伪装成浏览器
如果要以POST发送一个请求,只需要把参数data以bytes形式传入。
我们模拟一个微博登录,先读取登录的邮箱和口令,然后按照weibo.cn的登录页的格式以username=xxx&password=xxx的编码传入:
from urllib import request, parse
print('Login to weibo.cn...')
email = input('Email: ')
passwd = input('Password: ')
login_data = parse.urlencode([
('username', email),
('password', passwd),
('entry', 'mweibo'),
('client_id', ''),
('savestate', '1'),
('ec', ''),
# ('pagerefer', 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F')
])
req = request.Request('https://login.sina.com.cn/signup/signin.php')
req.add_header('Origin', 'https://passport.weibo.com')
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
# req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')
with request.urlopen(req, data=login_data.encode('utf-8')) as f:
print('Status:', f.status, f.reason)
for k, v in f.getheaders():
print('%s: %s' % (k, v))
print('Data:', f.read().decode('utf-8'))
Login to weibo.cn...