{ "metadata": { "name": "", "signature": "sha256:f68b9bfb9c3fa936de40e570b5ee8d7a82f02160fce8cc4fc8384697c0b86ac7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7: Basic Types" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example sum2.c, Page 131" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def main():\n", " sum=0\n", " print \"This program sums a series of integers.\"\n", " n=int(raw_input(\"Enter integers (0 to terminate): \")) #input the integers to operate on\n", " while(n!=0):\n", " sum=sum+n #calculating sum till 0 encountered\n", " n=input()\n", " print \"The sum is: %d\" % sum #printing sum\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This program sums a series of integers.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter integers (0 to terminate): 8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "23\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "71\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum is: 107\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example length.c, Page 142" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def main():\n", " str=raw_input(\"Enter a message: \") #input string\n", " length=len(str) #calculate length\n", " print \"Your message was %d character(s) long\" % length #display length\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a message: Brevity is the soul of wit.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your message was 27 character(s) long\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }