Upload
haoxun-zhan
View
176
Download
4
Embed Size (px)
Citation preview
Python Introduction For Novice
huntzhan ( )
Topic
• Background
• Core Concepts
• Roadmap To Learn Python
Background
• The Brief History
• Pros And Cons
The Brief History
• Version 1.0, 1994
• Version 2.0, 2000; Version 3.0, 2008
• Dynamic type, reference count GC
• “There should be one-- and preferably only one --obvious way to do it.”[1]
Pros And Cons
• Pros:
• easy to learn an code
• readability
• lost of third-party libraries (ecosystem)
Pros And Cons
• Cons:
• Speed is a problem (10x ~ 100x slower than C++ [2])
• Static program analysis is hard
Core Concepts
• Binary vs. Unicode
• Simplified Object Model
Binary vs. Unicode• Unicode
• character
• code point
• Binary Representation
• encoding
Example: zh_CN
• character:
• code point: U+4E2D (0x4E2D, see [4])
• encoding (UTF-8): E4 B8 AD
Example: emoji
• character: 😀
• code point: U+1F600 (0x1F600, see [3])
• encoding (UTF-8): F0 9F 98 80
py2, str/unicode
• str: binary data, ' '
• unicode: represent unicode characters, u' '
py3, bytes/str
• bytes: binary data, b' '
• str: represent unicode characters, ' '
str/unicode conversion
• str —decode—> unicode
• unicode —encode—> str
Encoding
• Source Code [5]: # -*- coding: <encoding name> -*-
• stdin/stdout/stderr [6][7]: PYTHONIOENCODING
• py2 default: ASCII; py3 default: UTF-8
• In favour of UTF-8!
Source Code Encoding
• Place # -*- coding: <encoding name> -*- at the top of source file.
stdin/stdout/stderr Encoding
• export PYTHONIOENCODING=en_US.UTF-8
• Python “sends” binary data to terminal.
• print unicode: encode first before printing.
Prefer Header for Python2
• LINE 1: declare source code encoding as UTF-8
• LINE 2 - 4: treat string as unicode, see [8]
• LINE 5 - 7: deal with 2/3 compatible issue, see [9]
Simplified Object Model
• Dynamic typing.
• Everything is an object.
Dynamic typing
Everything is an object
• have first-class functions
• every class is subclass of <type ‘object’>, except <type ‘object'> itself.
• class/function/literal…
Roadmap To Learn
• Skill Acquisition
• 2 Or 3 ?
• Ecosystem
• Popular Packages
Skill Acquisition• 0
• Beginner, 1 - 2
• Intermediate, 3 - 4
• Advanced, 5 - 6
• Expert, 7 - 8
• Mastery, 9 - 10
Beginner basic syntax (tutorial)
Intermediate language reference, object model, PEPs, standard libraries, ecosystem, DevOps, philosophy, development experience
Advanced C API, CPython implementation, bytecode execution, optimization technique, business solution
Expert publish a book (em…)
Mastery leading the community (em…)
2 Or 3
• Version 2.0, 2000; Version 3.0, 2008
• Python 3 is the future, Python 2 is legacy.
Ecosystem
• PyPI: Python Package Index [10]
• pip: tool for installing Python packages
Popular Packages
• Google “xxx python”
• Anaconda: “over 100 pre-built and tested scientific and analytic Python packages that include NumPy, Pandas, SciPy, Matplotlib, and IPython” [12]
• awesome-python [13]
Roadmap To Learn Python• Beginner:
• The Python Tutorial [14]
• Intermediate:
• Python Cookbook [15]
• Language Reference [16]
• Keep pushing…
Thank you
• [1]: https://www.python.org/dev/peps/pep-0020/
• [2]: https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=python3&lang2=gpp
• [3]: http://apps.timwhitlock.info/unicode/inspect?s=%F0%9F%98%80
• [4]: http://apps.timwhitlock.info/unicode/inspect?s=%E4%B8%AD
• [5]: https://www.python.org/dev/peps/pep-0263/
• [6]: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONIOENCODING
• [7]: http://stackoverflow.com/questions/2596714/why-does-python-print-unicode-characters-when-the-default-encoding-is-ascii
• [8]: https://docs.python.org/2/library/__future__.html
• [9]: http://python-future.org/
• [10]: https://pypi.python.org/
• [11]: https://pip.pypa.io/
• [12]: https://docs.continuum.io/anaconda/pkg-docs
• [13]: https://github.com/vinta/awesome-python
• [14]: https://docs.python.org/2/tutorial/
• [15]: http://chimera.labs.oreilly.com/books/1230000000393
• [16]: https://docs.python.org/2/reference/