\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Calculation of simple interest for 3 sets of p, n and r (while loop)'''\n",
"\n",
"#Variable declaration\n",
"count = 1\n",
"pr = [1000,2000,3500]\n",
"yr = [5,5,5]\n",
"intr = [13.5,13.5,3.5]\n",
"\n",
"# while loop\n",
"while count <= 3:\n",
" #Input from the user\n",
" #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
" p = pr[count-1] # principle\n",
" n = yr[count-1] # number of years\n",
" r = intr[count-1]# rate of interest\n",
"\n",
" #Calculation\n",
" si = p * n * r / 100 ; #formula for simple interest\n",
"\n",
" #Result\n",
" print \"Simple interest = Rs.\",si \n",
"\n",
" #Increment count\n",
" count = count + 1\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Simple interest = Rs. 675.0\n",
"Simple interest = Rs. 1350.0\n",
"Simple interest = Rs. 612.5\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Simple Interest using For Loop , Page number: 109
\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Calculation of simple interest for 3 sets of p, n and r (for loop)'''\n",
"\n",
"pr = [1000,2000,3500]\n",
"yr = [5,5,5]\n",
"intr = [13.5,13.5,3.5]\n",
"\n",
"#for loop\n",
"for count in range(1, 4):\n",
" #Input from the user\n",
" #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
" p = pr[count-1] # principle\n",
" n = yr[count-1] # number of years\n",
" r = intr[count-1]# rate of interest\n",
" \n",
" #Calculation\n",
" si = p * n * r / 100 ; #formula for simple interest\n",
"\n",
" #Result\n",
" print \"Simple interest = Rs.\",si "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Simple interest = Rs. 675.0\n",
"Simple interest = Rs. 1350.0\n",
"Simple interest = Rs. 612.5\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Nested For Loops , Page number: 114
\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Demonstration of nested loops'''\n",
"\n",
"#nested for loops\n",
"for r in range(1,4): #outer loop\n",
" for c in range(1,3): #inner loop\n",
" s = r + c #find the sum\n",
" print \"r = %d c = %d sum = %d\" % (r, c, s) #Display result\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"r = 1 c = 1 sum = 2\n",
"r = 1 c = 2 sum = 3\n",
"r = 2 c = 1 sum = 3\n",
"r = 2 c = 2 sum = 4\n",
"r = 3 c = 1 sum = 4\n",
"r = 3 c = 2 sum = 5\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Do While Loop , Page number: 116
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Execution of a loop an unknown number of times'''\n",
"\n",
"#do while loop\n",
"while True:\n",
" #num = raw_input(\"Enter a number: \")\n",
" num = 11\n",
" print \"square of %d is %d\"%(num, num * num )\n",
" print \"Want to enter another number y/n: \" \n",
" another = 'n'\n",
" print another\n",
" if another == 'y':\n",
" continue\n",
" else:\n",
" break\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"square of 11 is 121\n",
"Want to enter another number y/n: \n",
"n\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Do While using For Loop, Page number: 117
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''odd loop using a for loop'''\n",
"\n",
"#Variable declaration\n",
"another = 'y'\n",
"\n",
"#do while loop\n",
"import sys\n",
"for i in range(1,10000): #infinte loop\n",
" #num = raw_input(\"Enter a number: \")\n",
" num = 11\n",
" print \"square of %d is %d\"%(num, num * num )\n",
" print \"Want to enter another number y/n: \" \n",
" another = 'n'\n",
" print another\n",
" if another == 'y':\n",
" continue\n",
" else:\n",
" break\n",
" \n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"square of 11 is 121\n",
"Want to enter another number y/n: \n",
"n\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Do While using While Loop, Page number: 117
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''odd loop using a while loop'''\n",
"\n",
"#Variable declaration\n",
"another = 'y'\n",
"\n",
"#do while loop\n",
"while another == 'y':\n",
" #num = raw_input(\"Enter a number: \")\n",
" num = 11\n",
" print \"square of %d is %d\"%(num, num * num )\n",
" print \"Want to enter another number y/n: \" \n",
" another = 'n'\n",
" print another\n",
" \n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"square of 11 is 121\n",
"Want to enter another number y/n: \n",
"n\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Prime Number, Page number: 118
\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Write a program to determine whether a number is prime or not.\n",
"A prime number is one, which is divisible only by 1 or itself.'''\n",
"\n",
"#Input from user\n",
"#num = raw_input(\"Enter a number: \")\n",
"num = 11\n",
"\n",
"#Variable declaration\n",
"i = 2\n",
"\n",
"#while loop\n",
"while i <=(num - 1):\n",
" if num % i == 0:\n",
" print \"Not a prime number\" #Display if not prime number\n",
" break\n",
" i += 1\n",
"\n",
"#Display if prime number\n",
"if i == num:\n",
" print \"Prime number\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Prime number\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"