summaryrefslogtreecommitdiff
path: root/Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb
diff options
context:
space:
mode:
authorThomas Stephen Lee2015-09-04 22:04:10 +0530
committerThomas Stephen Lee2015-09-04 22:04:10 +0530
commit64419e47f762802600b3a2b6d8c433a16ccd3d55 (patch)
tree14ad7c37c9547cd516f141494f3fa375621edbaa /Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb
parent10f6fb8cd1d840a3042651dfaa6fd5af4924b94a (diff)
downloadPython-Textbook-Companions-64419e47f762802600b3a2b6d8c433a16ccd3d55.tar.gz
Python-Textbook-Companions-64419e47f762802600b3a2b6d8c433a16ccd3d55.tar.bz2
Python-Textbook-Companions-64419e47f762802600b3a2b6d8c433a16ccd3d55.zip
add/remove/update books
Diffstat (limited to 'Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb')
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb1070
1 files changed, 1070 insertions, 0 deletions
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb
new file mode 100755
index 00000000..a071712d
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb
@@ -0,0 +1,1070 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 4: Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.1, Page number: 91"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration and result\n",
+ "a = 5\n",
+ "c = 'h'\n",
+ "b = 3.14\n",
+ "def myfunction (x, ch, fl) :\n",
+ " print ('%d %c %.2f ' % (x, ch, fl))\n",
+ " \n",
+ "myfunction (a, c, b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5 h 3.14 \n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.2, Page number: 92"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def add (a, b) :\n",
+ " som = a + b\n",
+ " return som\n",
+ "\n",
+ "print ('The sum of %d and %d is %d' % (a, b, (add (a, b))))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 7 and 8 is 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.3, Page number: 99"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b) :\n",
+ " if a > b :\n",
+ " return a\n",
+ " else :\n",
+ " return b\n",
+ "\n",
+ "print ('The greater number is %d' % (greater (a, b)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 190\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greater number is 190\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.4, Page number: 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b) :\n",
+ " if a == b :\n",
+ " print ('Both numbers are equal')\n",
+ " elif a > b :\n",
+ " print ('The greater number is %d' % a)\n",
+ " else :\n",
+ " print ('The greater number is %d' % b)\n",
+ " return\n",
+ "\n",
+ "greater (a, b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Both numbers are equal\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.5, Page number: 102"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "c = int(raw_input('Enter third number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greatest (a, b, c) :\n",
+ " if a > b :\n",
+ " if a > c :\n",
+ " return a\n",
+ " else :\n",
+ " return c\n",
+ " else :\n",
+ " if b > c :\n",
+ " return b\n",
+ " else :\n",
+ " return c\n",
+ "\n",
+ "print ('The greatest number is %d' % (greatest (a, b, c)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 128\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter third number: 190\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greatest number is 190\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.6, Page number: 104"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printnum (n) :\n",
+ " print ('Counting upto %d' % n) \n",
+ " for i in range (1, n+1) :\n",
+ " print i,\n",
+ "\n",
+ "printnum (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Counting upto 7\n",
+ "1 2 3 4 5 6 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.7, Page number: 105"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printeven (n) :\n",
+ " print ('Even numbers upto %d' % n) \n",
+ " for i in range (2, n+1, 2) :\n",
+ " print i,\n",
+ "\n",
+ "printeven (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Even numbers upto 7\n",
+ "2 4 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.8, Page number: 107"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printodd (n) :\n",
+ " print ('Odd numbers upto %d' % n) \n",
+ " for i in range (1, n+1, 2) :\n",
+ " print i,\n",
+ "\n",
+ "printodd (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Odd numbers upto 7\n",
+ "1 3 5 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.9, Page number: 108"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printnum (n) :\n",
+ " print ('Number line for %d' % n) \n",
+ " for i in range (-n, n+1) :\n",
+ " print i,\n",
+ "\n",
+ "printnum (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number line for 4\n",
+ "-4 -3 -2 -1 0 1 2 3 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.10, Page number: 110"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def summate (n) :\n",
+ " total = 0\n",
+ " for i in range (1, n+1) :\n",
+ " total = total + i\n",
+ " return total\n",
+ "\n",
+ "print('Sum of first %d natural numbers is %d' % (n, summate (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of first 6 natural numbers is 21\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.11, Page number: 111"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def factors (n) :\n",
+ " print ('The factors of %d are' % n)\n",
+ " for i in range (1, n+1) :\n",
+ " if n%i == 0 :\n",
+ " print i,\n",
+ "\n",
+ "factors (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 28\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The factors of 28 are\n",
+ "1 2 4 7 14 28\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.12, Page number: 113"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def multiple (n) :\n",
+ " print ('The first 10 multiples of %d are' % n)\n",
+ " for i in range (1, 11) :\n",
+ " print('%d X %d = %d' % (n, i, n*i))\n",
+ "\n",
+ "multiple (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The first 10 multiples of 6 are\n",
+ "6 X 1 = 6\n",
+ "6 X 2 = 12\n",
+ "6 X 3 = 18\n",
+ "6 X 4 = 24\n",
+ "6 X 5 = 30\n",
+ "6 X 6 = 36\n",
+ "6 X 7 = 42\n",
+ "6 X 8 = 48\n",
+ "6 X 9 = 54\n",
+ "6 X 10 = 60\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.13, Page number: 114"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n) :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " return sum\n",
+ "\n",
+ "print ('The sum of digits of %d is %d' % (n, sumofdigit (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 5214\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of digits of 5214 is 12\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.14, Page number: 116"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def prime (n) :\n",
+ " count = 0\n",
+ " for i in range (2, n) :\n",
+ " if n%i == 0 :\n",
+ " count = count + 1\n",
+ " break\n",
+ "\n",
+ " if count == 0 :\n",
+ " print ('The number %d is prime' % n)\n",
+ " else :\n",
+ " print ('The number %d is composite' % n)\n",
+ "\n",
+ "prime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 14\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number 14 is composite\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.15, Page number: 118"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('%d terms of Fibonacci series are ' % n)\n",
+ "\n",
+ "def printfibo (n) :\n",
+ " first = 0\n",
+ " second = 1\n",
+ " count = 3\n",
+ "\n",
+ " print ('%d %d ' % (first, second)),\n",
+ "\n",
+ " while count<=n :\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ " print (' %d ' % third), \n",
+ " count = count + 1\n",
+ "\n",
+ "printfibo (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 terms of Fibonacci series are \n",
+ "0 1 1 2 3 5 8 13 21 34 \n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.16, Page number: 120"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n) :\n",
+ " sum = n\n",
+ " while sum>=10 :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " n = sum\n",
+ " return sum\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, sumofdigit (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 8626\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 8626 is 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.17, Page number: 122"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('Prime numbers upto %d are ' % n)\n",
+ "\n",
+ "def printprime (n) :\n",
+ " for num in range (2, n+1) :\n",
+ " flag = 1\n",
+ " for i in range (2, num) :\n",
+ " if num % i == 0 : \n",
+ " flag = 0\n",
+ " if flag :\n",
+ " print ('%d ' % num),\n",
+ "\n",
+ "printprime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 40\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Prime numbers upto 40 are \n",
+ "2 3 5 7 11 13 17 19 23 29 31 37 \n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.18, Page number: 124"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for count in range (1, lin+1) :\n",
+ " print ('%d ' % count),\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 \n",
+ "1 2 \n",
+ "1 2 3 \n",
+ "1 2 3 4 \n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.19, Page number: 126"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for count in range (lin, 2*lin) :\n",
+ " print ('%d ' % count),\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 \n",
+ "2 3 \n",
+ "3 4 5 \n",
+ "4 5 6 7 \n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.20, Page number: 128"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for blk in range (1, n-lin+1) :\n",
+ " print ' ',\n",
+ " for up in range (1, lin+1) :\n",
+ " print ('%d' % up),\n",
+ " down = lin-1\n",
+ " while down>0 :\n",
+ " print ('%d' % down),\n",
+ " down = down-1\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1\n",
+ " 1 2 1\n",
+ " 1 2 3 2 1\n",
+ "1 2 3 4 3 2 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.21, Page number: 131"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for blk in range (1, n-lin+1) :\n",
+ " print ' ',\n",
+ " for up in range (lin, 2*lin) :\n",
+ " print ('%d' % (up%10)),\n",
+ " down = 2*lin-2\n",
+ " while down>=lin :\n",
+ " print ('%d' % (down%10)),\n",
+ " down = down-1\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1\n",
+ " 2 3 2\n",
+ " 3 4 5 4 3\n",
+ "4 5 6 7 6 5 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file