Python Notes for Engineers
-Engineers need to use computers to deliver solutions of data processing,
-numerical analysis, simulations, digital control, etc. Python as a generic
-programming language is being widely used by engineers for these technical
-computing problems. The simplicity of the programming language reduces the
-efforts of software development and maintenance. The abundant standard and
-third-party libraries provide tremendous capabilities to various applications.
-Famous scientific Python packages include `NumPy <http://numpy.scipy.org/>`_,
-`SciPy <http://www.scipy.org/>`_, `matplotlib
-<http://matplotlib.sourceforge.net/>`_, `IPython <http://ipython.org/>`_, `Sage
-<http://www.sagemath.org/>`_, `NLTK <http://www.nltk.org/>`_, `SimPy
-<http://simpy.sourceforge.net/>`_, to name but a few.
+The skill to program digital computers is important for modern engineers.
+Although pencils, scratch pads, and calculator are still important, Engineers
+now routinely use computers to process data, perform numerical analysis and
+simulations, and control devices. We can't do much without a programming
+language, and we should choose one to maximize our productivity. In this
+document, we are going to show that Python_ is such a good choice, and how to
+use it to solve technical problems.
+.. _Python: http://python.org/
This project is intended to provide introductory information about Python for
technical computing. It includes a set of documents and the corresponding code
http://pyengr.readthedocs.org/en/latest/. The project is licensed under `GNU
+The programming language Python was `first made public in 1991
+<http://en.wikipedia.org/wiki/History_of_Python>`__. Python is a
+multi-paradigm and batteries-included programming language. It supports
+imperative, structural, object-oriented, and functional programming. It
+contains a wide spectrum of standard libraries, and has more than 10,000
+3rd-party packages available online. The flexibility in programming paradigms
+allows the users to attack a problem with a suitable approach. The versatility
+of libraries further enriches our armament. Moreover, Python allows
+straight-forward extension to its core implementation via the C API. The
+interpreter itself can be easily incorporated into another host system.
+Regarding problem-solving, Python is much more than a programming language.
+It's more like an extensible runtime environment with rich programmability.
+Python_ is an interpreted language with a strong and dynamic typing system. In
+most Unix-based computers, Python is pre-installed and one can enter its
+interactive mode in a terminal::
+ Python 2.7.3rc2 (default, Apr 22 2012, 22:30:17)
+ Type "help", "copyright", "credits" or "license" for more information.
+ >>> sys.stdout.write('%g\n' % math.pi)
+ >>> sys.stdout.write('%g\n' % math.cos(45./180.*math.pi))
+Indeed Python is both powerful and easy-to-use. But what makes Python great
+for technical applications is the embedded mentality that resonates with
+engineering and scientific discipline. See `The Zen of Python (Python
+Enhancement Proposal (PEP) 20) <http://www.python.org/dev/peps/pep-0020/>`__:
+ $ python -c 'import this'
+ The Zen of Python, by Tim Peters
+ Beautiful is better than ugly.
+ Explicit is better than implicit.
+ Simple is better than complex.
+ Complex is better than complicated.
+ Flat is better than nested.
+ Sparse is better than dense.
+ Special cases aren't special enough to break the rules.
+ Although practicality beats purity.
+ Errors should never pass silently.
+ Unless explicitly silenced.
+ In the face of ambiguity, refuse the temptation to guess.
+ There should be one-- and preferably only one --obvious way to do it.
+ Although that way may not be obvious at first unless you're Dutch.
+ Now is better than never.
+ Although never is often better than *right* now.
+ If the implementation is hard to explain, it's a bad idea.
+ If the implementation is easy to explain, it may be a good idea.
+ Namespaces are one honking great idea -- let's do more of those!
+These proverbs are the general guidelines for Python programmers. It promotes
+several points favorable for engineers and scientists:
+- Simplicity. Engineers and scientists want `Occam's razor
+ <http://en.wikipedia.org/wiki/Occam's_razor>`_. Simplification is our job.
+ We know a trustworthy solution is usually simple and beautiful.
+- Disambiguation. Although expressions can differ, facts are facts.
+ Uncertainty is acceptable, but anything true should never be taken as false,
+- Practicality. Given infinite amount of time, anything can be done.
+ Especially for engineers, constraints are needed to deliver valuable products
+- Collaboration. Modern engineering and science face challenges that are too
+ complex for one person to handle. We always need to work as a team.
+The more I write Python, the more I feel my code echoes my engineering
+practice. Although there are many good programming environments, and some can
+be more convenient than Python in specific areas, only Python and its community
+have a value system so close to the training I received as a computational
+The Zen of Python is very insightful to programming Python. Breaking the Zen
+means not writing "`Pythonic
+<http://faassen.n--tree.net/blog/view/weblog/2005/08/06/0>`__" code. Python
+community has a tendency to find conventions for solving similar problems and
+encourages programmers to use the idiomatic Python. For example, when
+converting a sequence of data, it is encouraged to use a `list comprehension
+ # it is concise and clear if you know what's a list comprehension.
+ values = [float(tok) for tok in line.split()]
+ # it works, but is not idiomatic to Python, i.e., not "Pythonic".
+ for tok in line.split():
+But it doesn't mean using list comprehensions is always Pythonic. Consider a
+ lines = ['1 2 3\n', '4 5 6\n']
+ # nested list comprehensions are not easy to understand.
+ values = [float(tok) for line in lines for tok in line.split()]
+ # so a loop now looks much more concise.
+ values.extend(float(tok) for tok in line.split()) # we use generator.
+Don't worry if you haven't familiarize yourself with the above syntax.
+Learning new things takes time. The point here is Python provides clear
+directions to how things should be done. That doesn't mean Python is
+inflexible. Indeed, the coding style of Python isn't as free as that of Lisp,
+Perl, or Ruby, but Python finds a good balance between freedom and discipline
+in coding, so you can find Python providing maintainable code in nearly every
+Python has abundant standard and third-party libraries that provide a lot of
+capabilities to various applications. Famous scientific Python packages
+include `NumPy <http://numpy.scipy.org/>`_, `SciPy <http://www.scipy.org/>`_,
+`matplotlib <http://matplotlib.sourceforge.net/>`_, `IPython
+<http://ipython.org/>`_, `Sage <http://www.sagemath.org/>`_, `NLTK
+<http://www.nltk.org/>`_, `SimPy <http://simpy.sourceforge.net/>`_, to name but