"
]
},
{
"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": [
"
"
]
},
{
"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": [
"