29
Python Introduction For Novice huntzhan ()

Python Introduction For Novice

Embed Size (px)

Citation preview

Page 1: Python Introduction For Novice

Python Introduction For Novice

huntzhan ( )

Page 2: Python Introduction For Novice

Topic

• Background

• Core Concepts

• Roadmap To Learn Python

Page 3: Python Introduction For Novice

Background

• The Brief History

• Pros And Cons

Page 4: Python Introduction For Novice

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]

Page 5: Python Introduction For Novice

Pros And Cons

• Pros:

• easy to learn an code

• readability

• lost of third-party libraries (ecosystem)

Page 6: Python Introduction For Novice

Pros And Cons

• Cons:

• Speed is a problem (10x ~ 100x slower than C++ [2])

• Static program analysis is hard

Page 7: Python Introduction For Novice

Core Concepts

• Binary vs. Unicode

• Simplified Object Model

Page 8: Python Introduction For Novice

Binary vs. Unicode• Unicode

• character

• code point

• Binary Representation

• encoding

Page 9: Python Introduction For Novice

Example: zh_CN

• character:

• code point: U+4E2D (0x4E2D, see [4])

• encoding (UTF-8): E4 B8 AD

Page 10: Python Introduction For Novice

Example: emoji

• character: 😀

• code point: U+1F600 (0x1F600, see [3])

• encoding (UTF-8): F0 9F 98 80

Page 11: Python Introduction For Novice

py2, str/unicode

• str: binary data, ' '

• unicode: represent unicode characters, u' '

Page 12: Python Introduction For Novice

py3, bytes/str

• bytes: binary data, b' '

• str: represent unicode characters, ' '

Page 13: Python Introduction For Novice

str/unicode conversion

• str —decode—> unicode

• unicode —encode—> str

Page 14: Python Introduction For Novice

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!

Page 15: Python Introduction For Novice

Source Code Encoding

• Place # -*- coding: <encoding name> -*- at the top of source file.

Page 16: Python Introduction For Novice

stdin/stdout/stderr Encoding

• export PYTHONIOENCODING=en_US.UTF-8

• Python “sends” binary data to terminal.

• print unicode: encode first before printing.

Page 17: Python Introduction For Novice

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]

Page 18: Python Introduction For Novice

Simplified Object Model

• Dynamic typing.

• Everything is an object.

Page 19: Python Introduction For Novice

Dynamic typing

Page 20: Python Introduction For Novice

Everything is an object

• have first-class functions

• every class is subclass of <type ‘object’>, except <type ‘object'> itself.

• class/function/literal…

Page 21: Python Introduction For Novice

Roadmap To Learn

• Skill Acquisition

• 2 Or 3 ?

• Ecosystem

• Popular Packages

Page 22: Python Introduction For Novice

Skill Acquisition• 0

• Beginner, 1 - 2

• Intermediate, 3 - 4

• Advanced, 5 - 6

• Expert, 7 - 8

• Mastery, 9 - 10

Page 23: Python Introduction For Novice

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…)

Page 24: Python Introduction For Novice

2 Or 3

• Version 2.0, 2000; Version 3.0, 2008

• Python 3 is the future, Python 2 is legacy.

Page 25: Python Introduction For Novice

Ecosystem

• PyPI: Python Package Index [10]

• pip: tool for installing Python packages

Page 26: Python Introduction For Novice

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]

Page 27: Python Introduction For Novice

Roadmap To Learn Python• Beginner:

• The Python Tutorial [14]

• Intermediate:

• Python Cookbook [15]

• Language Reference [16]

• Keep pushing…

Page 28: Python Introduction For Novice

Thank you

Page 29: Python Introduction For Novice

• [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/