"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def message():\n",
" print \"Smile, and the world smiles with you...\" \n",
"\n",
"message() #function call\n",
"print \"Cry, and you stop the monotony!\" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Smile, and the world smiles with you...\n",
"Cry, and you stop the monotony!\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Calling More Than One Funnction , Page number: 159
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definitions\n",
"def italy():\n",
" print \"I am in italy\" \n",
" \n",
"def brazil():\n",
" print \"I am in brazil\" \n",
"\n",
"def argentina():\n",
" print \"I am in argentina\" \n",
" \n",
"\n",
"print \"I am in main\" \n",
"#function calls\n",
"italy()\n",
"brazil()\n",
"argentina()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in main\n",
"I am in italy\n",
"I am in brazil\n",
"I am in argentina\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Function Calling Function, Page number: 161
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definitions\n",
"def argentina():\n",
" print \"I am in argentina\" \n",
"\n",
"def brazil():\n",
" print \"I am in brazil\" \n",
" argentina() #function call\n",
"\n",
"def italy():\n",
" print \"I am in italy\" \n",
" brazil() #function call\n",
" print \"I am back in italy\" \n",
" \n",
"print \"I am in main\" \n",
"#function call\n",
"italy()\n",
"print \"I am finally back in main\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in main\n",
"I am in italy\n",
"I am in brazil\n",
"I am in argentina\n",
"I am back in italy\n",
"I am finally back in main\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Calsum Function , Page number: 166
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def calsum ( x, y, z ):\n",
" d = x + y + z\n",
" return d\n",
"\n",
"#Input from user\n",
"#a,b,c = raw_input (\"Enter any three numbers: \").split()\n",
"print \"Enter any three numbers: \"\n",
"a = 10\n",
"b = 20\n",
"c = 30\n",
"print a,b,c\n",
"\n",
"#function call\n",
"s = calsum(a,b,c) \n",
"\n",
"#Result\n",
"print \"Sum = \", s \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any three numbers: \n",
"10 20 30\n",
"Sum = 60\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Return Statement , Page number: 169
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"statements that may be present in a function.\n",
"Also, the return statement need not always be present at the end of the called\n",
"\n",
"# function definition\n",
"def fun():\n",
" #Input from user\n",
" #ch = raw_input (\"Enter any alphabet: \")\n",
" print \"Enter any alphabet: \"\n",
" ch = 'a'\n",
" print ch\n",
" ch = ord(ch)\n",
" if ch >= 65 and ch <= 90:\n",
" return ascii(ch)\n",
" else:\n",
" return ascii( ch + 32 )\n",
"\n",
"#function call\n",
"ch = fun()\n",
"\n",
"#Result\n",
"print ch\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Formal Arguments, Page number: 170
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"changed in the called function, the corresponding\n",
"\n",
"# function definition\n",
"def fun(b):\n",
" b = 60\n",
" print b \n",
"\n",
"a = 30\n",
"fun(a) #function call\n",
"print a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"60\n",
"30\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def square(x):\n",
" y = x * x\n",
" return y\n",
"\n",
"#Input from user\n",
"#a = raw_input(\"Enter any number: \")\n",
"print \"Enter any number: \"\n",
"a = 4.5\n",
"print a\n",
"\n",
"b = square(a) #function call\n",
"\n",
"#Result\n",
"print \"Square of %f is %f\" %( a, b )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any number: \n",
"4.5\n",
"Square of 4.500000 is 20.250000\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Void Function , Page number: 177
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def gospel(): # void function\n",
" print \"Viruses are electronic bandits...\" \n",
" print \"who eat nuggets of information...\" \n",
" print \"and chunks of bytes...\" \n",
" print \"when you least expect...\" \n",
"\n",
"gospel() # function call"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Viruses are electronic bandits...\n",
"who eat nuggets of information...\n",
"and chunks of bytes...\n",
"when you least expect...\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Address of a Variable , Page number: 180
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 3\n",
"\n",
"#Result\n",
"print \"Address of i = \" , id(i) #printing address\n",
"print \"Value of i = \", i \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of i = 30301288\n",
"Value of i = 3\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Pointer Relations , Page number: 182
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 3 \n",
"j = id(i) # address of variable 'i'\n",
"\n",
"#Result\n",
"print \"Address of i = \", id(i) \n",
"print \"Address of i = \", j \n",
"print \"Address of j = \", id(j)\n",
"print \"Value of j = \", j \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of i = 30301288\n",
"Address of i = 30301288\n",
"Address of j = 134133200\n",
"Value of j = 30301288\n",
"Value of i = 3\n",
"Value of i = 3\n",
"Value of i = 3\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Pointer to Another Pointer , Page number: 184
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 3\n",
"j = id(i) # address of i\n",
"k = id(j) # address of j\n",
"\n",
"#Result\n",
"print \"Address of i = \", id(i) \n",
"print \"Address of i = \", j \n",
"print \"Address of i = \", j \n",
"print \"Address of j = \", id(j) \n",
"print \"Address of j = \", k \n",
"print \"Address of k = \", id(k)\n",
"print \"Value of j = \", j \n",
"print \"Value of k = \", k \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of i = 30301288\n",
"Address of i = 30301288\n",
"Address of i = 30301288\n",
"Address of j = 134132944\n",
"Address of j = 134132944\n",
"Address of k = 134133200\n",
"Value of j = 30301288\n",
"Value of k = 134132944\n",
"Value of i = 3\n",
"Value of i = 3\n",
"Value of i = 3\n",
"Value of i = 3\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Call By Value , Page number: 186
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def swapv (x,y):\n",
" x,y=y,x\n",
" print \"x = %d y = %d\" %( x, y )\n",
"\n",
"#Variable declaration\n",
"a = 10\n",
"b = 20\n",
"\n",
"swapv ( a, b ) # function call\n",
"\n",
"#Result\n",
"print \"a = %d b = %d\" %( a, b )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"x = 20 y = 10\n",
"a = 10 b = 20\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Call By Reference , Page number: 187
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def swapv (a,b):\n",
" a,b=b,a\n",
" return a,b\n",
"\n",
"#Variable declaration\n",
"a = 10\n",
"b = 20\n",
"\n",
"a,b = swapv ( a, b ) # function call\n",
"\n",
"#Result\n",
"print \"a = %d b = %d\" %( a, b )"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a = 20 b = 10\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Area And Perimeter of a Circle , Page number: 188
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Input from user\n",
"#radius = raw_input(\"Enter radius of a circle: \" )\n",
"print \"Enter radius of a circle: \"\n",
"radius = 5\n",
"print radius\n",
"\n",
"#Function definition\n",
"def areaperi ( r ):\n",
" a = 3.14 * r * r\n",
" p = 2 * 3.14 * r \n",
" return a,p\n",
"\n",
"area,perimeter = areaperi ( radius ) #function call\n",
"\n",
"\n",
"#Result\n",
"print \"Area = \", area \n",
"print \"Perimeter = \", perimeter "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter radius of a circle: \n",
"5\n",
"Area = 78.5\n",
"Perimeter = 31.4\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Iterative Factorial Function , Page number: 190
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def factorial(x):\n",
" f = 1\n",
" for i in range(x,0,-1 ):\n",
" f = f * i\n",
" return f\n",
"\n",
"#Input from user\n",
"#a = raw_input( \"Enter any number: \" )\n",
"print \"Enter any number: \"\n",
"a = 6\n",
"print a\n",
"\n",
"fact = factorial(a) #function call\n",
"\n",
"#Result\n",
"print \"Factorial value = \", fact\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any number: \n",
"6\n",
"Factorial value = 720\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Recursive Factorial Function, Page number: 191
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def rec(x):\n",
" if x == 1:\n",
" return 1\n",
" else:\n",
" f = x * rec ( x - 1 ) # calling the function\n",
" return f\n",
"\n",
"#Input from user\n",
"#a = raw_input( \"Enter any number: \" )\n",
"print \"Enter any number: \"\n",
"a = 5\n",
"print a\n",
"\n",
"fact = rec(a) #function call\n",
"\n",
"#Result\n",
"print \"Factorial value = \", fact \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any number: \n",
"5\n",
"Factorial value = 120\n"
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"