Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python introduction

982 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Python introduction

  1. 1. Introduction Python Roger Xia Aug 2013
  2. 2. 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/
  3. 3. Zen
  4. 4. August 2013
  5. 5. 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
  6. 6. 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)
  7. 7. 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
  8. 8. • data analysis • machine learning • Big data : R: http://www.xmind.net/m/LKF2/
  9. 9. Python has a simple, minimal, clean syntax
  10. 10. find the roots of a quadratic equation
  11. 11. HTTP Requests
  12. 12. 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
  13. 13. 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)
  14. 14. 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?
  15. 15. Built-in types • None • bool : True/False • int/long/float • str/unicode (u’Spam’) • tuple • list • dict • lambda expression
  16. 16. Dictionary • defines one-to-one relationships between keys and values /定义了键和值之间一对一的 关系
  17. 17. List
  18. 18. Using lists as stacks/queues
  19. 19. List comprehensions creating a list based on existing lists Nested List comprehensions Built in function
  20. 20. Tuple • A tuple is an immutable list. A tuple can not be changed in any way once it is created.
  21. 21. 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]
  22. 22. Lambda Functions anonymous functions in python Type help() for help documents Python 支持一种有趣的语法,它允许你快速定义单行的最小函数。这些叫做 lambda 的函数,是从 Lisp 借用来的,可以用在任何需要函数的地方。
  23. 23. >>> 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
  24. 24. others • Regular expressions: – http://www.tutorialspoint.com/python/python_re g_expressions.htm • Exceptions: – http://docs.python.org/2/tutorial/errors.html
  25. 25. 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.
  26. 26. 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
  27. 27. 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
  28. 28. • >>> 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 • >>>
  29. 29. MongoDB operation mongodb PyMongo mongo-java-driver python java
  30. 30. 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
  31. 31. pymongo
  32. 32. Thank you! Questions?

×