{ "metadata": { "name": "Chapter1_Introduction" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", "==========================\n", "First edition\n", "-----------------------------------\n", "\n", "Chapter 1\n", "==========\n", "__________\n", "\n", "Introduction\n", "----------\n", "[Python](http://www.python.org) is a Very High Level Language, object-oriented, dynamic and with strong typing, interpreted and interactive.\n", "\n", "Features\n", "---------------\n", "Python has a clear and concise syntax, which favors the readability of source code, and makes the language more productive.\n", "\n", "The language includes several high-level structures (lists, dictionaries, date / time, complex numbers and others) and a vast collection of modules ready for use, plus third-party frameworks that can be added. It also has features found in other modern languages, such as generators, introspection, persistence, metaclasses and unity tests. Multiparadigm, the language supports modular, functional, and object-oriented programming. Even the basic types in Python are objects. The language is interpreted through bytecode by the Python virtual machine, making the code portable. This makes it possible to build applications on one platform and run them on other systems or direct from the source.\n", "\n", "Python is open source software (with license compatible with the *General Public License (GPL)*, but less restrictive, allowing Python to be even incorporated into proprietary products). The language specification is maintained by the [Python Software Foundation](http://www.python.org/psf/) (PSF).\n", "\n", "Besides being used as the main language in the development of systems, Python is also used as a *scripting* language in various pieces of software, enabling you to automate tasks and add new features, among them: LibreOffice.org, PostgreSQL, Blender, GIMP and Inkscape.\n", "\n", "It is possible to integrate Python with other languages such as C and Fortran. In general terms, the language has many similarities with other dynamic languages such as Perl and Ruby.\n", "\n", "History\n", "---------\n", "The language was created in 1990 by Guido van Rossum, the National Research Institute for Mathematics and Computer Science in the Netherlands (CWI) and had originally focused on users as physicists and engineers. Python was designed from another existing language at the time, called ABC.\n", "\n", "Today, the language is well accepted in the industry for high-tech companies, such as:\n", "\n", "+ Google (Web applications).\n", "+ Yahoo (Web applications).\n", "+ Microsoft (IronPython: Python for. NET)\n", "+ Nokia (available for recent lines of cell phones and PDAs).\n", "+ Disney (3D animations).\n", "\n", "Versions\n", "-------\n", "The official implementation of Python is maintained by the PSF and written in C, and therefore is also known as CPython. The latest stable version is available for download at:\n", "\n", "[http://www.python.org/download/](http://www.python.org/download/)\n", "\n", "For Windows platforms, simply run the installer. For other platforms, such as Linux, Python is usually already part of the system, but in some cases it may be necessary to compile and install the interpreter from the source files.\n", "\n", "There are also implementations of Python for. NET (IronPython), JVM (Jython) and Python (PyPy).\n", "\n", "Running programs\n", "--------------------\n", "\n", "Example of Python program:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# the character \"#\" indicates that the rest of the line is a comment\n", "# A list of musical instruments\n", "instruments = ['Bass', 'Drums', 'Guitar']\n", "\n", "# for each name in the list of instruments\n", "for instrument in instruments:\n", " # show the name of the musical instrument\n", " print instrument" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Bass\n", "Drums\n", "Guitar\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the example, `instruments` is a list containing the items \"Bass\", \"Drums\" and \"Guitar\". Now `instrument` is a name that corresponds to each of the items on the list, as the loop is executed.\n", "\n", "The source files are usually identified by the extension\u00a0\".py\" and can be run directly by the interpreter:\n", "\n", "`python apl.py`\n", "\n", "Thus `apl.py` will run. On Windows, the file extensions\u00a0\".py\", \". pyw\", \". pyc\" and \". pyo\" are associated with Python automatically during installation, so just click a the file to run it. The \". pyw\" files run with an alternate version of the interpreter that does not open the console window.\n", "\n", "Dynamic Typing\n", "----------------\n", "Python uses dynamic typing, which means that the type of a variable is inferred by the interpreter at runtime (this is known as *Duck Typing*). By the time a variable is created by attribution the interpreter defines the type of a variable, along with the operations that can be applied.\n", "\n", "Typing of Python is strong, ie, the interpreter checks whether the transactions are valid and does automatic coercions between incompatible types. In Python, coercions are performed automatically only between types that are clearly related, as integer and long integer. To perform the operation between non-compatible types, you must explicitly convert the type of the variable or variables before the operation.\n", "\n", "Compilation and interpretation\n", "--------------------------\n", "The source code is translated by Python to bytecode, which is a binary format with instructions for the interpreter. The bytecode is cross platform and can be distributed and run without the original source.\n", "\n", "![Compilation, interpretation and packing](files/bpyfd_diags1.png)\n", "\n", "By default, the parser compiles the code and stores the bytecode on disk, so the next time you run it, there is no need to recompile the program, reducing the load time of execution. If the source files are changed, the interpreter will be responsible for regenerating the bytecode automatically, even using the *interactive shell*. When a program or a module is invoked, the interpreter performs the analysis of the code, converts to symbols, compiles (if there is no updated bytecode on disk) and runs it in the Python virtual machine.\n", "\n", "The bytecode is stored in files with the extension \". pyc\" (normal bytecode) or \". pyo\" (optimized bytecode). The bytecode can also be packaged along with an executable interpreter, to facilitate the distribution of the application, eliminating the need to install Python on each computer.\n", "\n", "Interactive Mode\n", "----------------\n", "The Python interpreter can be used interactively, where lines of code are typed into a *prompt* (command line) *shell* similar to the operating system.\n", "\n", "`python`\n", "\n", "It is ready to receive commands after the appearance of the signal `>>>` on the screen:\n", "\n", "`Python 2.6.4 (r264:75706, Nov 3 2009, 13:20:47)`
\n", "`[GCC 4.4.1] on linux2`
\n", "`Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.`
\n", "`>>>`\n", "\n", "On Windows, the interactive mode is also available via the icon \"Python (command line)\".\n", "\n", "The interactive mode is a distinguishing feature of the language, as it is possible to test and modify code snippets before inclusion in programs, to extract and convert data or even analyze the state of the objects in memory, among other possibilities.\n", "\n", "Besides the traditional interactive mode of Python, there are other programs that act as alternatives to more sophisticated interfaces (such as PyCrust):\n", "![PyCrust](files/pycrust.png)\n", "\n", "Tools\n", "-----------\n", "There are many development tools for Python, such as IDEs, editors and shells (that take advantage of the interactive capabilities of Python).\n", "\n", "*Integrated Development Environments* (IDEs) are software packages that integrate various development tools in an environment consistent with the goal of increasing developer productivity. Generally, IDEs include such features as syntax highlighting (colorized source code according to the syntax of the language), source browsers, integrated shell and *code completion* (the editor presents possible ways to complete the text it can identify while typing).\n", "Among Python IDEs, there are:\n", "\n", "+ [PyScripter](http://code.google.com/p/pyscripter/)\n", "+ [SPE](http://pythonide.blogspot.com/) (Stani's Python Editor)\n", "+ [Eric](http://eric-ide.python-projects.org/)\n", "+ [PyDev](http://pydev.org/) (plug-in para a IDE Eclipse)\n", "\n", "![PyScripter](files/pyscripter.png)\n", "\n", "There are also text editors specialized in programming code, which have features like syntax colorization, export to other formats and convert text encoding.\n", "\n", "These editors support multiple programming languages\u200b\u200b, Python among them:\n", "\n", "+ [SciTE](http://www.scintilla.org/SciTE.html)\n", "+ [Notepad++](http://notepad-plus.sourceforge.net/br/site.htm)\n", "\n", "*Shell* is the name given to interactive environments for executing commands that can be used to test small pieces of code and for activities like data crunching (extraction of information of interest in masses of data and subsequent translation to other formats).\n", "\n", "Beyond the standard Python *Shell*, there are others available:\n", "\n", "+ PyCrust \n", "+ IPython \n", "\n", "Packers are utilities that are used to build executables that comprise the bytecode, the interpreter and other dependencies, allowing the application to run on machines without Python installed, which facilitates program distribution.\n", "\n", "Among packers for Python, are available:\n", "\n", "+ py2exe (Windows only)\n", "+ cx_Freeze (portable)\n", "\n", "*Frameworks* are collections of software components (libraries, utilities and others) that have been designed to be used by other systems.\n", "\n", "Some of the most known *frameworks* availble are:\n", "\n", "+ Web: Django, TurboGears, Zope and web2py.\n", "+ Graphic interface: wxPython, PyGTK and PyQt.\n", "+ Scientific processing: NumPy and SciPy.\n", "+ Image processing: PIL.\n", "+ 2D: Matplotlib and SVGFig.\n", "+ 3D: Visual Python, PyOpenGL and Python Ogre.\n", "+ Object-relational mapping: SQLAlchemy e SQLObject.\n", "\n", "Culture\n", "-------\n", "The name Python was taken by Guido van Rossum from british TV program *Monty Python's Flying Circus*, and there are many references to the show in its documentation. For instance, Python's oficial package repository was called Cheese Shop, the name of one of the frames of the program. Currently, the repository name is [Python Package Index](http://pypi.python.org/pypi) (PYPI).\n", "\n", "The goals of the project was summarized by Tim Peters in a text called *Zen of Python*, which is available in Python itself using the command:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import this" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The text emphasizes the pragmatic attitude of the *Benevolent Dictator for Life (BDFL)* as Guido is known in the Python community.\n", "\n", "Proposals for improving the language are called PEPs *(Python Enhancement Proposals)*, which also serve as a reference for new features to be implemented in the language.\n", "\n", "In addition to the official website, other good source of information about the language are: [Python Cookbook](http://aspn.activestate.com/ASPN/Python/Cookbook/) site that stores \"recipes\": small portions of code to accomplish specific tasks." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "" ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }