Skip to content

Commit 3d4c826

Browse files
committed
Merge pull request ricardoduarte#2 from luisgustavoneves/master
chapters2-4
2 parents 194fd3d + adb34d6 commit 3d4c826

File tree

12 files changed

+2521
-1
lines changed

12 files changed

+2521
-1
lines changed

Chapter1/Chapter1_Introduction.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
"\n",
188188
"Culture\n",
189189
"-------\n",
190-
"The name Python was taken by Guido van Rossum from british TV program *Monty Python Flying Circus*, and there are many references to the show in language documentation. For instance, Python oficial package repository was called Cheese Shop, the name of one of the frames of the programa. Currently, the repository name isAtualmente, o nome do reposit\u00f3rio \u00e9 [Python Package Index](http://pypi.python.org/pypi) (PYPI).\n",
190+
"The name Python was taken by Guido van Rossum from british TV program *Monty Python Flying Circus*, and there are many references to the show in language documentation. For instance, Python oficial package repository was called Cheese Shop, the name of one of the frames of the programa. Currently, the repository name is [Python Package Index](http://pypi.python.org/pypi) (PYPI).\n",
191191
"\n",
192192
"The goals of the project were summarized by Tim Peters in a text called *Zen of Python* , which is available in Python itself using the command:"
193193
]

Chapter2/Chapter2_Syntax.ipynb

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
{
2+
"metadata": {
3+
"name": "Chapter2_Syntax"
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 2: Syntax\n",
20+
"===================\n",
21+
"___________________\n",
22+
"A program written in Python consists of lines, which may continue in 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 for:\n",
27+
"\n",
28+
"+ change the encoding of the source file of the program by adding a comment with the text `# - * - coding: <encoding> - # -` at the beginning of the file, in which `<encoding> ` is the file encoding (usually latin1 or utf-8). Change 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+
"outputs": [
45+
{
46+
"output_type": "stream",
47+
"stream": "stdout",
48+
"text": [
49+
"21\n"
50+
]
51+
}
52+
],
53+
"prompt_number": 3
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"source": [
58+
"Examples of broken lines:"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"collapsed": false,
64+
"input": [
65+
"# A line broken by backslash\n",
66+
"a = 7 * 3 + \\\n",
67+
"5 / 2\n",
68+
"\n",
69+
"# A list (broken by comma)\n",
70+
"b = ['a', 'b', 'c', \n",
71+
"'d', 'e']\n",
72+
"\n",
73+
"# A function call (broken by comma)\n",
74+
"c = range(1,\n",
75+
"11)\n",
76+
"\n",
77+
"# Prints everything\n",
78+
"print a, b, c"
79+
],
80+
"language": "python",
81+
"outputs": [
82+
{
83+
"output_type": "stream",
84+
"stream": "stdout",
85+
"text": [
86+
"23 ['a', 'b', 'c', 'd', 'e'] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
87+
]
88+
}
89+
],
90+
"prompt_number": 5
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"source": [
95+
"The command `print` inserts spaces between expressions that are received as a parameter, and a newline character at the end, unless he receives a comma at the end of the parameter list.\n",
96+
"\n",
97+
"Blocks\n",
98+
"------\n",
99+
"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 <span class = \"note\" title = \"The official recommendation coding style (http://www.python.org/dev/peps/pep-0008/) is to use four spaces for indentation and this convention is widely accepted by developers.\"> spaces </ span >.\n",
100+
"\n",
101+
"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",
102+
"\n",
103+
"![Estrutura de um programa](files/bpypd_diags2.png)\n",
104+
"\n",
105+
"Example:"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"collapsed": false,
111+
"input": [
112+
"# For i on the list 234, 654, 378, 798:\n",
113+
"for i in [234, 654, 378, 798]:\n",
114+
" # If the remainder dividing by 3 is equal to zero:\n",
115+
" if i % 3 == 0:\n",
116+
" # Prints...\n",
117+
" print i, '/ 3 =', i / 3"
118+
],
119+
"language": "python",
120+
"outputs": [
121+
{
122+
"output_type": "stream",
123+
"stream": "stdout",
124+
"text": [
125+
"234 / 3 = 78\n",
126+
"654 / 3 = 218\n",
127+
"378 / 3 = 126\n",
128+
"798 / 3 = 266\n"
129+
]
130+
}
131+
],
132+
"prompt_number": 1
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"source": [
137+
"The operator `%` computes the modulus (remainder of division)."
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"collapsed": false,
143+
"input": [],
144+
"language": "python",
145+
"outputs": [
146+
{
147+
"html": [
148+
"<style>\n",
149+
" @font-face {\n",
150+
" font-family: \"Computer Modern\";\n",
151+
" src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
152+
" }\n",
153+
" div.cell{\n",
154+
" width:800px;\n",
155+
" margin-left:16% !important;\n",
156+
" margin-right:auto;\n",
157+
" }\n",
158+
" h1 {\n",
159+
" font-family: Helvetica, serif;\n",
160+
" }\n",
161+
" h4{\n",
162+
" margin-top:12px;\n",
163+
" margin-bottom: 3px;\n",
164+
" }\n",
165+
" div.text_cell_render{\n",
166+
" font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",
167+
" line-height: 145%;\n",
168+
" font-size: 130%;\n",
169+
" width:800px;\n",
170+
" margin-left:auto;\n",
171+
" margin-right:auto;\n",
172+
" }\n",
173+
" .CodeMirror{\n",
174+
" font-family: \"Source Code Pro\", source-code-pro,Consolas, monospace;\n",
175+
" }\n",
176+
" .note{\n",
177+
" border-bottom: 1px black dotted;\n",
178+
" }\n",
179+
" .prompt{\n",
180+
" display: None;\n",
181+
" }\n",
182+
" .text_cell_render h5 {\n",
183+
" font-weight: 300;\n",
184+
" font-size: 16pt;\n",
185+
" color: #4057A1;\n",
186+
" font-style: italic;\n",
187+
" margin-bottom: .5em;\n",
188+
" margin-top: 0.5em;\n",
189+
" display: block;\n",
190+
" }\n",
191+
" \n",
192+
" .warning{\n",
193+
" color: rgb( 240, 20, 20 )\n",
194+
" } \n",
195+
"</style>\n",
196+
"<script>\n",
197+
" MathJax.Hub.Config({\n",
198+
" TeX: {\n",
199+
" extensions: [\"AMSmath.js\"]\n",
200+
" },\n",
201+
" tex2jax: {\n",
202+
" inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
203+
" displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
204+
" },\n",
205+
" displayAlign: 'center', // Change this to 'center' to center equations.\n",
206+
" \"HTML-CSS\": {\n",
207+
" styles: {'.MathJax_Display': {\"margin\": 4}}\n",
208+
" }\n",
209+
" });\n",
210+
"</script>"
211+
],
212+
"output_type": "pyout",
213+
"prompt_number": 1,
214+
"text": [
215+
"<IPython.core.display.HTML at 0x50f8f98>"
216+
]
217+
}
218+
],
219+
"prompt_number": 1
220+
}
221+
]
222+
}
223+
]
224+
}

Chapter2/bpypd_diags2.png

70.5 KB
Loading

0 commit comments

Comments
 (0)