├── javascripts └── main.js ├── Chapter11 ├── dt.csv ├── arq.zip └── temp.txt ├── images ├── bg_hr.png ├── blacktocat.png ├── by-nc-sa.png ├── pypd_cover2e.jpg ├── icon_download.png └── sprite_download.png ├── Chapter1 ├── pycrust.png ├── pyscripter.png ├── bpyfd_diags1.png └── Chapter1_Introduction.ipynb ├── Chapter10 ├── bpyfd_diags8.png ├── Chapter10_Packages.ipynb └── .ipynb_checkpoints │ └── Chapter10_Packages-checkpoint.ipynb ├── Chapter2 ├── bpyfd_diags2.png └── Chapter2_Syntax.ipynb ├── Chapter4 ├── bpyfd_diags3.png └── Chapter4_Loops.ipynb ├── Chapter5 ├── bpyfd_diags4.png └── bpyfd_diags5.png ├── Chapter8 ├── bpyfd_diags6.png ├── calc.py ├── modutils.py ├── Chapter8_Modules.ipynb └── .ipynb_checkpoints │ └── Chapter8_Modules-checkpoint.ipynb ├── Chapter9 ├── bpyfd_diags7.png ├── Chapter9_Scope_of_names.ipynb └── .ipynb_checkpoints │ └── Chapter9_Scope_of_names-checkpoint.ipynb ├── Chapter13 ├── temp.txt ├── trace.log ├── Chapter13_Exceptions.ipynb └── .ipynb_checkpoints │ └── Chapter13_Exceptions-checkpoint.ipynb ├── stylesheets ├── custom.css ├── pygment_trac.css └── stylesheet.css ├── Dockerfile ├── params.json ├── README.md ├── notebook_template.ipynb ├── ExercisesI └── ExercisesI.ipynb ├── Chapter12 ├── Chapter12_Third_Party_Libraries.ipynb └── .ipynb_checkpoints │ └── Chapter12_Third_Party_Libraries-checkpoint.ipynb ├── Chapter3 └── Chapter3_Control_flow.ipynb ├── Chapter15 ├── Chapter15_Generators.ipynb └── .ipynb_checkpoints │ └── Chapter15_Generators-checkpoint.ipynb ├── Chapter7 └── Chapter7_Documentation.ipynb ├── Chapter6 └── Chapter6_Functions.ipynb └── index.html /javascripts/main.js: -------------------------------------------------------------------------------- 1 | console.log('This would be the main JS file.'); 2 | -------------------------------------------------------------------------------- /Chapter11/dt.csv: -------------------------------------------------------------------------------- 1 | temperature,15.0,C,10:40,2006-12-31 2 | weight,42.5,kg,10:45,2006-12-31 3 | -------------------------------------------------------------------------------- /images/bg_hr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/images/bg_hr.png -------------------------------------------------------------------------------- /Chapter11/arq.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter11/arq.zip -------------------------------------------------------------------------------- /Chapter1/pycrust.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter1/pycrust.png -------------------------------------------------------------------------------- /images/blacktocat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/images/blacktocat.png -------------------------------------------------------------------------------- /images/by-nc-sa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/images/by-nc-sa.png -------------------------------------------------------------------------------- /Chapter1/pyscripter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter1/pyscripter.png -------------------------------------------------------------------------------- /images/pypd_cover2e.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/images/pypd_cover2e.jpg -------------------------------------------------------------------------------- /Chapter1/bpyfd_diags1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter1/bpyfd_diags1.png -------------------------------------------------------------------------------- /Chapter10/bpyfd_diags8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter10/bpyfd_diags8.png -------------------------------------------------------------------------------- /Chapter2/bpyfd_diags2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter2/bpyfd_diags2.png -------------------------------------------------------------------------------- /Chapter4/bpyfd_diags3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter4/bpyfd_diags3.png -------------------------------------------------------------------------------- /Chapter5/bpyfd_diags4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter5/bpyfd_diags4.png -------------------------------------------------------------------------------- /Chapter5/bpyfd_diags5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter5/bpyfd_diags5.png -------------------------------------------------------------------------------- /Chapter8/bpyfd_diags6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter8/bpyfd_diags6.png -------------------------------------------------------------------------------- /Chapter9/bpyfd_diags7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/Chapter9/bpyfd_diags7.png -------------------------------------------------------------------------------- /images/icon_download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/images/icon_download.png -------------------------------------------------------------------------------- /images/sprite_download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ricardoduarte/python-for-developers/HEAD/images/sprite_download.png -------------------------------------------------------------------------------- /Chapter8/calc.py: -------------------------------------------------------------------------------- 1 | # File calc.py 2 | 3 | # Function defined in module 4 | def average(list): 5 | 6 | return float(sum(list)) / len(list) 7 | -------------------------------------------------------------------------------- /Chapter13/temp.txt: -------------------------------------------------------------------------------- 1 | 0.06 0.66 0.01 0.94 0.58 2 | 0.93 0.72 0.14 0.88 0.50 3 | 0.74 0.34 0.64 0.51 0.95 4 | 0.35 0.87 0.10 0.02 0.21 5 | 0.66 0.92 0.66 0.46 0.82 6 | -------------------------------------------------------------------------------- /Chapter11/temp.txt: -------------------------------------------------------------------------------- 1 | 000 2 | 001 3 | 002 4 | 003 5 | 004 6 | 005 7 | 006 8 | 007 9 | 008 10 | 009 11 | 010 12 | 011 13 | 012 14 | 013 15 | 014 16 | 015 17 | 016 18 | 017 19 | 018 20 | 019 21 | -------------------------------------------------------------------------------- /Chapter8/modutils.py: -------------------------------------------------------------------------------- 1 | """ 2 | modutils => utility routines for modules 3 | """ 4 | 5 | import os.path 6 | import sys 7 | import glob 8 | 9 | def find(txt): 10 | """find modules with parameter 11 | """ 12 | 13 | resp = [] 14 | 15 | for path in sys.path: 16 | mods = glob.glob('%s/*.py' % path) 17 | 18 | for mod in mods: 19 | if txt in os.path.basename(mod): 20 | resp.append(mod) 21 | 22 | return resp 23 | -------------------------------------------------------------------------------- /stylesheets/custom.css: -------------------------------------------------------------------------------- 1 | h4 { 2 | font-weight: normal; 3 | text-align: justify; 4 | } 5 | 6 | #TOC, #book-cover { 7 | width: 50%; 8 | float: left; 9 | } 10 | 11 | #TOC { 12 | font-size: 130%; 13 | } 14 | 15 | #main_content_wrap p, #main_content_wrap ol { 16 | text-align: justify; 17 | } 18 | 19 | #main_content_wrap ul li { 20 | list-style-type: none; 21 | margin-bottom: 5px; 22 | } 23 | 24 | #main_content_wrap ul li a { 25 | font-weight: bold; 26 | } 27 | 28 | #image-cc { 29 | width: auto !important; 30 | } -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # build with `docker build -t dockername:tag .` 2 | # run with `docker run -p 8888:8888 dockername:tag` 3 | # open http://localhost:8888 in browser 4 | FROM jupyter/base-notebook:37af02395694 5 | 6 | USER root 7 | RUN /bin/bash -c "conda create --yes -n py27 python=2.7; source activate py27; conda install --yes notebook ipykernel; ipython kernel install" 8 | 9 | COPY . /home/jovyan 10 | RUN chown -R jovyan:users /home/jovyan 11 | RUN echo 'PATH="$PATH:/opt/conda/bin"' >> .bashrc 12 | 13 | CMD /bin/bash -c "source activate py27; start-notebook.sh" 14 | -------------------------------------------------------------------------------- /Chapter13/trace.log: -------------------------------------------------------------------------------- 1 | Traceback (most recent call last): 2 | File "", line 5, in 3 | fn = raw_input('Nome do arquivo: ').strip() 4 | File "/usr/local/lib/python2.7/dist-packages/IPython/zmq/ipkernel.py", line 345, in 5 | raw_input = lambda prompt='' : self._no_raw_input() 6 | File "/usr/local/lib/python2.7/dist-packages/IPython/zmq/ipkernel.py", line 688, in _no_raw_input 7 | raise StdinNotImplementedError("raw_input was called, but this " 8 | StdinNotImplementedError: raw_input was called, but this frontend does not support stdin. 9 | Traceback (most recent call last): 10 | File "", line 8, in 11 | for i, s in enumerate(file(fn)): 12 | IOError: [Errno 2] No such file or directory: 'test' 13 | -------------------------------------------------------------------------------- /params.json: -------------------------------------------------------------------------------- 1 | {"name":"Python for Developers","tagline":"First edition in English","body":"### Welcome to GitHub Pages.\r\nThis automatic page generator is the easiest way to create beautiful pages for all of your projects. Author your page content here using GitHub Flavored Markdown, select a template crafted by a designer, and publish. After your page is generated, you can check out the new branch:\r\n\r\n```\r\n$ cd your_repo_root/repo_name\r\n$ git fetch origin\r\n$ git checkout gh-pages\r\n```\r\n\r\nIf you're using the GitHub for Mac, simply sync your repository and you'll see the new branch.\r\n\r\n### Designer Templates\r\nWe've crafted some handsome templates for you to use. Go ahead and continue to layouts to browse through them. You can easily go back to edit your page before publishing. After publishing your page, you can revisit the page generator and switch to another theme. Your Page content will be preserved if it remained markdown format.\r\n\r\n### Rather Drive Stick?\r\nIf you prefer to not use the automatic generator, push a branch named `gh-pages` to your repository to create a page manually. In addition to supporting regular HTML content, GitHub Pages support Jekyll, a simple, blog aware static site generator written by our own Tom Preston-Werner. Jekyll makes it easy to create site-wide headers and footers without having to copy them across every page. It also offers intelligent blog support and other advanced templating features.\r\n\r\n### Authors and Contributors\r\nYou can @mention a GitHub username to generate a link to their profile. The resulting `` element will link to the contributor's GitHub Profile. For example: In 2007, Chris Wanstrath (@defunkt), PJ Hyett (@pjhyett), and Tom Preston-Werner (@mojombo) founded GitHub.\r\n\r\n### Support or Contact\r\nHaving trouble with Pages? Check out the documentation at http://help.github.com/pages or contact support@github.com and we’ll help you sort it out.\r\n","google":"UA-37218909-3","note":"Don't delete this file! It's used internally to help with page regeneration."} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Python for Developers 2 | ===================== 3 | First edition 4 | ------------- 5 | This book is geared toward those who already have programming knowledge. It covers topics that include: creation of user interfaces, computer graphics, internet applications, distributed systems, among other issues. 6 | 7 | Foreword 8 | -------- 9 | Dynamic languages were seen in the past only as scripting languages, used to automate small tasks, but with the passing of time, they grew, matured and conquered their place in the market, to the point of calling the attention of the major providers of technology. 10 | 11 | Several factors contributed to this change, such as the internet, open source software and agile development methodologies. The internet has enabled the sharing of information in a way unprecedented in history, which made possible the growth of open source software. Dynamic languages are usually open source and share the same features and in some cases, the same goals. 12 | 13 | Among the dynamic languages, Python stands out as one of the most popular and powerful. There is a lively community of language users in the world, and this is reflected in active mailing lists and many tools available in open source. 14 | 15 | Learning a new programming language means learning to think differently. And learning a dynamic language represents an even harder paradigm shift for those people who have spent years developing in static languages. 16 | 17 | The e-book "Python para Desenvolvedores" ("Python for Developers") aimed to fill a gap, which was the lack of free courseware in Portuguese about the Python programming language. The work was adopted as support material for several universities in Brazil, served as a reference for academic papers and has been downloaded over 100,000 times from its official website. 18 | 19 | This e-book was originally written in Portuguese and was translated by a small group of volunteers who do not speak English natively, so we ask for help from those who know the English language better, to refine the work, which we believe is a meaningful contribution to the Python users around the world. 20 | 21 | Visit the e-book website: [http://ricardoduarte.github.io/python-for-developers/](http://ricardoduarte.github.io/python-for-developers/) 22 | 23 | Run in docker 24 | ------------- 25 | There is a dockerfile prepared to ease the deployment of the notebook. 26 | 27 | To build and run the container run the following commands 28 | 29 | ``` 30 | docker build -t python-for-developers:latest . 31 | docker run -itp 8888:8888 python-for-developers:latest 32 | ``` 33 | 34 | After this the notebook will be available at `http://localhost:8888` with the token printed to the terminal. 35 | -------------------------------------------------------------------------------- /notebook_template.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "notebook_template" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First edition in English\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 1: Introduction\n", 20 | "=======================\n", 21 | "_______________________\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "collapsed": false, 27 | "input": [], 28 | "language": "python", 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "html": [ 33 | "\n", 88 | "" 103 | ], 104 | "output_type": "pyout", 105 | "prompt_number": 1, 106 | "text": [ 107 | "" 108 | ] 109 | } 110 | ], 111 | "prompt_number": 1 112 | } 113 | ], 114 | "metadata": {} 115 | } 116 | ] 117 | } -------------------------------------------------------------------------------- /stylesheets/pygment_trac.css: -------------------------------------------------------------------------------- 1 | .highlight .hll { background-color: #ffffcc } 2 | .highlight { background: #f0f3f3; } 3 | .highlight .c { color: #0099FF; font-style: italic } /* Comment */ 4 | .highlight .err { color: #AA0000; background-color: #FFAAAA } /* Error */ 5 | .highlight .k { color: #006699; font-weight: bold } /* Keyword */ 6 | .highlight .o { color: #555555 } /* Operator */ 7 | .highlight .cm { color: #0099FF; font-style: italic } /* Comment.Multiline */ 8 | .highlight .cp { color: #009999 } /* Comment.Preproc */ 9 | .highlight .c1 { color: #0099FF; font-style: italic } /* Comment.Single */ 10 | .highlight .cs { color: #0099FF; font-weight: bold; font-style: italic } /* Comment.Special */ 11 | .highlight .gd { background-color: #FFCCCC; border: 1px solid #CC0000 } /* Generic.Deleted */ 12 | .highlight .ge { font-style: italic } /* Generic.Emph */ 13 | .highlight .gr { color: #FF0000 } /* Generic.Error */ 14 | .highlight .gh { color: #003300; font-weight: bold } /* Generic.Heading */ 15 | .highlight .gi { background-color: #CCFFCC; border: 1px solid #00CC00 } /* Generic.Inserted */ 16 | .highlight .go { color: #AAAAAA } /* Generic.Output */ 17 | .highlight .gp { color: #000099; font-weight: bold } /* Generic.Prompt */ 18 | .highlight .gs { font-weight: bold } /* Generic.Strong */ 19 | .highlight .gu { color: #003300; font-weight: bold } /* Generic.Subheading */ 20 | .highlight .gt { color: #99CC66 } /* Generic.Traceback */ 21 | .highlight .kc { color: #006699; font-weight: bold } /* Keyword.Constant */ 22 | .highlight .kd { color: #006699; font-weight: bold } /* Keyword.Declaration */ 23 | .highlight .kn { color: #006699; font-weight: bold } /* Keyword.Namespace */ 24 | .highlight .kp { color: #006699 } /* Keyword.Pseudo */ 25 | .highlight .kr { color: #006699; font-weight: bold } /* Keyword.Reserved */ 26 | .highlight .kt { color: #007788; font-weight: bold } /* Keyword.Type */ 27 | .highlight .m { color: #FF6600 } /* Literal.Number */ 28 | .highlight .s { color: #CC3300 } /* Literal.String */ 29 | .highlight .na { color: #330099 } /* Name.Attribute */ 30 | .highlight .nb { color: #336666 } /* Name.Builtin */ 31 | .highlight .nc { color: #00AA88; font-weight: bold } /* Name.Class */ 32 | .highlight .no { color: #336600 } /* Name.Constant */ 33 | .highlight .nd { color: #9999FF } /* Name.Decorator */ 34 | .highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */ 35 | .highlight .ne { color: #CC0000; font-weight: bold } /* Name.Exception */ 36 | .highlight .nf { color: #CC00FF } /* Name.Function */ 37 | .highlight .nl { color: #9999FF } /* Name.Label */ 38 | .highlight .nn { color: #00CCFF; font-weight: bold } /* Name.Namespace */ 39 | .highlight .nt { color: #330099; font-weight: bold } /* Name.Tag */ 40 | .highlight .nv { color: #003333 } /* Name.Variable */ 41 | .highlight .ow { color: #000000; font-weight: bold } /* Operator.Word */ 42 | .highlight .w { color: #bbbbbb } /* Text.Whitespace */ 43 | .highlight .mf { color: #FF6600 } /* Literal.Number.Float */ 44 | .highlight .mh { color: #FF6600 } /* Literal.Number.Hex */ 45 | .highlight .mi { color: #FF6600 } /* Literal.Number.Integer */ 46 | .highlight .mo { color: #FF6600 } /* Literal.Number.Oct */ 47 | .highlight .sb { color: #CC3300 } /* Literal.String.Backtick */ 48 | .highlight .sc { color: #CC3300 } /* Literal.String.Char */ 49 | .highlight .sd { color: #CC3300; font-style: italic } /* Literal.String.Doc */ 50 | .highlight .s2 { color: #CC3300 } /* Literal.String.Double */ 51 | .highlight .se { color: #CC3300; font-weight: bold } /* Literal.String.Escape */ 52 | .highlight .sh { color: #CC3300 } /* Literal.String.Heredoc */ 53 | .highlight .si { color: #AA0000 } /* Literal.String.Interpol */ 54 | .highlight .sx { color: #CC3300 } /* Literal.String.Other */ 55 | .highlight .sr { color: #33AAAA } /* Literal.String.Regex */ 56 | .highlight .s1 { color: #CC3300 } /* Literal.String.Single */ 57 | .highlight .ss { color: #FFCC33 } /* Literal.String.Symbol */ 58 | .highlight .bp { color: #336666 } /* Name.Builtin.Pseudo */ 59 | .highlight .vc { color: #003333 } /* Name.Variable.Class */ 60 | .highlight .vg { color: #003333 } /* Name.Variable.Global */ 61 | .highlight .vi { color: #003333 } /* Name.Variable.Instance */ 62 | .highlight .il { color: #FF6600 } /* Literal.Number.Integer.Long */ 63 | 64 | .type-csharp .highlight .k { color: #0000FF } 65 | .type-csharp .highlight .kt { color: #0000FF } 66 | .type-csharp .highlight .nf { color: #000000; font-weight: normal } 67 | .type-csharp .highlight .nc { color: #2B91AF } 68 | .type-csharp .highlight .nn { color: #000000 } 69 | .type-csharp .highlight .s { color: #A31515 } 70 | .type-csharp .highlight .sc { color: #A31515 } 71 | -------------------------------------------------------------------------------- /Chapter10/Chapter10_Packages.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 10: Packages\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "Packages are folders that are identified by the interpreter by the presence of a file named `__init__.py`. Packages work as collections to organize modules hierarchically.\n", 23 | "\n", 24 | "![Package](files/bpyfd_diags8.png)\n", 25 | "\n", 26 | "You can import all package modules using the statement `from packagename import *`.\n", 27 | "\n", 28 | "The `__init__.py` file can be empty or contain package initialization code or define a variable called `__all__`, a list of modules of the package that will be imported when using \"*\". Without the file, Python does not recognize the folder as a valid packet." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "collapsed": false, 34 | "input": [], 35 | "language": "python", 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "html": [ 40 | "\n", 88 | "" 103 | ], 104 | "output_type": "pyout", 105 | "prompt_number": 1, 106 | "text": [ 107 | "" 108 | ] 109 | } 110 | ], 111 | "prompt_number": 1 112 | } 113 | ], 114 | "metadata": {} 115 | } 116 | ] 117 | } -------------------------------------------------------------------------------- /Chapter10/.ipynb_checkpoints/Chapter10_Packages-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 10: Packages\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "Packages are folders that are identified by the interpreter by the presence of a file named `__init__.py`. Packages work as collections to organize hierarchically modules.\n", 23 | "\n", 24 | "![Package](files/bpyfd_diags8.png)\n", 25 | "\n", 26 | "You can import all package modules using the statement `from packagename import *`.\n", 27 | "\n", 28 | "The `__init__.py` file can be empty or contain package initialization code or define a variable called `__all__`, a list of modules of the package that will be imported when using \"*\". Without the file, Python does not recognize the folder as a valid packet." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "collapsed": false, 34 | "input": [], 35 | "language": "python", 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "html": [ 40 | "\n", 88 | "" 103 | ], 104 | "output_type": "pyout", 105 | "prompt_number": 1, 106 | "text": [ 107 | "" 108 | ] 109 | } 110 | ], 111 | "prompt_number": 1 112 | } 113 | ], 114 | "metadata": {} 115 | } 116 | ] 117 | } -------------------------------------------------------------------------------- /ExercisesI/ExercisesI.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "ExercisesI" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Exercises I\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "1\\. Implement the two functions:\n", 23 | "\n", 24 | "+ One that converts temperature in degrees *Celsius* to degrees *Fahrenheit*.\n", 25 | "+ Anthoer that converts temperature in degrees *Fahrenheit* to degrees *Celsius*.\n", 26 | "\n", 27 | "Keep in mind that:\n", 28 | "\n", 29 | "$$F = \\frac{9}{5} \\cdot C + 32$$\n", 30 | "\n", 31 | "2\\. Implement a function that returns true if the number is prime (false otherwise). Test from 1 to 100.\n", 32 | "\n", 33 | "3\\. Implement a function that takes a list of lists of any length and returns a list of one dimension.\n", 34 | "\n", 35 | "4\\. Implement a function that takes a dictionary and returns the sum, average and range of its values\u200b\u200b.\n", 36 | "\n", 37 | "5\\. Write a function that:\n", 38 | "Gets a phrase as a parameter.\n", 39 | "Returns a new sentence with each word with the letters reversed.\n", 40 | "\n", 41 | "6\\. Create a function that:\n", 42 | "\n", 43 | "+ Gets a list of tuples (data), an integer (key, by default equal to zero) and a boolean (reverse, false by default).\n", 44 | "+ Returns data sorted by item indicated by key in descending order and if reverse is true." 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "collapsed": false, 50 | "input": [], 51 | "language": "python", 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "html": [ 56 | "\n", 104 | "" 119 | ], 120 | "output_type": "pyout", 121 | "prompt_number": 1, 122 | "text": [ 123 | "" 124 | ] 125 | } 126 | ], 127 | "prompt_number": 1 128 | } 129 | ], 130 | "metadata": {} 131 | } 132 | ] 133 | } -------------------------------------------------------------------------------- /Chapter9/Chapter9_Scope_of_names.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-desvelopers/#content)\n", 15 | "===================================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 9: Scope of names\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "\n", 23 | "The scope of names in Python are maintained by *Namespaces*, which are dictionaries that list the names of the objects (references) and the objects themselves.\n", 24 | "\n", 25 | "Normally, the names are defined in two dictionaries, which can be accessed through the functions `locals()` and `globals()`. These dictionaries are updated dynamically at runtime.\n", 26 | "\n", 27 | "![Namespaces](files/bpyfd_diags7.png)\n", 28 | "\n", 29 | "Global variables can be overshadowed by local variables (because the local scope is consulted before the global scope). To avoid this, you must declare the variable as global in the local scope.\n", 30 | "\n", 31 | "example:" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "collapsed": false, 37 | "input": [ 38 | "def addlist(list):\n", 39 | " \"\"\"\n", 40 | " Add lists of lists, recursively\n", 41 | " the result is global\n", 42 | " \"\"\"\n", 43 | " global add\n", 44 | " \n", 45 | " for item in list:\n", 46 | " if type(item) is list: # If item type is list\n", 47 | " addlist(item)\n", 48 | " else:\n", 49 | " add += item\n", 50 | "\n", 51 | "add = 0\n", 52 | "addlist([[1, 2], [3, 4, 5], 6])\n", 53 | "\n", 54 | "print add # 21" 55 | ], 56 | "language": "python", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "output_type": "stream", 61 | "stream": "stdout", 62 | "text": [ 63 | "21\n" 64 | ] 65 | } 66 | ], 67 | "prompt_number": 1 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "Using global variables is not considered a good development practice, as they make the system harder to understand, so it is better to avoid their use. The same applies to overshadowing variables." 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "collapsed": false, 79 | "input": [], 80 | "language": "python", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "html": [ 85 | "\n", 133 | "" 148 | ], 149 | "output_type": "pyout", 150 | "prompt_number": 1, 151 | "text": [ 152 | "" 153 | ] 154 | } 155 | ], 156 | "prompt_number": 1 157 | } 158 | ], 159 | "metadata": {} 160 | } 161 | ] 162 | } -------------------------------------------------------------------------------- /Chapter9/.ipynb_checkpoints/Chapter9_Scope_of_names-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-desvelopers/#content)\n", 15 | "===================================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 9: Scope of names\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "\n", 23 | "The scope of names in Python is maintained by *Namespaces*, which are dictionaries that list the names of the objects (references) and the objects themselves.\n", 24 | "\n", 25 | "Normally, the names are defined in two dictionaries, which can be accessed through the functions `locals()` and `globals()`. These dictionaries are updated dynamically at runtime.\n", 26 | "\n", 27 | "![Namespaces](files/bpyfd_diags7.png)\n", 28 | "\n", 29 | "Global variables can be overshadowed by local variables (because the local scope is consulted before the global scope). To avoid this, you must declare the variable as global in the local scope.\n", 30 | "\n", 31 | "example:" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "collapsed": false, 37 | "input": [ 38 | "def addlist(list):\n", 39 | " \"\"\"\n", 40 | " Add lists of lists, recursively\n", 41 | " the result is global\n", 42 | " \"\"\"\n", 43 | " global add\n", 44 | " \n", 45 | " for item in list:\n", 46 | " if type(item) is list: # If item type is list\n", 47 | " addlist(item)\n", 48 | " else:\n", 49 | " add += item\n", 50 | "\n", 51 | "add = 0\n", 52 | "addlist([[1, 2], [3, 4, 5], 6])\n", 53 | "\n", 54 | "print add # 21" 55 | ], 56 | "language": "python", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "output_type": "stream", 61 | "stream": "stdout", 62 | "text": [ 63 | "21\n" 64 | ] 65 | } 66 | ], 67 | "prompt_number": 1 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "Using global variables is not considered a good development practice, as they make the system harder to understand, so it is better to avoid its use. The same applies to overshadowing variables." 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "collapsed": false, 79 | "input": [], 80 | "language": "python", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "html": [ 85 | "\n", 133 | "" 148 | ], 149 | "output_type": "pyout", 150 | "prompt_number": 1, 151 | "text": [ 152 | "" 153 | ] 154 | } 155 | ], 156 | "prompt_number": 1 157 | } 158 | ], 159 | "metadata": {} 160 | } 161 | ] 162 | } -------------------------------------------------------------------------------- /Chapter12/Chapter12_Third_Party_Libraries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 12 : Third Party Libraries\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "There are many libraries written by others available for Python, consisting of packages or modules, that implement many features beyond the standard library.\n", 23 | "\n", 24 | "Generally, the libraries are distributed in the following ways:\n", 25 | "\n", 26 | "+ *distutils* packages.\n", 27 | "+ Package managers from operating system.\n", 28 | "+ Installers.\n", 29 | "+ Python Eggs.\n", 30 | "\n", 31 | "Packages using *distutils* module, which is distributed with Python, are very popular. The packages are distributed in compressed files (usually \".tar.gz\" , \".tar.bz2\" or \".zip \" ). To install, you must unzip the file, go into the folder that was unzipped, and to execute the command:\n", 32 | "\n", 33 | " python setup.py install\n", 34 | "\n", 35 | "The package will be installed in \"site-packages\" folder in Python.\n", 36 | "\n", 37 | "Package managers from operating System usually work with their own packet formats, like \".deb\" (Debian Linux) or \".rpm\" (RedHat Linux). How to install the packages depends on the manager. The big advantage is that the package manager takes care of dependencies and updates.\n", 38 | "\n", 39 | "Installer programs are nothing more than executables that install the library. They are generally used in Windows environments and can be uninstalled from the Control Panel.\n", 40 | "\n", 41 | "Egg is a Python package format (with the extension \".egg\") which is administered by the easy_install utility, which is part of the project [setuptools](http://peak.telecommunity.com/DevCenter/setuptools/). Similar to some tools found in other languages such as Ruby Gems, it is gradually becoming the de facto standard for distributing libraries in Python.\n", 42 | "\n", 43 | "The program finds the newest version of the package in [PYPI](http://pypi.python.org/pypi) (*Python Package Index*), the Python package repository, and also tries to install dependencies as needed.\n", 44 | "\n", 45 | "Python Egg packages can be installed via the command:\n", 46 | "\n", 47 | " easy_install package_name\n", 48 | "\n", 49 | "The easy_install *script* is installed in the \"scripts\" folder of Python." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "collapsed": false, 55 | "input": [], 56 | "language": "python", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "html": [ 61 | "\n", 109 | "" 124 | ], 125 | "output_type": "pyout", 126 | "prompt_number": 1, 127 | "text": [ 128 | "" 129 | ] 130 | } 131 | ], 132 | "prompt_number": 1 133 | } 134 | ], 135 | "metadata": {} 136 | } 137 | ] 138 | } -------------------------------------------------------------------------------- /Chapter12/.ipynb_checkpoints/Chapter12_Third_Party_Libraries-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 12 : Third Party Libraries\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "There are many libraries written by others available for Python, consisting of packages or modules, that implement many features beyond the standard library .\n", 23 | "\n", 24 | "Generally, the libraries are distributed in the following ways :\n", 25 | "\n", 26 | "+ *distutils* packages.\n", 27 | "+ Package managers from operating system.\n", 28 | "+ Installers.\n", 29 | "+ Python Eggs.\n", 30 | "\n", 31 | "Packages using *distutils* module, which is distributed with Python, are very popular. The packages are distributed in compressed files (usually \".tar.gz\" , \".tar.bz2\" or \".zip \" ). To install , you must unzip the file , go into the folder that was unzipped , and to execute the command:\n", 32 | "\n", 33 | " python setup.py install\n", 34 | "\n", 35 | "The package will be installed in \"site-packages\" folder in Python .\n", 36 | "\n", 37 | "Package managers from operating System, usually work with their own packet formats, like \".deb\" (Debian Linux) or \".rpm\" (RedHat Linux). How to install the packages depends on the manager. The big advantage is that the package manager takes care of dependencies and updates.\n", 38 | "\n", 39 | "Installer programs are nothing more than executables that install the library. They are generally used in Windows environment and can be uninstalled from the Control Panel.\n", 40 | "\n", 41 | "Egg is a Python package format (with the extension \".egg\") which is administered by easy_install utility, which is part of the project [setuptools](http://peak.telecommunity.com/DevCenter/setuptools/). Similar to some tools found in other languages such as Ruby Gems, is gradually becoming the de facto standard for distributing libraries in Python .\n", 42 | "\n", 43 | "The program finds the newest version of the package in [PYPI](http://pypi.python.org/pypi) (*Python Package Index*), the Python package repository, and also tries to install dependencies as needed .\n", 44 | "\n", 45 | "Python Egg packages can be installed via the command:\n", 46 | "\n", 47 | " easy_install package_name\n", 48 | "\n", 49 | "The easy_install *script* is installed in the \"scripts\" folder of Python." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "collapsed": false, 55 | "input": [], 56 | "language": "python", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "html": [ 61 | "\n", 109 | "" 124 | ], 125 | "output_type": "pyout", 126 | "prompt_number": 1, 127 | "text": [ 128 | "" 129 | ] 130 | } 131 | ], 132 | "prompt_number": 1 133 | } 134 | ], 135 | "metadata": {} 136 | } 137 | ] 138 | } -------------------------------------------------------------------------------- /Chapter3/Chapter3_Control_flow.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "Chapter3_Control_flow" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "orig_nbformat": 2, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "markdown", 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 3: Control Flow\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "It is very common for a program that certain sets of instructions are executed conditionally, in cases such as validating data entries, for example.\n", 23 | "\n", 24 | "Syntax:\n", 25 | "\n", 26 | " if :\n", 27 | " \n", 28 | " elif :\n", 29 | " \n", 30 | " elif :\n", 31 | " \n", 32 | " else:\n", 33 | " \n", 34 | "\n", 35 | "Where:\n", 36 | "\n", 37 | "+ ``: sentence that can be evaluated as true or false.\n", 38 | "+ ``: sequence of command lines.\n", 39 | "+ The clauses `elif` and `else` are optional and\u00a0 several `elifs` for the `if` may be used but only\u00a0 one `else` at the end.\n", 40 | "+ Parentheses are only required to avoid ambiguity.\n", 41 | "Example:" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "collapsed": false, 47 | "input": [ 48 | "temp = 23 # temperature value used to test\n", 49 | "\n", 50 | "if temp < 0:\n", 51 | " print 'Freezing...'\n", 52 | "elif 0 <= temp <= 20:\n", 53 | " print 'Cold'\n", 54 | "elif 21 <= temp <= 25:\n", 55 | " print 'Normal'\n", 56 | "elif 26 <= temp <= 35:\n", 57 | " print 'Hot'\n", 58 | "else:\n", 59 | " print 'Very Hot!'" 60 | ], 61 | "language": "python", 62 | "outputs": [ 63 | { 64 | "output_type": "stream", 65 | "stream": "stdout", 66 | "text": [ 67 | "Normal\n" 68 | ] 69 | } 70 | ], 71 | "prompt_number": 3 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "source": [ 76 | "Imagine that in the above program, `23` is the temperature which was read by\u00a0 some sensor or manually entered by the user and `Normal` is the response of the program.\n", 77 | "\n", 78 | "If the code block is composed of only one line, it can be written after the colon:\n", 79 | "\n", 80 | " if temp < 0: print 'Freezing...'\n", 81 | "\n", 82 | "Since version 2.5, Python supports the expression:\n", 83 | "\n", 84 | " = if else \n", 85 | "\n", 86 | "Where `` receives `` if `` is true and `` otherwise." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "collapsed": false, 92 | "input": [], 93 | "language": "python", 94 | "outputs": [ 95 | { 96 | "html": [ 97 | "\n", 145 | "" 160 | ], 161 | "output_type": "pyout", 162 | "prompt_number": 1, 163 | "text": [ 164 | "" 165 | ] 166 | } 167 | ], 168 | "prompt_number": 1 169 | } 170 | ] 171 | } 172 | ] 173 | } -------------------------------------------------------------------------------- /Chapter4/Chapter4_Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "Chapter4_Loops" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 4: Loops\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "Loops are repetition structures, generally used to process data collections, such as lines of a file or records of a database that must be processed by the same code block.\n", 23 | "\n", 24 | "For\n", 25 | "---\n", 26 | "It is the repetition structure most often used in Python. The statement accepts not only static\u00a0sequences, but also sequences generated by iterators. Iterators are structures that allow iterations, i.e. access to items of a collection of elements, sequentially.\n", 27 | "\n", 28 | "![Loop Example](files/bpyfd_diags3.png)\n", 29 | "\n", 30 | "During the execution of a *for* loop, the reference points to an element in the sequence. At each iteration, the reference is updated, in order for the *for* code block to process the corresponding element.\n", 31 | "\n", 32 | "The clause *break*\u00a0stops the loop and *continue* passes it to the next iteration. The code inside the `else` is executed at the end of the loop, except if the loop has been interrupted by *break*.\n", 33 | "\n", 34 | "Syntax:\n", 35 | "\n", 36 | " for in :\n", 37 | " \n", 38 | " continue\n", 39 | " break\n", 40 | " else:\n", 41 | " \n", 42 | " \n", 43 | "Example:" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "collapsed": false, 49 | "input": [ 50 | "# Sum 0 to 99\n", 51 | "s = 0\n", 52 | "for x in range(1, 100):\n", 53 | " s = s + x\n", 54 | "print s" 55 | ], 56 | "language": "python", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "output_type": "stream", 61 | "stream": "stdout", 62 | "text": [ 63 | "4950\n" 64 | ] 65 | } 66 | ], 67 | "prompt_number": 1 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "The function `range(m, n, p)`, is very useful in loops, as it returns a list of integers starting at `m` through smaller than\u00a0`n` in steps of length `p`, which can be used as the order for the loop.\n", 74 | "\n", 75 | "While\n", 76 | "-----\n", 77 | "Executes a block of code in response to a condition.\n", 78 | "\n", 79 | "Syntax:\n", 80 | "\n", 81 | " while :\n", 82 | " \n", 83 | " continue\n", 84 | " break\n", 85 | " else:\n", 86 | " \n", 87 | " \n", 88 | "The code block inside the *while* loop is repeated while the loop condition is evaluated as true.\n", 89 | " \n", 90 | "Example:" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "collapsed": false, 96 | "input": [ 97 | "# Sum 0 to 99\n", 98 | "s = 0\n", 99 | "x = 1\n", 100 | "\n", 101 | "while x < 100:\n", 102 | " s = s + x\n", 103 | " x = x + 1\n", 104 | "print s" 105 | ], 106 | "language": "python", 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "output_type": "stream", 111 | "stream": "stdout", 112 | "text": [ 113 | "4950\n" 114 | ] 115 | } 116 | ], 117 | "prompt_number": 2 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "The *while* loop is appropriate when there is no way to determine how many iterations will occur and there is a sequence to follow." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "collapsed": false, 129 | "input": [], 130 | "language": "python", 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "html": [ 135 | "\n", 183 | "" 198 | ], 199 | "output_type": "pyout", 200 | "prompt_number": 1, 201 | "text": [ 202 | "" 203 | ] 204 | } 205 | ], 206 | "prompt_number": 1 207 | } 208 | ], 209 | "metadata": {} 210 | } 211 | ] 212 | } -------------------------------------------------------------------------------- /Chapter15/Chapter15_Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 15: Generators\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "Functions generally follow the conventional process flow, return values and quit. Generators work similarly, but remember the state of the processing between calls, staying in memory and returning the next item expected when activated.\n", 23 | "\n", 24 | "Generators have several advantages over conventional functions:\n", 25 | "\n", 26 | "+ *Lazy Evaluation*: generators are only processed when it is really needed, saving processing resources. \n", 27 | "+ They reduce the need to create lists.\n", 28 | "+ They allow working with unlimited sequences of elements.\n", 29 | "\n", 30 | "Generators are usually called through a *for* loop. The syntax is similar to the traditional function, but with the *yield* instruction substituting for *return*. In each new iteraction, *yield* returns the next value.\n", 31 | "\n", 32 | "Exemplo:" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "collapsed": false, 38 | "input": [ 39 | "def gen_pares():\n", 40 | " \"\"\"\n", 41 | " Generates even numbers from 0 to 20\n", 42 | " \"\"\"\n", 43 | " i = 0\n", 44 | "\n", 45 | " while i <= 20:\n", 46 | " yield i\n", 47 | " i += 2\n", 48 | "\n", 49 | "# Shows each number and goes to the next\n", 50 | "for n in gen_pares():\n", 51 | " print n" 52 | ], 53 | "language": "python", 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "output_type": "stream", 58 | "stream": "stdout", 59 | "text": [ 60 | "0\n", 61 | "2\n", 62 | "4\n", 63 | "6\n", 64 | "8\n", 65 | "10\n", 66 | "12\n", 67 | "14\n", 68 | "16\n", 69 | "18\n", 70 | "20\n" 71 | ] 72 | } 73 | ], 74 | "prompt_number": 4 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "Another example:" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "collapsed": false, 86 | "input": [ 87 | "import os\n", 88 | "\n", 89 | "# Finds files recursively\n", 90 | "def find(path='.'):\n", 91 | "\n", 92 | " for item in os.listdir(path):\n", 93 | " fn = os.path.normpath(os.path.join(path, item))\n", 94 | "\n", 95 | " if os.path.isdir(fn):\n", 96 | "\n", 97 | " for f in find(fn):\n", 98 | " yield f\n", 99 | "\n", 100 | " else:\n", 101 | " yield fn\n", 102 | "\n", 103 | "# At each interaction, the generator yeld a new file name\n", 104 | "for fn in find():\n", 105 | " print fn" 106 | ], 107 | "language": "python", 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "output_type": "stream", 112 | "stream": "stdout", 113 | "text": [ 114 | ".ipynb_checkpoints/Chapter15_Generators-checkpoint.ipynb\n", 115 | "Chapter15_Generators.ipynb\n" 116 | ] 117 | } 118 | ], 119 | "prompt_number": 2 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "There are many generators that are built in to the lanaguage, like xrange(). Moreover, in the module *itertools*, many useful generators are defined.\n", 126 | "\n", 127 | "To convert the output of a generator into a list:\n", 128 | "\n", 129 | " alist = list(generator())\n", 130 | "\n", 131 | "That way, all the items will be generated at once." 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "collapsed": false, 137 | "input": [], 138 | "language": "python", 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "html": [ 143 | "\n", 191 | "" 206 | ], 207 | "output_type": "pyout", 208 | "prompt_number": 1, 209 | "text": [ 210 | "" 211 | ] 212 | } 213 | ], 214 | "prompt_number": 1 215 | } 216 | ], 217 | "metadata": {} 218 | } 219 | ] 220 | } 221 | -------------------------------------------------------------------------------- /Chapter15/.ipynb_checkpoints/Chapter15_Generators-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 15: Generators\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "tentativa\n", 23 | "The functions generally follow the conventional process flow, return values \u200b\u200band quit. Generators work similarly, but remember the state of the processing between calls, staying in memory and returning the next item expected when activated.\n", 24 | "\n", 25 | "The generators have several advantages over conventional functions:\n", 26 | "\n", 27 | "+ *Lazy Evaluation*: generators are only processed when it is really needed, saving processing resources. \n", 28 | "+ They reduce the need to create lists.\n", 29 | "+ They allow to work with unlimited sequences of elements.\n", 30 | "\n", 31 | "Generators are usually called through a *for* loop. The syntax is similar to the traditional function, just the *yield* instruction substitutes *return*. In each new iteraction, *yield* returns the next value.\n", 32 | "\n", 33 | "Exemplo:" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "collapsed": false, 39 | "input": [ 40 | "def gen_pares():\n", 41 | " \"\"\"\n", 42 | " Generates even numbers from 0 to 20\n", 43 | " \"\"\"\n", 44 | " i = 0\n", 45 | "\n", 46 | " while i <= 20:\n", 47 | " yield i\n", 48 | " i += 2\n", 49 | "\n", 50 | "# Shows each number and goes to the next\n", 51 | "for n in gen_pares():\n", 52 | " print n" 53 | ], 54 | "language": "python", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "output_type": "stream", 59 | "stream": "stdout", 60 | "text": [ 61 | "0\n", 62 | "2\n", 63 | "4\n", 64 | "6\n", 65 | "8\n", 66 | "10\n", 67 | "12\n", 68 | "14\n", 69 | "16\n", 70 | "18\n", 71 | "20\n" 72 | ] 73 | } 74 | ], 75 | "prompt_number": 4 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "Another example:" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "collapsed": false, 87 | "input": [ 88 | "import os\n", 89 | "\n", 90 | "# Finds files recursively\n", 91 | "def find(path='.'):\n", 92 | "\n", 93 | " for item in os.listdir(path):\n", 94 | " fn = os.path.normpath(os.path.join(path, item))\n", 95 | "\n", 96 | " if os.path.isdir(fn):\n", 97 | "\n", 98 | " for f in find(fn):\n", 99 | " yield f\n", 100 | "\n", 101 | " else:\n", 102 | " yield fn\n", 103 | "\n", 104 | "# At each interaction, the generator yeld a new file name\n", 105 | "for fn in find():\n", 106 | " print fn" 107 | ], 108 | "language": "python", 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "output_type": "stream", 113 | "stream": "stdout", 114 | "text": [ 115 | ".ipynb_checkpoints/Chapter15_Generators-checkpoint.ipynb\n", 116 | "Chapter15_Generators.ipynb\n" 117 | ] 118 | } 119 | ], 120 | "prompt_number": 2 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "There are many generators that are builtin to the lanaguage, like xrange(). Moreover, in the module *itertools*, many useful generators are defined.\n", 127 | "\n", 128 | "To convert the output of a generator into a list:\n", 129 | "\n", 130 | " alist = list(generator())\n", 131 | "\n", 132 | "That way, all the items will be generated at once." 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "collapsed": false, 138 | "input": [], 139 | "language": "python", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "html": [ 144 | "\n", 192 | "" 207 | ], 208 | "output_type": "pyout", 209 | "prompt_number": 1, 210 | "text": [ 211 | "" 212 | ] 213 | } 214 | ], 215 | "prompt_number": 1 216 | } 217 | ], 218 | "metadata": {} 219 | } 220 | ] 221 | } -------------------------------------------------------------------------------- /Chapter2/Chapter2_Syntax.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "Chapter2_Syntax" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "=============================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 2: Syntax\n", 20 | "===================\n", 21 | "___________________\n", 22 | "A program written in Python consists of lines, which may continue on the following lines, by using the backslash character (`\\`) at the end of the line or parentheses, brackets or braces in expressions that use such characters.\n", 23 | "\n", 24 | "The character `#` marks the beginning of a comment. Any text after the `#` will be ignored until the end of the line, with the exception of functional comments.\n", 25 | "\n", 26 | "Functional comments are used to:\n", 27 | "\n", 28 | "+ change the encoding of the source file of the program by adding a comment with the text `# - * - coding: - # -` at the beginning of the file, in which ` ` is the file encoding (usually latin1 or utf-8). Changing encoding is required to support characters that are not part of the English language, in the source code of the program.\n", 29 | "+ define the interpreter that will be used to run the program on UNIX systems, through a comment starting with `#!` at the beginning of the file, which indicates the path to the interpreter (usually the comment line will be something like` #! / usr / bin / env python` ).\n", 30 | "\n", 31 | "Example of functional comments:" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "collapsed": false, 37 | "input": [ 38 | "#!/usr/bin/env python\n", 39 | "\n", 40 | "# A code line that shows the result of 7 times 3\n", 41 | "print 7 * 3" 42 | ], 43 | "language": "python", 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "output_type": "stream", 48 | "stream": "stdout", 49 | "text": [ 50 | "21\n" 51 | ] 52 | } 53 | ], 54 | "prompt_number": 3 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "Examples of broken lines:" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "collapsed": false, 66 | "input": [ 67 | "# A line broken by backslash\n", 68 | "a = 7 * 3 + \\\n", 69 | "5 / 2\n", 70 | "\n", 71 | "# A list (broken by comma)\n", 72 | "b = ['a', 'b', 'c', \n", 73 | "'d', 'e']\n", 74 | "\n", 75 | "# A function call (broken by comma)\n", 76 | "c = range(1,\n", 77 | "11)\n", 78 | "\n", 79 | "# Prints everything\n", 80 | "print a, b, c" 81 | ], 82 | "language": "python", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "output_type": "stream", 87 | "stream": "stdout", 88 | "text": [ 89 | "23 ['a', 'b', 'c', 'd', 'e'] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" 90 | ] 91 | } 92 | ], 93 | "prompt_number": 5 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "The command `print` inserts spaces between expressions that are received as a parameter, and a newline character at the end, unless it receives a comma at the end of the parameter list.\n", 100 | "\n", 101 | "Blocks\n", 102 | "------\n", 103 | "In Python, code blocks are defined by the use of indentation, which should be constant in the code block, but it is considered good practice to maintain consistency throughout the project and avoid mixing tabs and spaces.\n", 104 | "\n", 105 | "The line before the block always ends with a colon (:) and is a control structure of the language or a statement of a new structure (a function, for example).\n", 106 | "\n", 107 | "![Program structure](files/bpyfd_diags2.png)\n", 108 | "\n", 109 | "Example:" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "collapsed": false, 115 | "input": [ 116 | "# For i on the list 234, 654, 378, 798:\n", 117 | "for i in [234, 654, 378, 798]:\n", 118 | " # If the remainder dividing by 3 is equal to zero:\n", 119 | " if i % 3 == 0:\n", 120 | " # Prints...\n", 121 | " print i, '/ 3 =', i / 3" 122 | ], 123 | "language": "python", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "output_type": "stream", 128 | "stream": "stdout", 129 | "text": [ 130 | "234 / 3 = 78\n", 131 | "654 / 3 = 218\n", 132 | "378 / 3 = 126\n", 133 | "798 / 3 = 266\n" 134 | ] 135 | } 136 | ], 137 | "prompt_number": 1 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "The operator `%` computes the modulus (remainder of division)." 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "collapsed": false, 149 | "input": [], 150 | "language": "python", 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "html": [ 155 | "\n", 203 | "" 218 | ], 219 | "output_type": "pyout", 220 | "prompt_number": 1, 221 | "text": [ 222 | "" 223 | ] 224 | } 225 | ], 226 | "prompt_number": 1 227 | } 228 | ], 229 | "metadata": {} 230 | } 231 | ] 232 | } -------------------------------------------------------------------------------- /stylesheets/stylesheet.css: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | Slate Theme for GitHub Pages 3 | by Jason Costello, @jsncostello 4 | *******************************************************************************/ 5 | 6 | @import url(pygment_trac.css); 7 | 8 | /******************************************************************************* 9 | MeyerWeb Reset 10 | *******************************************************************************/ 11 | 12 | html, body, div, span, applet, object, iframe, 13 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 14 | a, abbr, acronym, address, big, cite, code, 15 | del, dfn, em, img, ins, kbd, q, s, samp, 16 | small, strike, strong, sub, sup, tt, var, 17 | b, u, i, center, 18 | dl, dt, dd, ol, ul, li, 19 | fieldset, form, label, legend, 20 | table, caption, tbody, tfoot, thead, tr, th, td, 21 | article, aside, canvas, details, embed, 22 | figure, figcaption, footer, header, hgroup, 23 | menu, nav, output, ruby, section, summary, 24 | time, mark, audio, video { 25 | margin: 0; 26 | padding: 0; 27 | border: 0; 28 | font: inherit; 29 | vertical-align: baseline; 30 | } 31 | 32 | @font-face { 33 | font-family: "Computer Modern"; 34 | src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf'); 35 | } 36 | 37 | /* HTML5 display-role reset for older browsers */ 38 | article, aside, details, figcaption, figure, 39 | footer, header, hgroup, menu, nav, section { 40 | display: block; 41 | } 42 | 43 | ol, ul { 44 | list-style: none; 45 | } 46 | 47 | blockquote, q { 48 | } 49 | 50 | table { 51 | border-collapse: collapse; 52 | border-spacing: 0; 53 | } 54 | 55 | /******************************************************************************* 56 | Theme Styles 57 | *******************************************************************************/ 58 | 59 | body { 60 | box-sizing: border-box; 61 | color:#373737; 62 | background: #212121; 63 | font-size: 16px; 64 | font-family: 'Myriad Pro', Calibri, Helvetica, Arial, sans-serif; 65 | line-height: 1.5; 66 | -webkit-font-smoothing: antialiased; 67 | } 68 | 69 | h1, h2, h3, h4, h5, h6 { 70 | margin: 10px 0; 71 | font-weight: 700; 72 | color:#222222; 73 | font-family: 'Lucida Grande', 'Calibri', Helvetica, Arial, sans-serif; 74 | letter-spacing: -1px; 75 | } 76 | 77 | h1 { 78 | font-size: 36px; 79 | font-weight: 700; 80 | } 81 | 82 | h2 { 83 | padding-bottom: 10px; 84 | font-size: 32px; 85 | background: url('../images/bg_hr.png') repeat-x bottom; 86 | } 87 | 88 | h3 { 89 | font-size: 24px; 90 | } 91 | 92 | h4 { 93 | font-size: 21px; 94 | } 95 | 96 | h5 { 97 | font-size: 18px; 98 | } 99 | 100 | h6 { 101 | font-size: 16px; 102 | } 103 | 104 | p { 105 | margin: 10px 0 15px 0; 106 | } 107 | 108 | footer p { 109 | color: #f2f2f2; 110 | } 111 | 112 | a { 113 | text-decoration: none; 114 | color: #007edf; 115 | text-shadow: none; 116 | 117 | transition: color 0.5s ease; 118 | transition: text-shadow 0.5s ease; 119 | -webkit-transition: color 0.5s ease; 120 | -webkit-transition: text-shadow 0.5s ease; 121 | -moz-transition: color 0.5s ease; 122 | -moz-transition: text-shadow 0.5s ease; 123 | -o-transition: color 0.5s ease; 124 | -o-transition: text-shadow 0.5s ease; 125 | -ms-transition: color 0.5s ease; 126 | -ms-transition: text-shadow 0.5s ease; 127 | } 128 | 129 | #main_content a:hover { 130 | color: #0069ba; 131 | text-shadow: #0090ff 0px 0px 2px; 132 | } 133 | 134 | footer a:hover { 135 | color: #43adff; 136 | text-shadow: #0090ff 0px 0px 2px; 137 | } 138 | 139 | em { 140 | font-style: italic; 141 | } 142 | 143 | strong { 144 | font-weight: bold; 145 | } 146 | 147 | img { 148 | position: relative; 149 | margin: 0 auto; 150 | max-width: 739px; 151 | padding: 5px; 152 | margin: 10px 0 10px 0; 153 | border: 1px solid #ebebeb; 154 | 155 | box-shadow: 0 0 5px #ebebeb; 156 | -webkit-box-shadow: 0 0 5px #ebebeb; 157 | -moz-box-shadow: 0 0 5px #ebebeb; 158 | -o-box-shadow: 0 0 5px #ebebeb; 159 | -ms-box-shadow: 0 0 5px #ebebeb; 160 | } 161 | 162 | pre, code { 163 | width: 100%; 164 | color: #222; 165 | background-color: #fff; 166 | 167 | font-family: Monaco, "Bitstream Vera Sans Mono", "Lucida Console", Terminal, monospace; 168 | font-size: 14px; 169 | 170 | border-radius: 2px; 171 | -moz-border-radius: 2px; 172 | -webkit-border-radius: 2px; 173 | 174 | 175 | 176 | } 177 | 178 | pre { 179 | width: 100%; 180 | padding: 10px; 181 | box-shadow: 0 0 10px rgba(0,0,0,.1); 182 | overflow: auto; 183 | } 184 | 185 | code { 186 | padding: 3px; 187 | margin: 0 3px; 188 | box-shadow: 0 0 10px rgba(0,0,0,.1); 189 | } 190 | 191 | pre code { 192 | display: block; 193 | box-shadow: none; 194 | } 195 | 196 | blockquote { 197 | color: #666; 198 | margin-bottom: 20px; 199 | padding: 0 0 0 20px; 200 | border-left: 3px solid #bbb; 201 | } 202 | 203 | ul, ol, dl { 204 | margin-bottom: 15px 205 | } 206 | 207 | ul li { 208 | list-style: inside; 209 | padding-left: 20px; 210 | } 211 | 212 | ol li { 213 | list-style: decimal inside; 214 | padding-left: 20px; 215 | } 216 | 217 | dl dt { 218 | font-weight: bold; 219 | } 220 | 221 | dl dd { 222 | padding-left: 20px; 223 | font-style: italic; 224 | } 225 | 226 | dl p { 227 | padding-left: 20px; 228 | font-style: italic; 229 | } 230 | 231 | hr { 232 | height: 1px; 233 | margin-bottom: 5px; 234 | border: none; 235 | background: url('../images/bg_hr.png') repeat-x center; 236 | } 237 | 238 | table { 239 | border: 1px solid #373737; 240 | margin-bottom: 20px; 241 | text-align: left; 242 | } 243 | 244 | th { 245 | font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif; 246 | padding: 10px; 247 | background: #373737; 248 | color: #fff; 249 | } 250 | 251 | td { 252 | padding: 10px; 253 | border: 1px solid #373737; 254 | } 255 | 256 | form { 257 | background: #f2f2f2; 258 | padding: 20px; 259 | } 260 | 261 | img { 262 | width: 100%; 263 | max-width: 100%; 264 | } 265 | 266 | /******************************************************************************* 267 | Full-Width Styles 268 | *******************************************************************************/ 269 | 270 | .outer { 271 | width: 100%; 272 | } 273 | 274 | .inner { 275 | position: relative; 276 | max-width: 640px; 277 | padding: 20px 10px; 278 | margin: 0 auto; 279 | } 280 | 281 | #forkme_banner { 282 | display: block; 283 | position: absolute; 284 | top:0; 285 | right: 10px; 286 | z-index: 10; 287 | padding: 10px 50px 10px 10px; 288 | color: #fff; 289 | background: url('../images/blacktocat.png') #0090ff no-repeat 95% 50%; 290 | font-weight: 700; 291 | box-shadow: 0 0 10px rgba(0,0,0,.5); 292 | border-bottom-left-radius: 2px; 293 | border-bottom-right-radius: 2px; 294 | } 295 | 296 | #header_wrap { 297 | background: #212121; 298 | background: -moz-linear-gradient(top, #373737, #212121); 299 | background: -webkit-linear-gradient(top, #373737, #212121); 300 | background: -ms-linear-gradient(top, #373737, #212121); 301 | background: -o-linear-gradient(top, #373737, #212121); 302 | background: linear-gradient(top, #373737, #212121); 303 | } 304 | 305 | #header_wrap .inner { 306 | padding: 50px 10px 30px 10px; 307 | } 308 | 309 | #project_title { 310 | margin: 0; 311 | color: #fff; 312 | font-size: 42px; 313 | font-weight: 700; 314 | text-shadow: #111 0px 0px 10px; 315 | font-family: 'Computer Modern'; 316 | } 317 | 318 | #project_tagline { 319 | color: #fff; 320 | font-size: 24px; 321 | font-weight: 300; 322 | background: none; 323 | text-shadow: #111 0px 0px 10px; 324 | font-family: 'Computer Modern'; 325 | } 326 | 327 | #downloads { 328 | position: absolute; 329 | width: 210px; 330 | z-index: 10; 331 | bottom: -40px; 332 | right: 0; 333 | height: 70px; 334 | background: url('../images/icon_download.png') no-repeat 0% 90%; 335 | } 336 | 337 | .zip_download_link { 338 | display: block; 339 | float: right; 340 | width: 90px; 341 | height:70px; 342 | text-indent: -5000px; 343 | overflow: hidden; 344 | background: url(../images/sprite_download.png) no-repeat bottom left; 345 | } 346 | 347 | .tar_download_link { 348 | display: block; 349 | float: right; 350 | width: 90px; 351 | height:70px; 352 | text-indent: -5000px; 353 | overflow: hidden; 354 | background: url(../images/sprite_download.png) no-repeat bottom right; 355 | margin-left: 10px; 356 | } 357 | 358 | .zip_download_link:hover { 359 | background: url(../images/sprite_download.png) no-repeat top left; 360 | } 361 | 362 | .tar_download_link:hover { 363 | background: url(../images/sprite_download.png) no-repeat top right; 364 | } 365 | 366 | #main_content_wrap { 367 | background: #f2f2f2; 368 | border-top: 1px solid #111; 369 | border-bottom: 1px solid #111; 370 | } 371 | 372 | #main_content { 373 | padding-top: 40px; 374 | } 375 | 376 | #footer_wrap { 377 | background: #212121; 378 | } 379 | 380 | 381 | 382 | /******************************************************************************* 383 | Small Device Styles 384 | *******************************************************************************/ 385 | 386 | @media screen and (max-width: 480px) { 387 | body { 388 | font-size:14px; 389 | } 390 | 391 | #downloads { 392 | display: none; 393 | } 394 | 395 | .inner { 396 | min-width: 320px; 397 | max-width: 480px; 398 | } 399 | 400 | #project_title { 401 | font-size: 32px; 402 | } 403 | 404 | h1 { 405 | font-size: 28px; 406 | } 407 | 408 | h2 { 409 | font-size: 24px; 410 | } 411 | 412 | h3 { 413 | font-size: 21px; 414 | } 415 | 416 | h4 { 417 | font-size: 18px; 418 | } 419 | 420 | h5 { 421 | font-size: 14px; 422 | } 423 | 424 | h6 { 425 | font-size: 12px; 426 | } 427 | 428 | code, pre { 429 | min-width: 320px; 430 | max-width: 480px; 431 | font-size: 11px; 432 | } 433 | 434 | } 435 | -------------------------------------------------------------------------------- /Chapter8/Chapter8_Modules.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 8: Modules\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "For Python, modules are source files that can be imported into a program. They can contain any Python structure and run when imported. They are compiled when first imported and stored in a file (with the extension \".pyc\" or \".pyo\"), have their own *namespaces* and support *Doc Strings*. They are singleton objects (only one instance is loaded into memory, which is available globally for the program).\n", 23 | "\n", 24 | "![Modules](files/bpyfd_diags6.png)\n", 25 | "\n", 26 | "The modules are located by the interpreter through the list of folders `PYTHONPATH` (sys.path), which usually includes the current directory first.\n", 27 | "\n", 28 | "The modules are loaded with the `import` statement. Thus, when using a module structure, it is necessary to identify the module. This is called absolute import." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "collapsed": false, 34 | "input": [ 35 | "import os\n", 36 | "print os.name" 37 | ], 38 | "language": "python", 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "output_type": "stream", 43 | "stream": "stdout", 44 | "text": [ 45 | "posix\n" 46 | ] 47 | } 48 | ], 49 | "prompt_number": 1 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "You can also import modules with relative form:" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "collapsed": false, 61 | "input": [ 62 | "from os import name\n", 63 | "print name" 64 | ], 65 | "language": "python", 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "output_type": "stream", 70 | "stream": "stdout", 71 | "text": [ 72 | "posix\n" 73 | ] 74 | } 75 | ], 76 | "prompt_number": 2 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "To avoid problems such as variable obfuscation, the absolute import is considered a better programming practice than the relative import.\n", 83 | "\n", 84 | "Example of module:" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "collapsed": false, 90 | "input": [ 91 | "# File calc.py\n", 92 | "\n", 93 | "# Function defined in module\n", 94 | "def average(list):\n", 95 | "\n", 96 | " return float(sum(list)) / len(list)" 97 | ], 98 | "language": "python", 99 | "metadata": {}, 100 | "outputs": [] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "Example of module usage:" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "collapsed": false, 112 | "input": [ 113 | "# Imports calc module\n", 114 | "import calc\n", 115 | "\n", 116 | "l = [23, 54, 31, 77, 12, 34]\n", 117 | "\n", 118 | "# Calls the function defined in calc\n", 119 | "print calc.average(l)" 120 | ], 121 | "language": "python", 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "output_type": "stream", 126 | "stream": "stdout", 127 | "text": [ 128 | "38.5\n" 129 | ] 130 | } 131 | ], 132 | "prompt_number": 4 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "The main module of a program has the variable `__name__` equals to `__main__`, thus it is possible to test if the main module:" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "collapsed": false, 144 | "input": [ 145 | "if __name__ == \"__main__\":\n", 146 | " # Code here will only be run \n", 147 | " # if it is the main module\n", 148 | " # and not when it is imported by another program\n", 149 | " pass" 150 | ], 151 | "language": "python", 152 | "metadata": {}, 153 | "outputs": [], 154 | "prompt_number": 6 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "That way it is easy to turn a program into a module.\n", 161 | "\n", 162 | "Another module example:" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "collapsed": false, 168 | "input": [ 169 | "\"\"\"\n", 170 | "modutils => utility routines for modules\n", 171 | "\"\"\"\n", 172 | "\n", 173 | "import os.path\n", 174 | "import sys\n", 175 | "import glob\n", 176 | "\n", 177 | "def find(txt):\n", 178 | " \"\"\"find modules with name containing the parameter\n", 179 | " \"\"\"\n", 180 | "\n", 181 | " resp = []\n", 182 | "\n", 183 | " for path in sys.path:\n", 184 | " mods = glob.glob('%s/*.py' % path)\n", 185 | "\n", 186 | " for mod in mods:\n", 187 | " if txt in os.path.basename(mod):\n", 188 | " resp.append(mod)\n", 189 | "\n", 190 | " return resp" 191 | ], 192 | "language": "python", 193 | "metadata": {}, 194 | "outputs": [] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "Example module use:" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "collapsed": false, 206 | "input": [ 207 | "from os.path import getsize, getmtime\n", 208 | "from time import localtime, asctime\n", 209 | "\n", 210 | "import modutils\n", 211 | "\n", 212 | "mods = modutils.find('xml')\n", 213 | "\n", 214 | "for mod in mods:\n", 215 | "\n", 216 | " tm = asctime(localtime(getmtime(mod)))\n", 217 | " kb = getsize(mod) / 1024\n", 218 | " print '%s: (%d kbytes, %s)' % (mod, kb, tm)" 219 | ], 220 | "language": "python", 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "output_type": "stream", 225 | "stream": "stdout", 226 | "text": [ 227 | "/usr/lib/python2.7/xmlrpclib.py: (50 kbytes, Fri Apr 19 16:20:45 2013)\n", 228 | "/usr/lib/python2.7/xmllib.py: (34 kbytes, Fri Apr 19 16:20:45 2013)\n", 229 | "/usr/lib/python2.7/dist-packages/libxml2.py: (335 kbytes, Wed May 1 14:19:10 2013)\n", 230 | "/usr/lib/python2.7/dist-packages/drv_libxml2.py: (14 kbytes, Wed May 1 14:19:10 2013)\n" 231 | ] 232 | } 233 | ], 234 | "prompt_number": 7 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "Splitting programs into modules makes it easy to reuse and locate faults in the code." 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "collapsed": false, 246 | "input": [], 247 | "language": "python", 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "html": [ 252 | "\n", 300 | "" 315 | ], 316 | "output_type": "pyout", 317 | "prompt_number": 1, 318 | "text": [ 319 | "" 320 | ] 321 | } 322 | ], 323 | "prompt_number": 1 324 | } 325 | ], 326 | "metadata": {} 327 | } 328 | ] 329 | } -------------------------------------------------------------------------------- /Chapter8/.ipynb_checkpoints/Chapter8_Modules-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 8: Modules\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "For Python, modules are source files that can be imported into a program. They can contain any Python structure and run when imported. They are compiled when first imported and stored in file (with file extension \".pyc\" or \".pyo\"), have their own *namespaces* and support *Doc Strings*. They are singleton objects (only one instance is loaded into memory, which is available globally for the program).\n", 23 | "\n", 24 | "![Modules](files/bpyfd_diags6.png)\n", 25 | "\n", 26 | "The modules are located by the interpreter through the list of folders `PYTHONPATH` (sys.path), which usually includes the current directory first.\n", 27 | "\n", 28 | "The modules are loaded with the `import` statement. Thus, when using a module structure, it is necessary to identify the module. This is called absolute import." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "collapsed": false, 34 | "input": [ 35 | "import os\n", 36 | "print os.name" 37 | ], 38 | "language": "python", 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "output_type": "stream", 43 | "stream": "stdout", 44 | "text": [ 45 | "posix\n" 46 | ] 47 | } 48 | ], 49 | "prompt_number": 1 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "You can also import modules with relative form:" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "collapsed": false, 61 | "input": [ 62 | "from os import name\n", 63 | "print name" 64 | ], 65 | "language": "python", 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "output_type": "stream", 70 | "stream": "stdout", 71 | "text": [ 72 | "posix\n" 73 | ] 74 | } 75 | ], 76 | "prompt_number": 2 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "By avoiding problems such as variable obfuscation, the absolute import is considered a better programming practice than the relative import.\n", 83 | "\n", 84 | "Example of module:" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "collapsed": false, 90 | "input": [ 91 | "# File calc.py\n", 92 | "\n", 93 | "# Function defined in module\n", 94 | "def average(list):\n", 95 | "\n", 96 | " return float(sum(list)) / len(list)" 97 | ], 98 | "language": "python", 99 | "metadata": {}, 100 | "outputs": [] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "Exemple of module usage:" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "collapsed": false, 112 | "input": [ 113 | "# Imports calc module\n", 114 | "import calc\n", 115 | "\n", 116 | "l = [23, 54, 31, 77, 12, 34]\n", 117 | "\n", 118 | "# Calls the function defined in calc\n", 119 | "print calc.media(l)" 120 | ], 121 | "language": "python", 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "output_type": "stream", 126 | "stream": "stdout", 127 | "text": [ 128 | "38.5\n" 129 | ] 130 | } 131 | ], 132 | "prompt_number": 4 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "The main module of a program has the variable `__name__` equals to `__main__`, thus it is possible to test if the main module:" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "collapsed": false, 144 | "input": [ 145 | "if __name__ == \"__main__\":\n", 146 | " # Code here will only be runned \n", 147 | " # if it is the main module\n", 148 | " # and not when it is imported by another program\n", 149 | " pass" 150 | ], 151 | "language": "python", 152 | "metadata": {}, 153 | "outputs": [], 154 | "prompt_number": 6 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "That way it is easy to turn a program into a module.\n", 161 | "\n", 162 | "Another module example:" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "collapsed": false, 168 | "input": [ 169 | "\"\"\"\n", 170 | "modutils => utility routines for modules\n", 171 | "\"\"\"\n", 172 | "\n", 173 | "import os.path\n", 174 | "import sys\n", 175 | "import glob\n", 176 | "\n", 177 | "def find(txt):\n", 178 | " \"\"\"find modules with name containing the parameter\n", 179 | " \"\"\"\n", 180 | "\n", 181 | " resp = []\n", 182 | "\n", 183 | " for path in sys.path:\n", 184 | " mods = glob.glob('%s/*.py' % path)\n", 185 | "\n", 186 | " for mod in mods:\n", 187 | " if txt in os.path.basename(mod):\n", 188 | " resp.append(mod)\n", 189 | "\n", 190 | " return resp" 191 | ], 192 | "language": "python", 193 | "metadata": {}, 194 | "outputs": [] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "Exemplo de uso do m\u00f3dulo:" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "collapsed": false, 206 | "input": [ 207 | "from os.path import getsize, getmtime\n", 208 | "from time import localtime, asctime\n", 209 | "\n", 210 | "import modutils\n", 211 | "\n", 212 | "mods = modutils.find('xml')\n", 213 | "\n", 214 | "for mod in mods:\n", 215 | "\n", 216 | " tm = asctime(localtime(getmtime(mod)))\n", 217 | " kb = getsize(mod) / 1024\n", 218 | " print '%s: (%d kbytes, %s)' % (mod, kb, tm)" 219 | ], 220 | "language": "python", 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "output_type": "stream", 225 | "stream": "stdout", 226 | "text": [ 227 | "/usr/lib/python2.7/xmlrpclib.py: (50 kbytes, Fri Apr 19 16:20:45 2013)\n", 228 | "/usr/lib/python2.7/xmllib.py: (34 kbytes, Fri Apr 19 16:20:45 2013)\n", 229 | "/usr/lib/python2.7/dist-packages/libxml2.py: (335 kbytes, Wed May 1 14:19:10 2013)\n", 230 | "/usr/lib/python2.7/dist-packages/drv_libxml2.py: (14 kbytes, Wed May 1 14:19:10 2013)\n" 231 | ] 232 | } 233 | ], 234 | "prompt_number": 7 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "Splitting programs into modules makes it easy to reuse and locating faults in the code." 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "collapsed": false, 246 | "input": [], 247 | "language": "python", 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "html": [ 252 | "\n", 300 | "" 315 | ], 316 | "output_type": "pyout", 317 | "prompt_number": 1, 318 | "text": [ 319 | "" 320 | ] 321 | } 322 | ], 323 | "prompt_number": 1 324 | } 325 | ], 326 | "metadata": {} 327 | } 328 | ] 329 | } -------------------------------------------------------------------------------- /Chapter7/Chapter7_Documentation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "Chapter7_Documentation" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 7: Documentation\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "PyDOC is a documentation tool for Python. It can be used both to access to the documentation of the modules that come with Python, and the documentation of third party modules.\n", 23 | "\n", 24 | "In Windows, go to the icon \"Module Docs\" documentation of the standard library and \"Python Manuals\" to view the tutorial, referrals, and other more extensive documentation.\n", 25 | "\n", 26 | "To use PyDOC in Linux:\n", 27 | "\n", 28 | " pydoc ./modulo.py\n", 29 | "\n", 30 | "To show the documentation of `modulo.py` in the current directory.\n", 31 | "\n", 32 | "In Linux, the documentation of libraries can be seen throught the *browser* by using:\n", 33 | "\n", 34 | " pydoc -p 8000\n", 35 | "\n", 36 | "At the address http://localhost:8000/.\n", 37 | "To run the graphical version of PyDOC type:\n", 38 | "\n", 39 | " pydoc -g\n", 40 | "\n", 41 | "PyDOC uses the module *Doc Strings* to generate the documentation.\n", 42 | "\n", 43 | "Besides that, it is also possible to consult the documentation on the interpreter itself, with the function `help()`.\n", 44 | "\n", 45 | "Example:" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "collapsed": false, 51 | "input": [ 52 | "help(list)" 53 | ], 54 | "language": "python", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "output_type": "stream", 59 | "stream": "stdout", 60 | "text": [ 61 | "Help on class list in module __builtin__:\n", 62 | "\n", 63 | "class list(object)\n", 64 | " | list() -> new empty list\n", 65 | " | list(iterable) -> new list initialized from iterable's items\n", 66 | " | \n", 67 | " | Methods defined here:\n", 68 | " | \n", 69 | " | __add__(...)\n", 70 | " | x.__add__(y) <==> x+y\n", 71 | " | \n", 72 | " | __contains__(...)\n", 73 | " | x.__contains__(y) <==> y in x\n", 74 | " | \n", 75 | " | __delitem__(...)\n", 76 | " | x.__delitem__(y) <==> del x[y]\n", 77 | " | \n", 78 | " | __delslice__(...)\n", 79 | " | x.__delslice__(i, j) <==> del x[i:j]\n", 80 | " | \n", 81 | " | Use of negative indices is not supported.\n", 82 | " | \n", 83 | " | __eq__(...)\n", 84 | " | x.__eq__(y) <==> x==y\n", 85 | " | \n", 86 | " | __ge__(...)\n", 87 | " | x.__ge__(y) <==> x>=y\n", 88 | " | \n", 89 | " | __getattribute__(...)\n", 90 | " | x.__getattribute__('name') <==> x.name\n", 91 | " | \n", 92 | " | __getitem__(...)\n", 93 | " | x.__getitem__(y) <==> x[y]\n", 94 | " | \n", 95 | " | __getslice__(...)\n", 96 | " | x.__getslice__(i, j) <==> x[i:j]\n", 97 | " | \n", 98 | " | Use of negative indices is not supported.\n", 99 | " | \n", 100 | " | __gt__(...)\n", 101 | " | x.__gt__(y) <==> x>y\n", 102 | " | \n", 103 | " | __iadd__(...)\n", 104 | " | x.__iadd__(y) <==> x+=y\n", 105 | " | \n", 106 | " | __imul__(...)\n", 107 | " | x.__imul__(y) <==> x*=y\n", 108 | " | \n", 109 | " | __init__(...)\n", 110 | " | x.__init__(...) initializes x; see help(type(x)) for signature\n", 111 | " | \n", 112 | " | __iter__(...)\n", 113 | " | x.__iter__() <==> iter(x)\n", 114 | " | \n", 115 | " | __le__(...)\n", 116 | " | x.__le__(y) <==> x<=y\n", 117 | " | \n", 118 | " | __len__(...)\n", 119 | " | x.__len__() <==> len(x)\n", 120 | " | \n", 121 | " | __lt__(...)\n", 122 | " | x.__lt__(y) <==> x x*n\n", 126 | " | \n", 127 | " | __ne__(...)\n", 128 | " | x.__ne__(y) <==> x!=y\n", 129 | " | \n", 130 | " | __repr__(...)\n", 131 | " | x.__repr__() <==> repr(x)\n", 132 | " | \n", 133 | " | __reversed__(...)\n", 134 | " | L.__reversed__() -- return a reverse iterator over the list\n", 135 | " | \n", 136 | " | __rmul__(...)\n", 137 | " | x.__rmul__(n) <==> n*x\n", 138 | " | \n", 139 | " | __setitem__(...)\n", 140 | " | x.__setitem__(i, y) <==> x[i]=y\n", 141 | " | \n", 142 | " | __setslice__(...)\n", 143 | " | x.__setslice__(i, j, y) <==> x[i:j]=y\n", 144 | " | \n", 145 | " | Use of negative indices is not supported.\n", 146 | " | \n", 147 | " | __sizeof__(...)\n", 148 | " | L.__sizeof__() -- size of L in memory, in bytes\n", 149 | " | \n", 150 | " | append(...)\n", 151 | " | L.append(object) -- append object to end\n", 152 | " | \n", 153 | " | count(...)\n", 154 | " | L.count(value) -> integer -- return number of occurrences of value\n", 155 | " | \n", 156 | " | extend(...)\n", 157 | " | L.extend(iterable) -- extend list by appending elements from the iterable\n", 158 | " | \n", 159 | " | index(...)\n", 160 | " | L.index(value, [start, [stop]]) -> integer -- return first index of value.\n", 161 | " | Raises ValueError if the value is not present.\n", 162 | " | \n", 163 | " | insert(...)\n", 164 | " | L.insert(index, object) -- insert object before index\n", 165 | " | \n", 166 | " | pop(...)\n", 167 | " | L.pop([index]) -> item -- remove and return item at index (default last).\n", 168 | " | Raises IndexError if list is empty or index is out of range.\n", 169 | " | \n", 170 | " | remove(...)\n", 171 | " | L.remove(value) -- remove first occurrence of value.\n", 172 | " | Raises ValueError if the value is not present.\n", 173 | " | \n", 174 | " | reverse(...)\n", 175 | " | L.reverse() -- reverse *IN PLACE*\n", 176 | " | \n", 177 | " | sort(...)\n", 178 | " | L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n", 179 | " | cmp(x, y) -> -1, 0, 1\n", 180 | " | \n", 181 | " | ----------------------------------------------------------------------\n", 182 | " | Data and other attributes defined here:\n", 183 | " | \n", 184 | " | __hash__ = None\n", 185 | " | \n", 186 | " | __new__ = \n", 187 | " | T.__new__(S, ...) -> a new object with type S, a subtype of T\n", 188 | "\n" 189 | ] 190 | } 191 | ], 192 | "prompt_number": 1 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "Which shows the Python list documentation." 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "collapsed": false, 204 | "input": [], 205 | "language": "python", 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "html": [ 210 | "\n", 258 | "" 273 | ], 274 | "output_type": "pyout", 275 | "prompt_number": 1, 276 | "text": [ 277 | "" 278 | ] 279 | } 280 | ], 281 | "prompt_number": 1 282 | } 283 | ], 284 | "metadata": {} 285 | } 286 | ] 287 | } -------------------------------------------------------------------------------- /Chapter13/Chapter13_Exceptions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 13: Exceptions\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "When a failure occurs in the program (such as division by zero, for example) at runtime, an exception is generated. If the exception is not handled, it will be propagated through function calls to the main program module, interrupting execution." 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "collapsed": false, 28 | "input": [ 29 | "print 1/0" 30 | ], 31 | "language": "python", 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "ename": "ZeroDivisionError", 36 | "evalue": "integer division or modulo by zero", 37 | "output_type": "pyerr", 38 | "traceback": [ 39 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 40 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 41 | "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero" 42 | ] 43 | } 44 | ], 45 | "prompt_number": 1 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "The *try* instruction allows exception handling in Python. If an exception occurs in a block marked by *try*, it is possible to handle the exception through the instruction *except*. It is possible to have many *except* blocks for the same *try* block." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "collapsed": false, 57 | "input": [ 58 | "try:\n", 59 | " print 1/0\n", 60 | "except ZeroDivisionError:\n", 61 | " print 'Error trying to divide by zero.'" 62 | ], 63 | "language": "python", 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "output_type": "stream", 68 | "stream": "stdout", 69 | "text": [ 70 | "Error trying to divide by zero.\n" 71 | ] 72 | } 73 | ], 74 | "prompt_number": 1 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "If *except* receives the name of an exception, only that exception will be handled. If no exception name is passed as a parameter, all exceptions will be handled.\n", 81 | "\n", 82 | "Example:" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "collapsed": false, 88 | "input": [ 89 | "import traceback\n", 90 | "\n", 91 | "# Try to get a file name\n", 92 | "try:\n", 93 | " fn = raw_input('Nome do arquivo: ').strip()\n", 94 | "\n", 95 | " # Numbering lines\n", 96 | " for i, s in enumerate(file(fn)):\n", 97 | " print i + 1, s,\n", 98 | "\n", 99 | "# If an error happens\n", 100 | "except:\n", 101 | "\n", 102 | " # Show it on the screen\n", 103 | " trace = traceback.format_exc()\n", 104 | "\n", 105 | " # And save it on a file\n", 106 | " print 'An error happened:\\n', trace\n", 107 | " file('trace.log', 'a').write(trace)\n", 108 | "\n", 109 | " # end the program\n", 110 | " raise SystemExit" 111 | ], 112 | "language": "python", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "stream": "stdout", 119 | "text": [ 120 | "Nome do arquivo: test\n" 121 | ] 122 | }, 123 | { 124 | "ename": "SystemExit", 125 | "evalue": "", 126 | "output_type": "pyerr", 127 | "traceback": [ 128 | "An exception has occurred, use %tb to see the full traceback.\n", 129 | "\u001b[1;31mSystemExit\u001b[0m\n" 130 | ] 131 | }, 132 | { 133 | "output_type": "stream", 134 | "stream": "stdout", 135 | "text": [ 136 | "An error happened:\n", 137 | "Traceback (most recent call last):\n", 138 | " File \"\", line 8, in \n", 139 | " for i, s in enumerate(file(fn)):\n", 140 | "IOError: [Errno 2] No such file or directory: 'test'\n", 141 | "\n" 142 | ] 143 | }, 144 | { 145 | "output_type": "stream", 146 | "stream": "stderr", 147 | "text": [ 148 | "To exit: use 'exit', 'quit', or Ctrl-D.\n" 149 | ] 150 | } 151 | ], 152 | "prompt_number": 2 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "The module *traceback* offers functions for dealing with error messages. The function format_exc() returns the output of the last exception formatted in a *string*.\n", 159 | "\n", 160 | "The handling of exceptions may have an *else* block, which will be executed when no exception occurs and a *finally* block, which will be executed anyway, whether an exception occurred or not. New types of exceptions may be defined through inheritance of the class *Exception*.\n", 161 | "\n", 162 | "Since version 2.6, the instruction *with* is available, that may replace the combination of *try / finally* in many situations. It is possible to define an object that will be used during the *with* block execution. The object will support the context management protocol, which means that it will need to have an `__enter__()` method, which will be executed at the beginning of the block, and another called `__exit__()`, which will be called at the end of the block.\n", 163 | "\n", 164 | "Example:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "collapsed": false, 170 | "input": [ 171 | "import random\n", 172 | "\n", 173 | "# Creates a file with 25 random numbers\n", 174 | "with file('temp.txt', 'w') as temp:\n", 175 | " for y in range(5):\n", 176 | " for x in range(5):\n", 177 | " # \"print >> \" records command output on the file\n", 178 | " print >> temp, '%.2f' % random.random(),\n", 179 | " print >> temp\n", 180 | "\n", 181 | "# Shows file content\n", 182 | "with file('temp.txt') as temp:\n", 183 | " for i in temp:\n", 184 | " print i,\n", 185 | "\n", 186 | "# Out of the blocks, the file will be closed\n", 187 | "# The following command generates an exception ValueError: I/O operation on closed file\n", 188 | "print >> temp" 189 | ], 190 | "language": "python", 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "ename": "ValueError", 195 | "evalue": "I/O operation on closed file", 196 | "output_type": "pyerr", 197 | "traceback": [ 198 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 199 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;31m# Out of the blocks, the file will be closed\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;31m# This generates an exception ValueError: I/O operation on closed file\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;33m>>\u001b[0m \u001b[0mtemp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 200 | "\u001b[1;31mValueError\u001b[0m: I/O operation on closed file" 201 | ] 202 | }, 203 | { 204 | "output_type": "stream", 205 | "stream": "stdout", 206 | "text": [ 207 | "0.06 0.66 0.01 0.94 0.58\n", 208 | "0.93 0.72 0.14 0.88 0.50\n", 209 | "0.74 0.34 0.64 0.51 0.95\n", 210 | "0.35 0.87 0.10 0.02 0.21\n", 211 | "0.66 0.92 0.66 0.46 0.82\n" 212 | ] 213 | } 214 | ], 215 | "prompt_number": 3 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "With file closed at the end of the block, the record attempt generates an exception.\n" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "collapsed": false, 227 | "input": [], 228 | "language": "python", 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "html": [ 233 | "\n", 281 | "" 296 | ], 297 | "output_type": "pyout", 298 | "prompt_number": 1, 299 | "text": [ 300 | "" 301 | ] 302 | } 303 | ], 304 | "prompt_number": 1 305 | } 306 | ], 307 | "metadata": {} 308 | } 309 | ] 310 | } -------------------------------------------------------------------------------- /Chapter13/.ipynb_checkpoints/Chapter13_Exceptions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 13: Exceptions\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "When a failure occurs in the program (such as division by zero, for example) at runtime, an exception is generated. If the exception is not handled, it will be propagated through function calls to the main program module, interrupting execution." 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "collapsed": false, 28 | "input": [ 29 | "print 1/0" 30 | ], 31 | "language": "python", 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "ename": "ZeroDivisionError", 36 | "evalue": "integer division or modulo by zero", 37 | "output_type": "pyerr", 38 | "traceback": [ 39 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 40 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 41 | "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero" 42 | ] 43 | } 44 | ], 45 | "prompt_number": 1 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "The *try* instruction allows exception handling in Python. If an exception occur in a block marked by *try*, it is possible to tread the exception through the instruction *except*. It is possible to have many *except* blocks for the same *try* block." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "collapsed": false, 57 | "input": [ 58 | "try:\n", 59 | " print 1/0\n", 60 | "except ZeroDivisionError:\n", 61 | " print 'Error trying to divide by zero.'" 62 | ], 63 | "language": "python", 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "output_type": "stream", 68 | "stream": "stdout", 69 | "text": [ 70 | "Error trying to divide by zero.\n" 71 | ] 72 | } 73 | ], 74 | "prompt_number": 1 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "If *except* receives the name of an exception, only that exception will be handled. If no exception name is passed as parameter, all exceptions will be handled.\n", 81 | "\n", 82 | "Example:" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "collapsed": false, 88 | "input": [ 89 | "import traceback\n", 90 | "\n", 91 | "# Try to get a file name\n", 92 | "try:\n", 93 | " fn = raw_input('Nome do arquivo: ').strip()\n", 94 | "\n", 95 | " # Numbering lines\n", 96 | " for i, s in enumerate(file(fn)):\n", 97 | " print i + 1, s,\n", 98 | "\n", 99 | "# If an error happens\n", 100 | "except:\n", 101 | "\n", 102 | " # Show it on the screen\n", 103 | " trace = traceback.format_exc()\n", 104 | "\n", 105 | " # And save it on a file\n", 106 | " print 'An error happened:\\n', trace\n", 107 | " file('trace.log', 'a').write(trace)\n", 108 | "\n", 109 | " # end the program\n", 110 | " raise SystemExit" 111 | ], 112 | "language": "python", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "stream": "stdout", 119 | "text": [ 120 | "Nome do arquivo: test\n" 121 | ] 122 | }, 123 | { 124 | "ename": "SystemExit", 125 | "evalue": "", 126 | "output_type": "pyerr", 127 | "traceback": [ 128 | "An exception has occurred, use %tb to see the full traceback.\n", 129 | "\u001b[1;31mSystemExit\u001b[0m\n" 130 | ] 131 | }, 132 | { 133 | "output_type": "stream", 134 | "stream": "stdout", 135 | "text": [ 136 | "An error happened:\n", 137 | "Traceback (most recent call last):\n", 138 | " File \"\", line 8, in \n", 139 | " for i, s in enumerate(file(fn)):\n", 140 | "IOError: [Errno 2] No such file or directory: 'test'\n", 141 | "\n" 142 | ] 143 | }, 144 | { 145 | "output_type": "stream", 146 | "stream": "stderr", 147 | "text": [ 148 | "To exit: use 'exit', 'quit', or Ctrl-D.\n" 149 | ] 150 | } 151 | ], 152 | "prompt_number": 2 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "The module *traceback* offers functions for dealing with error messages. The function format_exc() returns the output of the last exception formatted in a *string*.\n", 159 | "\n", 160 | "The handling of exceptions may have an *else* block, which will be executed when no exception occur and a *finally* block, which will be executed anyway, exception occurred or not. New types of exceptions may be defined through inheritance of the class *Exception*.\n", 161 | "\n", 162 | "Since version 2.6, it is available the instruction *with*, that may substitute the combination *try / finally* in many situations. It is possible to define an object that will be used during the *with* block execution. The object will support context management protocol, which means that it will need to have an `__enter__()` method, which will be executed on the beginning of the block, and another called `__exit__()`, which will be called at the end of the block.\n", 163 | "\n", 164 | "Example:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "collapsed": false, 170 | "input": [ 171 | "import random\n", 172 | "\n", 173 | "# Creates a file with 25 random numbers\n", 174 | "with file('temp.txt', 'w') as temp:\n", 175 | " for y in range(5):\n", 176 | " for x in range(5):\n", 177 | " # \"print >> \" records command output on the file\n", 178 | " print >> temp, '%.2f' % random.random(),\n", 179 | " print >> temp\n", 180 | "\n", 181 | "# Shows file content\n", 182 | "with file('temp.txt') as temp:\n", 183 | " for i in temp:\n", 184 | " print i,\n", 185 | "\n", 186 | "# Out of the blocks, the file will be closed\n", 187 | "# The following command generates an exception ValueError: I/O operation on closed file\n", 188 | "print >> temp" 189 | ], 190 | "language": "python", 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "ename": "ValueError", 195 | "evalue": "I/O operation on closed file", 196 | "output_type": "pyerr", 197 | "traceback": [ 198 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 199 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;31m# Out of the blocks, the file will be closed\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;31m# This generates an exception ValueError: I/O operation on closed file\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;33m>>\u001b[0m \u001b[0mtemp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 200 | "\u001b[1;31mValueError\u001b[0m: I/O operation on closed file" 201 | ] 202 | }, 203 | { 204 | "output_type": "stream", 205 | "stream": "stdout", 206 | "text": [ 207 | "0.06 0.66 0.01 0.94 0.58\n", 208 | "0.93 0.72 0.14 0.88 0.50\n", 209 | "0.74 0.34 0.64 0.51 0.95\n", 210 | "0.35 0.87 0.10 0.02 0.21\n", 211 | "0.66 0.92 0.66 0.46 0.82\n" 212 | ] 213 | } 214 | ], 215 | "prompt_number": 3 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "With file closed at the end of the block, the record attempt generates an exception.\n" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "collapsed": false, 227 | "input": [], 228 | "language": "python", 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "html": [ 233 | "\n", 281 | "" 296 | ], 297 | "output_type": "pyout", 298 | "prompt_number": 1, 299 | "text": [ 300 | "" 301 | ] 302 | } 303 | ], 304 | "prompt_number": 1 305 | } 306 | ], 307 | "metadata": {} 308 | } 309 | ] 310 | } -------------------------------------------------------------------------------- /Chapter6/Chapter6_Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "Chapter6_Functions" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "orig_nbformat": 2, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "markdown", 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "===================================\n", 16 | "First Edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 6: Functions\n", 20 | "=============================\n", 21 | "_____________________________\n", 22 | "Functions are blocks of code identified by a name, which can receive predetermined parameters.\n", 23 | "\n", 24 | "In Python, functions:\n", 25 | "\n", 26 | "+ Can return objects or not.\n", 27 | "+ Accept *Doc Strings*.\n", 28 | "+ Accept optional parameters (with *defaults* ). If no parameter is passed, it will be equal to the *default* defined in the function.\n", 29 | "+ Accepts parameters to be passed by name. In this case, the order in which the parameters were passed does not matter.\n", 30 | "+ Have their own namespace (local scope), and therefore may obscure definitions of global scope.\n", 31 | "+ Can have their properties changed (usually by decorators).\n", 32 | "\n", 33 | "*Doc Strings* are strings that are attached to a\u00a0Python structure. In functions, *Doc strings* are placed within the body of the function, usually at the beginning. The goal of *Doc Strings* is to be used as documentation for this structure.\n", 34 | "\n", 35 | "Syntax:\n", 36 | "\n", 37 | " def func(parameter1, parameter2=default_value):\n", 38 | " \"\"\"\n", 39 | " Doc String\n", 40 | " \"\"\"\n", 41 | " \n", 42 | " return value\n", 43 | "\n", 44 | "The parameters with *default* value must be declared after the ones without *default* value.\n", 45 | "\n", 46 | "Example (factorial with recursion):" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "collapsed": false, 52 | "input": [ 53 | "# Fatorial implemented with recursion\n", 54 | "\n", 55 | "def factorial(num):\n", 56 | "\n", 57 | " if num <= 1:\n", 58 | " return 1\n", 59 | " else:\n", 60 | " return(num * factorial(num - 1))\n", 61 | "\n", 62 | "# Testing factorial()\n", 63 | "print factorial(5)" 64 | ], 65 | "language": "python", 66 | "outputs": [ 67 | { 68 | "output_type": "stream", 69 | "stream": "stdout", 70 | "text": [ 71 | "120\n" 72 | ] 73 | } 74 | ], 75 | "prompt_number": 2 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "source": [ 80 | "Example (factorial without recursion):" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "collapsed": false, 86 | "input": [ 87 | "def fatorial(n):\n", 88 | "\n", 89 | " n = n if n > 1 else 1\n", 90 | " j = 1\n", 91 | " for i in range(1, n + 1):\n", 92 | " j = j * i\n", 93 | " return j\n", 94 | "\n", 95 | "# Testing...\n", 96 | "for i in range(1, 6):\n", 97 | " print i, '->', fatorial(i)" 98 | ], 99 | "language": "python", 100 | "outputs": [ 101 | { 102 | "output_type": "stream", 103 | "stream": "stdout", 104 | "text": [ 105 | "1 -> 1\n", 106 | "2 -> 2\n", 107 | "3 -> 6\n", 108 | "4 -> 24\n", 109 | "5 -> 120\n" 110 | ] 111 | } 112 | ], 113 | "prompt_number": 3 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "source": [ 118 | "Example (Fibonacci series with recursion):" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "collapsed": false, 124 | "input": [ 125 | "def fib(n):\n", 126 | " \"\"\"Fibonacci:\n", 127 | " fib(n) = fib(n - 1) + fib(n - 2) se n > 1\n", 128 | " fib(n) = 1 se n <= 1\n", 129 | " \"\"\"\n", 130 | " if n > 1:\n", 131 | " return fib(n - 1) + fib(n - 2)\n", 132 | " else:\n", 133 | " return 1\n", 134 | "\n", 135 | "# Show Fibonacci from 1 to 5\n", 136 | "for i in [1, 2, 3, 4, 5]:\n", 137 | " print i, '=>', fib(i)" 138 | ], 139 | "language": "python", 140 | "outputs": [ 141 | { 142 | "output_type": "stream", 143 | "stream": "stdout", 144 | "text": [ 145 | "1 => 1\n", 146 | "2 => 2\n", 147 | "3 => 3\n", 148 | "4 => 5\n", 149 | "5 => 8\n" 150 | ] 151 | } 152 | ], 153 | "prompt_number": 4 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "source": [ 158 | "Example (Fibonacci series without recursion):" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "collapsed": false, 164 | "input": [ 165 | "def fib(n):\n", 166 | " \"\"\"Fibonacci:\n", 167 | " fib(n) = fib(n - 1) + fib(n - 2) se n > 1\n", 168 | " fib(n) = 1 se n <= 1\n", 169 | " \"\"\"\n", 170 | " \n", 171 | " # the first two values\n", 172 | " l = [1, 1]\n", 173 | " \n", 174 | " # Calculating the others\n", 175 | " for i in range(2, n + 1):\n", 176 | " l.append(l[i -1] + l[i - 2])\n", 177 | " \n", 178 | " return l[n]\n", 179 | "\n", 180 | "# Show Fibonacci from 1 to 5\n", 181 | "for i in [1, 2, 3, 4, 5]:\n", 182 | " print i, '=>', fib(i)" 183 | ], 184 | "language": "python", 185 | "outputs": [ 186 | { 187 | "output_type": "stream", 188 | "stream": "stdout", 189 | "text": [ 190 | "1 => 1\n", 191 | "2 => 2\n", 192 | "3 => 3\n", 193 | "4 => 5\n", 194 | "5 => 8\n" 195 | ] 196 | } 197 | ], 198 | "prompt_number": 5 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "source": [ 203 | "Example (RGB conversion):" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "collapsed": false, 209 | "input": [ 210 | "def rgb_html(r=0, g=0, b=0):\n", 211 | " \"\"\"Converts R, G, B to #RRGGBB\"\"\"\n", 212 | "\n", 213 | " return '#%02x%02x%02x' % (r, g, b)\n", 214 | "\n", 215 | "def html_rgb(color='#000000'):\n", 216 | " \"\"\"Converts #RRGGBB em R, G, B\"\"\"\n", 217 | "\n", 218 | " if color.startswith('#'): color = color[1:]\n", 219 | "\n", 220 | " r = int(color[:2], 16)\n", 221 | " g = int(color[2:4], 16)\n", 222 | " b = int(color[4:], 16)\n", 223 | "\n", 224 | " return r, g, b # a sequence\n", 225 | "\n", 226 | "print rgb_html(200, 200, 255)\n", 227 | "print rgb_html(b=200, g=200, r=255) # what's happened? \n", 228 | "print html_rgb('#c8c8ff')" 229 | ], 230 | "language": "python", 231 | "outputs": [ 232 | { 233 | "output_type": "stream", 234 | "stream": "stdout", 235 | "text": [ 236 | "#c8c8ff\n", 237 | "#ffc8c8\n", 238 | "(200, 200, 255)\n" 239 | ] 240 | } 241 | ], 242 | "prompt_number": 6 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "source": [ 247 | "Observations:\n", 248 | "\n", 249 | "+ The\u00a0 arguments with default value must come last, after the non-default arguments.\n", 250 | "+ The default value for a parameter is calculated when the function is defined.\n", 251 | "+ The arguments passed without an identifier are received by the function in the form of a list.\n", 252 | "+ The arguments passed to the function with an identifier are received in the form of a dictionary.\n", 253 | "+ The parameters passed to the function with an identifier should come at the end of the parameter list.\n", 254 | "\n", 255 | "Example of how to get all parameters:" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "collapsed": false, 261 | "input": [ 262 | "# *args - arguments without name (list)\n", 263 | "# **kargs - arguments with name (ditcionary)\n", 264 | "\n", 265 | "def func(*args, **kargs):\n", 266 | " print args\n", 267 | " print kargs\n", 268 | "\n", 269 | "func('weigh', 10, unit='k')" 270 | ], 271 | "language": "python", 272 | "outputs": [ 273 | { 274 | "output_type": "stream", 275 | "stream": "stdout", 276 | "text": [ 277 | "('weigh', 10)\n", 278 | "{'unit': 'k'}\n" 279 | ] 280 | } 281 | ], 282 | "prompt_number": 7 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "source": [ 287 | "In the example, `kargs` will receive the named arguments and `args` will receive the others.\n", 288 | "\n", 289 | "The interpreter has some *builtin* functions defined, including `sorted()`, which orders sequences, and `cmp()`, which makes comparisons between two arguments and returns -1 if the first element is greater, 0 (zero) if they are equal, or 1 if the latter is higher. This function is used by the routine of ordering, a behavior that can be modified.\n", 290 | "\n", 291 | "Example:" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "collapsed": false, 297 | "input": [ 298 | "data = [(4, 3), (5, 1), (7, 2), (9, 0)]\n", 299 | "\n", 300 | "# Comparing by the last element\n", 301 | "def _cmp(x, y):\n", 302 | " return cmp(x[-1], y[-1])\n", 303 | "\n", 304 | "print 'List:', data\n", 305 | "\n", 306 | "# Ordering using _cmp()\n", 307 | "print 'Ordered:', sorted(data, _cmp)" 308 | ], 309 | "language": "python", 310 | "outputs": [ 311 | { 312 | "output_type": "stream", 313 | "stream": "stdout", 314 | "text": [ 315 | "List: [(4, 3), (5, 1), (7, 2), (9, 0)]\n", 316 | "Ordered: [(9, 0), (5, 1), (7, 2), (4, 3)]\n" 317 | ] 318 | } 319 | ], 320 | "prompt_number": 8 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "source": [ 325 | "Python also has a *builtin* function `eval()`, which evaluates code (source or object) and returns the value.\n", 326 | "\n", 327 | "Example:" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "collapsed": false, 333 | "input": [ 334 | "print eval('12. / 2 + 3.3')" 335 | ], 336 | "language": "python", 337 | "outputs": [ 338 | { 339 | "output_type": "stream", 340 | "stream": "stdout", 341 | "text": [ 342 | "9.3\n" 343 | ] 344 | } 345 | ], 346 | "prompt_number": 9 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "source": [ 351 | "With that it's possible to mount code to be passed to the interpreter during the execution of a program. This feature should be used with caution because code assembled from system inputs open up security holes." 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "collapsed": false, 357 | "input": [], 358 | "language": "python", 359 | "outputs": [ 360 | { 361 | "html": [ 362 | "\n", 410 | "" 425 | ], 426 | "output_type": "pyout", 427 | "prompt_number": 1, 428 | "text": [ 429 | "" 430 | ] 431 | } 432 | ], 433 | "prompt_number": 1 434 | } 435 | ] 436 | } 437 | ] 438 | } 439 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Python for Developers 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 | View on GitHub 21 | 22 |

Python for Developers

23 |

First edition in English

24 | 25 |
26 | Download this project as a .zip file 27 | Download this project as a tar.gz file 28 |
29 |
30 |
31 | 32 | 33 |
34 |
35 |

36 | This book is geared toward those who already have programming knowledge. It covers topics that include: 37 | creation of user interfaces, computer graphics, internet applications, distributed systems, among other 38 | issues. 39 |

40 | 41 |
42 |
    43 |
  1. Foreword
  2. 44 |
  3. Content
  4. 45 |
  5. License
  6. 46 |
  7. Acknowledgment
  8. 47 |
  9. About the author
  10. 48 |
  11. Using the book
  12. 49 |
50 |
51 |
52 | 53 |
54 | 55 |

56 | 57 | Foreword 58 |

59 |

60 | Dynamic languages were seen in the past only as scripting languages, used to automate small tasks, but with 61 | the passing of time, they grew, matured and conquered their place in the market, to the point of calling 62 | the attention of the major providers of technology. 63 |

64 |

65 | Several factors contributed to this change, such as the internet, open source software and agile 66 | development methodologies. The internet has enabled the sharing of information in a way unprecedented in 67 | history, which made possible the growth of open source software. Dynamic languages are usually open source 68 | and share the same features and in some cases, the same goals. 69 |

70 |

71 | Among the dynamic languages, Python stands out as one of the most popular and powerful. There is a lively 72 | community of language users in the world, and this is reflected in active mailing lists and many tools 73 | available in open source. 74 |

75 |

76 | Learning a new programming language means learning to think differently. And learn a dynamic language 77 | represents a paradigm shift even harder for those people who have spent years developing in static languages. 78 |

79 |

80 | The e-book "Python para Desenvolvedores" ("Python for Developers") aimed to fill a gap, which was the lack 81 | of free courseware in Portuguese about the Python programming language. The work was adopted as a support 82 | material for several universities in Brazil, served as a reference for academic papers and has been 83 | downloaded over 100,000 times from its official website. 84 |

85 |

86 | This e-book was originally written in Portuguese and was translated by a small group of volunteers who do 87 | not speak English natively, so we ask for help from those who know better the English language, to refine 88 | the work, which we believe is a meaningful contribution to the Python users around the world. 89 |

90 | 91 |

92 | 93 | Content 94 |

95 |

96 | The chapters below were rendered through nbviewer in real time. They 97 | are read-only. To change any of the content, simply clone this project in Github. 98 |

99 | 100 |

Part I

101 |
102 |

103 | This part deals with the basics of Python programming language, including syntax, types, 104 | control structures, functions, and documentation. 105 |

106 | 124 | 125 |

Part II

126 |
127 |

128 | This part is about modules and packages, highlighting some of the most important ones that are present in 129 | standard library of the language, installation of third-party libraries, exceptions and introspection. 130 |

131 | 148 | 149 |

Part III

150 |
151 |

152 | This part is divided into two topics: generators, an increasingly present technology in the language, and 153 | functional programming. 154 |

155 | 161 | 162 |

Part IV

163 |
164 |

165 | This part mainly focuses on object orientation, and also addresses decorators and automated tests. 166 |

167 | 179 | 180 |

Part V

181 |
182 |

183 | This part covers various technologies that today are provided by applications mainly to deal 184 | with storage and information exchange: database access, persistence, XML and Web. In addition to these 185 | topics, we have the use of threads and the MVC architecture. 186 |

187 | 196 | 197 |

Part VI

198 |
199 |

200 | 201 | This part presents some features of the packages NumPy, SciPy and Matplolib, and also of known 202 | graphical interfaces toolkits . Also, a brief introduction to computer graphics and 203 | distributed processing. Finally, comments about performance in Python and ways of packaging and 204 | distribute applications. 205 |

206 | 218 | 219 |

Appendix

220 |
221 |

222 | This part focuses on other technologies that have to live with Python in several ways. And the 223 | end, the answers of the exercises proposed in the previous parts. 224 |

225 | 240 | 241 |

242 | 243 | License 244 |

245 | 246 |

247 | This work is licensed under a Creative Commons Attribution - Noncommercial - 248 | ShareAlike license. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/br/ 249 | or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA. 250 |

251 | 252 |

253 | 254 | Acknowledgment 255 |

256 |

257 | I would like to thank my wife and my parents, for their patience during the 258 | creation of this work. 259 |

260 |

261 | Besides them, I would like to thank everyone who supported and helped to publicize the book. 262 |

263 | 264 |

265 | 266 | About the author 267 |

268 |

269 | Luiz Eduardo Borges is an engineer and systems analyst, a graduate degree in Computer Graphics by 270 | State University of Rio de Janeiro (UERJ). He has been working for over two decades in computer science under 271 | various ways. 272 |

273 | 274 |

275 | 276 | Using the book 277 |

278 |

279 | This book can be read in two different ways: 280 |

    281 |
  1. 282 | The first way is the most interactive of all. Here you have to clone the repository of the book to 283 | download all files .ipynb to your local machine. If you have IPython 284 | installed, you can then view all the chapters in your browser and run the code 285 | examples. This is one of the best ways to maximize your learning, but some dependencies 286 | must be installed on a machine. 287 |
  2. 288 |
  3. 289 | The second way is to use the site nbviewer which presents the 290 | IPython Notebook right in your browser. The content is updated according to the 291 | repository updates. To access the chapters, use the links on the Content section above. 292 |
  4. 293 | 298 |
299 |

300 |
301 |
302 | 303 | 304 | 311 | 312 | 316 | 322 | 323 | 324 | 325 | -------------------------------------------------------------------------------- /Chapter1/Chapter1_Introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "Chapter1_Introduction" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", 15 | "==========================\n", 16 | "First edition\n", 17 | "-----------------------------------\n", 18 | "\n", 19 | "Chapter 1\n", 20 | "==========\n", 21 | "__________\n", 22 | "\n", 23 | "Introduction\n", 24 | "----------\n", 25 | "[Python](http://www.python.org) is a Very High Level Language, object-oriented, dynamic and with strong typing, interpreted and interactive.\n", 26 | "\n", 27 | "Features\n", 28 | "---------------\n", 29 | "Python has a clear and concise syntax, which favors the readability of source code, and makes the language more productive.\n", 30 | "\n", 31 | "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", 32 | "\n", 33 | "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", 34 | "\n", 35 | "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", 36 | "\n", 37 | "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", 38 | "\n", 39 | "History\n", 40 | "---------\n", 41 | "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", 42 | "\n", 43 | "Today, the language is well accepted in the industry for high-tech companies, such as:\n", 44 | "\n", 45 | "+ Google (Web applications).\n", 46 | "+ Yahoo (Web applications).\n", 47 | "+ Microsoft (IronPython: Python for. NET)\n", 48 | "+ Nokia (available for recent lines of cell phones and PDAs).\n", 49 | "+ Disney (3D animations).\n", 50 | "\n", 51 | "Versions\n", 52 | "-------\n", 53 | "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", 54 | "\n", 55 | "[http://www.python.org/download/](http://www.python.org/download/)\n", 56 | "\n", 57 | "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", 58 | "\n", 59 | "There are also implementations of Python for. NET (IronPython), JVM (Jython) and Python (PyPy).\n", 60 | "\n", 61 | "Running programs\n", 62 | "--------------------\n", 63 | "\n", 64 | "Example of Python program:" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "collapsed": false, 70 | "input": [ 71 | "# the character \"#\" indicates that the rest of the line is a comment\n", 72 | "# A list of musical instruments\n", 73 | "instruments = ['Bass', 'Drums', 'Guitar']\n", 74 | "\n", 75 | "# for each name in the list of instruments\n", 76 | "for instrument in instruments:\n", 77 | " # show the name of the musical instrument\n", 78 | " print instrument" 79 | ], 80 | "language": "python", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "output_type": "stream", 85 | "stream": "stdout", 86 | "text": [ 87 | "Bass\n", 88 | "Drums\n", 89 | "Guitar\n" 90 | ] 91 | } 92 | ], 93 | "prompt_number": 5 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "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", 100 | "\n", 101 | "The source files are usually identified by the extension\u00a0\".py\" and can be run directly by the interpreter:\n", 102 | "\n", 103 | "`python apl.py`\n", 104 | "\n", 105 | "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", 106 | "\n", 107 | "Dynamic Typing\n", 108 | "----------------\n", 109 | "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", 110 | "\n", 111 | "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", 112 | "\n", 113 | "Compilation and interpretation\n", 114 | "--------------------------\n", 115 | "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", 116 | "\n", 117 | "![Compilation, interpretation and packing](files/bpyfd_diags1.png)\n", 118 | "\n", 119 | "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", 120 | "\n", 121 | "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", 122 | "\n", 123 | "Interactive Mode\n", 124 | "----------------\n", 125 | "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", 126 | "\n", 127 | "`python`\n", 128 | "\n", 129 | "It is ready to receive commands after the appearance of the signal `>>>` on the screen:\n", 130 | "\n", 131 | "`Python 2.6.4 (r264:75706, Nov 3 2009, 13:20:47)`
\n", 132 | "`[GCC 4.4.1] on linux2`
\n", 133 | "`Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.`
\n", 134 | "`>>>`\n", 135 | "\n", 136 | "On Windows, the interactive mode is also available via the icon \"Python (command line)\".\n", 137 | "\n", 138 | "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", 139 | "\n", 140 | "Besides the traditional interactive mode of Python, there are other programs that act as alternatives to more sophisticated interfaces (such as PyCrust):\n", 141 | "![PyCrust](files/pycrust.png)\n", 142 | "\n", 143 | "Tools\n", 144 | "-----------\n", 145 | "There are many development tools for Python, such as IDEs, editors and shells (that take advantage of the interactive capabilities of Python).\n", 146 | "\n", 147 | "*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", 148 | "Among Python IDEs, there are:\n", 149 | "\n", 150 | "+ [PyScripter](http://code.google.com/p/pyscripter/)\n", 151 | "+ [SPE](http://pythonide.blogspot.com/) (Stani's Python Editor)\n", 152 | "+ [Eric](http://eric-ide.python-projects.org/)\n", 153 | "+ [PyDev](http://pydev.org/) (plug-in para a IDE Eclipse)\n", 154 | "\n", 155 | "![PyScripter](files/pyscripter.png)\n", 156 | "\n", 157 | "There are also text editors specialized in programming code, which have features like syntax colorization, export to other formats and convert text encoding.\n", 158 | "\n", 159 | "These editors support multiple programming languages\u200b\u200b, Python among them:\n", 160 | "\n", 161 | "+ [SciTE](http://www.scintilla.org/SciTE.html)\n", 162 | "+ [Notepad++](http://notepad-plus.sourceforge.net/br/site.htm)\n", 163 | "\n", 164 | "*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", 165 | "\n", 166 | "Beyond the standard Python *Shell*, there are others available:\n", 167 | "\n", 168 | "+ PyCrust \n", 169 | "+ IPython \n", 170 | "\n", 171 | "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", 172 | "\n", 173 | "Among packers for Python, are available:\n", 174 | "\n", 175 | "+ py2exe (Windows only)\n", 176 | "+ cx_Freeze (portable)\n", 177 | "\n", 178 | "*Frameworks* are collections of software components (libraries, utilities and others) that have been designed to be used by other systems.\n", 179 | "\n", 180 | "Some of the most known *frameworks* availble are:\n", 181 | "\n", 182 | "+ Web: Django, TurboGears, Zope and web2py.\n", 183 | "+ Graphic interface: wxPython, PyGTK and PyQt.\n", 184 | "+ Scientific processing: NumPy and SciPy.\n", 185 | "+ Image processing: PIL.\n", 186 | "+ 2D: Matplotlib and SVGFig.\n", 187 | "+ 3D: Visual Python, PyOpenGL and Python Ogre.\n", 188 | "+ Object-relational mapping: SQLAlchemy e SQLObject.\n", 189 | "\n", 190 | "Culture\n", 191 | "-------\n", 192 | "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", 193 | "\n", 194 | "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:" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "collapsed": false, 200 | "input": [ 201 | "import this" 202 | ], 203 | "language": "python", 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "output_type": "stream", 208 | "stream": "stdout", 209 | "text": [ 210 | "The Zen of Python, by Tim Peters\n", 211 | "\n", 212 | "Beautiful is better than ugly.\n", 213 | "Explicit is better than implicit.\n", 214 | "Simple is better than complex.\n", 215 | "Complex is better than complicated.\n", 216 | "Flat is better than nested.\n", 217 | "Sparse is better than dense.\n", 218 | "Readability counts.\n", 219 | "Special cases aren't special enough to break the rules.\n", 220 | "Although practicality beats purity.\n", 221 | "Errors should never pass silently.\n", 222 | "Unless explicitly silenced.\n", 223 | "In the face of ambiguity, refuse the temptation to guess.\n", 224 | "There should be one-- and preferably only one --obvious way to do it.\n", 225 | "Although that way may not be obvious at first unless you're Dutch.\n", 226 | "Now is better than never.\n", 227 | "Although never is often better than *right* now.\n", 228 | "If the implementation is hard to explain, it's a bad idea.\n", 229 | "If the implementation is easy to explain, it may be a good idea.\n", 230 | "Namespaces are one honking great idea -- let's do more of those!\n" 231 | ] 232 | } 233 | ], 234 | "prompt_number": 6 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "The text emphasizes the pragmatic attitude of the *Benevolent Dictator for Life (BDFL)* as Guido is known in the Python community.\n", 241 | "\n", 242 | "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", 243 | "\n", 244 | "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." 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "collapsed": false, 250 | "input": [], 251 | "language": "python", 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "html": [ 256 | "\n", 304 | "" 319 | ], 320 | "output_type": "pyout", 321 | "prompt_number": 1, 322 | "text": [ 323 | "" 324 | ] 325 | } 326 | ], 327 | "prompt_number": 1 328 | } 329 | ], 330 | "metadata": {} 331 | } 332 | ] 333 | } 334 | --------------------------------------------------------------------------------