• Save
Python introduction
Upcoming SlideShare
Loading in...5
×
 

Python introduction

on

  • 738 views

 

Statistics

Views

Total Views
738
Views on SlideShare
738
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Code is 2-10x shorter than C, C++, JavaCode is extremely readableLanguage is very easy to learn
  • http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
  • http://stackoverflow.com/questions/12267544/machine-translation-using-babelize-shell-in-nltk
  • http://bitworking.org/news/Why_so_many_Python_web_frameworksR(又称R语言)是一款开源的跨平台的数值统计和数值图形化展现工具。通俗点说,R是用来做统计和画图的。R拥有自己的脚本语言和大量的统计、图形库(得益于开源社区),这让她看起来既美又实用。http://www.r-bloggers.com/stepping-up-to-big-data-with-r-and-python-a-mind-map-of-all-the-packages-you-will-ever-need/http://it-ebooks.info/book/1041/https://speakerdeck.com/pyconslides/awesome-big-data-algorithms-by-titus-brownhttps://speakerdeck.com/pyconslides/building-an-image-processing-pipeline-with-python-by-franck-chastagnol
  • http://www.xmind.net/m/WvfC/
  • http://folk.uio.no/hpl/WhyPython.pdf
  • Pyrex : http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/http://www.objectsbydesign.com/projects/python_links.html
  • Add C:\Python27 to windows path to run python under cmd
  • http://docs.python.org/2/library/types.html
  • Using lists as stacks/queues
  • It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends.
  • It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends.
  • Tuple 比 list 操作速度快。如果您定义了一个值的常量集,并且唯一要用它做的是不断地遍历它,请使用 tuple 代替 list。如果对不需要修改的数据进行 “写保护”,可以使代码更安全。Tuples 可以在 dictionary 中被用做 key,但是 list 不行。Dictionary key 必须是不可变的。Tuple 本身是不可改变的,但是如果您有一个 list 的 tuple,那就认为是可变的了,用做 dictionary key 就是不安全的。只有字符串、整数或其它对 dictionary 安全的 tuple 才可以用作 dictionary key。
  • all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names.
  • map(...) map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).reduce(...) reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.
  • http://docs.python.org/2/library/functions.html
  • http://docs.python.org/2/tutorial/modules.htmlIf you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module
  • The csv module implements classes to read and write tabular data in CSV format.http://docs.python.org/2/library/csv.html
  • http://docs.mongodb.org/ecosystem/drivers/python/
  • http://api.mongodb.org/python/current/installation.html
  • http://api.mongodb.org/python/current/tutorial.htmlhttp://api.mongodb.org/python/current/api/index.htmlhttps://education.10gen.com/courses/10gen/M101P/2013_September/about

Python introduction Python introduction Presentation Transcript

  • Introduction Python Roger Xia Aug 2013
  • What is A dynamic object-oriented programming language. Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs. – http://www.python.org/
  • Zen
  • August 2013
  • Who is using Python? spider and search engine Yahoo Maps, Yahoo Groups Python Success Stories – http://www.python.org/about/success/ – Star Wars! : http://www.youtube.com/watch?v=RqhUz2vh6lA – http://lineofthought.com/tools/python
  • My experience on Python? • 2007-2010 – Web Automation testing : MaxQ – Crawler web news for vertical search : beautifulsoup, lxml, mechanize • 2011 – Text/file processing – hadoop? • 2013 – scrapy (twisted) vs. gcrawler (gevent)
  • What Python can do? • Xml processing • Web Application • Off-line computation • Operation scripts – puppet/chef(ruby) – salt(python) • NLP Processing – has strong numeric processing capability : matrix operations, etc – Suitable for probability and machine learning code. – NLTK : nature language tool kit
  • • data analysis • machine learning • Big data : R: http://www.xmind.net/m/LKF2/
  • Python has a simple, minimal, clean syntax
  • find the roots of a quadratic equation
  • HTTP Requests
  • Easy to get started! • http://www.python.org/doc/ • <<Dive into Python>> : http://www.diveintopython.net/toc/index.html • Python standard libraries: http://docs.python.org/2/library/index.html • Google • PyPI : http://pypi.python.org/pypi – There are currently 33961 packages • PyCon : http://www.pycon.org/ • Practice, practice, practice
  • Getting started and Installation • Windows : find the install package here http://www.python.org/download/releases/2.7.5/ • Linux : Generally, python come installed with the operating system, if not, try – Centos/redhat : yum install python – Ubuntu : sudo apt-get install python2.7 – wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz (./configure & make & make install)
  • Using the python interpreter • Indentation/缩进 • # • “”” (doc string) • Variables are created when they are assigned. The name is case sensitive. • If __name__==“__main__”: – __name__ is a built-in variable which evaluate to the name of the current module. – being run directly or being imported?
  • Built-in types • None • bool : True/False • int/long/float • str/unicode (u’Spam’) • tuple • list • dict • lambda expression
  • Dictionary • defines one-to-one relationships between keys and values /定义了键和值之间一对一的 关系
  • List
  • Using lists as stacks/queues
  • List comprehensions creating a list based on existing lists Nested List comprehensions Built in function
  • Tuple • A tuple is an immutable list. A tuple can not be changed in any way once it is created.
  • Defining Functions >>> def fib(start=0, n=2000): ... "Print a Fibonacci series up to n, start from start" ... result = [] ... a, b = start, start+1 ... while b < n: ... result.append(b) ... a, b = b, a+b ... return result ... >>> f1 = fib(5) >>> f1 [6, 11, 17, 28, 45, 73, 118, 191, 309, 500, 809, 1309] >>> f2 = fib(0, 1000) >>> f2 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
  • Lambda Functions anonymous functions in python Type help() for help documents Python 支持一种有趣的语法,它允许你快速定义单行的最小函数。这些叫做 lambda 的函数,是从 Lisp 借用来的,可以用在任何需要函数的地方。
  • >>> a = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i in range(len(a)): ... print i, a[i] ... 0 Mary 1 had 2 a 3 little 4 lamb
  • others • Regular expressions: – http://www.tutorialspoint.com/python/python_re g_expressions.htm • Exceptions: – http://docs.python.org/2/tutorial/errors.html
  • Modules • A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name is available as the value of the global variable __name__. – A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. – Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. – When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path.
  • Packages • Packages are a way of structuring Python's module namespace by using "dotted module names". • The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name
  • What can you do with excel? • 1. read/write to normal csv file • 2. use csv module to do it • 3. pypi search for excel – http://www.simplistix.co.uk/presentations/python -excel.pdf
  • • >>> import csv • >>> with open('D:/eggs.csv', 'wb') as csvfile: • ... spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=cs • v.QUOTE_MINIMAL) • ... spamwriter.writerow(['Spam'] * 5 + ['Baked Beans']) • ... spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) • ... • >>> with open('D:/eggs.csv', 'rb') as csvfile: • ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') • ... for row in spamreader: • ... print ', '.join(row) • ... • Spam, Spam, Spam, Spam, Spam, Baked Beans • Spam, Lovely Spam, Wonderful Spam • >>>
  • MongoDB operation mongodb PyMongo mongo-java-driver python java
  • Install pymongo module • How to install a third-party python module? – easy_install pymongo • Add C:Python27Scripts to windows environment path variables – pip install pymongo • wget https://pypi.python.org/packages/source/s/setuptools/setuptools- 1.1.tar.gz • python setup.py install • wget https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz • python setup.py install – Installing from source • $ git clone git://github.com/mongodb/mongo-python-driver.git pymongo • $ cd pymongo/ • $ python setup.py install
  • pymongo
  • Thank you! Questions?