{ "metadata": { "name": "Chapter2_Syntax" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", "=============================\n", "First edition\n", "-----------------------------------\n", "\n", "Chapter 2: Syntax\n", "===================\n", "___________________\n", "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", "\n", "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", "\n", "Functional comments are used to:\n", "\n", "+ 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", "+ 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", "\n", "Example of functional comments:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#!/usr/bin/env python\n", "\n", "# A code line that shows the result of 7 times 3\n", "print 7 * 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "21\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Examples of broken lines:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A line broken by backslash\n", "a = 7 * 3 + \\\n", "5 / 2\n", "\n", "# A list (broken by comma)\n", "b = ['a', 'b', 'c', \n", "'d', 'e']\n", "\n", "# A function call (broken by comma)\n", "c = range(1,\n", "11)\n", "\n", "# Prints everything\n", "print a, b, c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "23 ['a', 'b', 'c', 'd', 'e'] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "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", "\n", "Blocks\n", "------\n", "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", "\n", "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", "\n", "![Program structure](files/bpyfd_diags2.png)\n", "\n", "Example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# For i on the list 234, 654, 378, 798:\n", "for i in [234, 654, 378, 798]:\n", " # If the remainder dividing by 3 is equal to zero:\n", " if i % 3 == 0:\n", " # Prints...\n", " print i, '/ 3 =', i / 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "234 / 3 = 78\n", "654 / 3 = 218\n", "378 / 3 = 126\n", "798 / 3 = 266\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The operator `%` computes the modulus (remainder of division)." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "" ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }