"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Check whether the entered number is less than 10? if yes, display the same\n",
"\n",
"#Variable initialization\n",
"v = raw_input(\"Enter the number : \")\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"v = int(v)\n",
"\n",
"#Result\n",
"if v < 10 :\n",
" print '\\nNumber entered is less than 10'\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number : 9\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Number entered is less than 10\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.2, Page number: 65
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Check equivalence of two numbers. Use if statement.\n",
"\n",
"#Variable initialization\n",
"\n",
"m = raw_input(\"Enter two numbers : \")\n",
"n = raw_input(\"Enter two numbers : \")\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"m = int(m)\n",
"n = int(n)\n",
"\n",
"#Result\n",
"if m - n == 0 :\n",
" print '\\nTwo numbers are equal'\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter two numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter two numbers : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Two numbers are equal\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.3, Page number: 66
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Check whether the candidate's age is greater than 17 or not. If yes, display\n",
"#message \"Eligible for Voting\"\n",
"\n",
"#Variable initialization\n",
"age = raw_input(\"Enter age : \")\n",
"age = int(age)\n",
"\n",
"#In python, raw_input() is used to read values\n",
"\n",
"#Result\n",
"if age > 17 :\n",
" print '\\nEligible for Voting.'\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter age : 20\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Eligible for Voting.\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.4, Page number: 66
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Use curly brace in the if block. Enter only the three numbers and calculate\n",
"#their sum and multiplication\n",
"\n",
"#Variable initialization\n",
"#in python,the map(int,raw_input().split()) function splits the input characters\n",
"#and converts it into integers and returns a value if all the values are integers\n",
"#otherwise an exception will be generated and initializes v as 0.\n",
"#using exception handling, this result can be achieved.\n",
"\n",
"#Exception handling\n",
"try:\n",
" #use space to separate the input characters\n",
" v = raw_input(\"Enter Three Numbers : \")\n",
" a,b,c = map(int,v.split())\n",
" x = 3\n",
"except:\n",
" x = 0\n",
"\n",
"#Result\n",
"#in python, block of statements are identified using indentation\n",
" \n",
"if x == 3:\n",
" print \"Addition : %d\"%(a+b+c)\n",
" print \"Multiplication : %d\"%(a*b*c)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers : 1 2 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Addition : 7\n",
"Multiplication : 8\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.5, Page number: 68
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the values of a,b,c through the keyboard. Add them and after addition\n",
"#check if it is in the range of 100 & 200 or not. Print separate message for each.\n",
"\n",
"\n",
"#Variable initialization\n",
"a = raw_input(\"Enter Three Numbers a b c : \")\n",
"b = raw_input(\"Enter Three Numbers a b c : \")\n",
"c = raw_input(\"Enter Three Numbers a b c : \")\n",
"\n",
"a = int(a)\n",
"b = int(b)\n",
"c = int(c)\n",
"\n",
"print 'a = %d b = %d c = %d'%(a,b,c)\n",
"#Calculation\n",
"d = a + b + c\n",
"\n",
"#Condition evaluation and Result\n",
"if d <= 200 and d >= 100 :\n",
" print \"Sum is %d which is in between 100 & 200\"%(d)\n",
"else:\n",
" print \"\\nSum is %d which is out of range\"%(d)\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers a b c : 50\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers a b c : 52\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers a b c : 54\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a = 50 b = 52 c = 54\n",
"Sum is 156 which is in between 100 & 200\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.6, Page number: 68
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the roots of a quadratic equation by using if else condition\n",
"\n",
"#Variable initialization\n",
"a = raw_input(\"Enter Values for a, b, c : \")\n",
"b = raw_input(\"Enter Values for a, b, c : \")\n",
"c = raw_input(\"Enter Values for a, b, c : \")\n",
"\n",
"a = int(a)\n",
"b = int(b)\n",
"c = int(c)\n",
"\n",
"#Condition evaluation and Result\n",
"if b * b > 4 * a * c :\n",
" x1 = b + sqrt(b*b - 4*a*c)/2*a\n",
" x2 = b - sqrt(b*b - 4*a*c)/2*a\n",
" print \"\\nx1 = %f x2 = %f\"%(x1,x2)\n",
"else:\n",
" print \"\\nRoots are Imaginary\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a, b, c : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a, b, c : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values for a, b, c : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Roots are Imaginary\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.7, Page number: 69
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate the square of those numbers only whose least significant digit is 5.\n",
"\n",
"\n",
"#Variable initialization\n",
"s = raw_input(\"Enter a Number : \")\n",
"s = int(s)\n",
"\n",
"d = s % 10;\n",
"\n",
"#Condition evaluation and Result\n",
"#There is no increment/decrement operator in python.\n",
"if d==5 :\n",
" s = s/10\n",
" s += 1\n",
" print \"\\nSquare = %d%d\"%(s*(s-1),d*d)\n",
"else:\n",
" print \"\\nInvalid Number\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 25\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Square = 625\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.8, Page number: 70
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate the salary of medical representative based on the sales. Bonus and\n",
"#incentive to be offered to him will be based on total sales. If the sale\n",
"#exceeds Rs.100000/- follow the particulars of table (1) otherwise (2)\n",
"\n",
"# 1. TABLE 2. TABLE\n",
"# Basic = Rs. 3000 Basic = Rs. 3000\n",
"# HRA = 20% of basic HRA = 20% of basic\n",
"# DA = 110% of basic DA = 110% of basic\n",
"# Conveyance = Rs. 500 Conveyance = Rs. 500\n",
"# Incentive = 10% of sales Incentive = 5% of sales\n",
"# Bonus = Rs. 500 Bonus = Rs. 200\n",
"\n",
"\n",
"#Variable initialization\n",
"sale = raw_input(\"Enter Total Sales in Rs. : \")\n",
"sale = int(sale)\n",
"\n",
"#Condition evaluation\n",
"if sale >= 100000 :\n",
" bs = 3000\n",
" hra = 20 * bs/100\n",
" da = 110 * bs/100\n",
" cv = 500\n",
" incentive = sale * 10/100\n",
" bonus = 500\n",
"else:\n",
" bs = 3000\n",
" hra = 20 * bs/100\n",
" da = 110 * bs/100\n",
" cv = 500\n",
" incentive = sale * 5/100\n",
" bonus = 200\n",
"\n",
"#Result\n",
"ts = bs + hra + da + cv + incentive + bonus\n",
"print \"\\nTotal Sales : %.2f\"%(sale)\n",
"print \"\\nBasic Salary : %.2f\"%(bs)\n",
"print \"\\nHra : %.2f\"%(hra)\n",
"print \"\\nDa : %.2f\"%(da)\n",
"print \"\\nConveyance : %.2f\"%(cv)\n",
"print \"\\nIncentive : %.2f\"%(incentive)\n",
"print \"\\nBonus : %.2f\"%(bonus)\n",
"print \"\\nGross Salary : %.2f\"%(ts)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Total Sales in Rs. : 100000\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Total Sales : 100000.00\n",
"\n",
"Basic Salary : 3000.00\n",
"\n",
"Hra : 600.00\n",
"\n",
"Da : 3300.00\n",
"\n",
"Conveyance : 500.00\n",
"\n",
"Incentive : 10000.00\n",
"\n",
"Bonus : 500.00\n",
"\n",
"Gross Salary : 17900.00\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.9, Page number: 72
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate energy bill using the starting and ending meter reading.\n",
"#The charges are as follows:\n",
"\n",
"# No. of Units consumed Rates in (Rs.)\n",
"# 200 - 500 3.50\n",
"# 100 - 200 2.50\n",
"# Less than 100 1.50\n",
"\n",
"#Variable initialization\n",
"initial = raw_input(\"Initial & Final Readings : \")\n",
"final = raw_input(\"Initial & Final Readings : \")\n",
"\n",
"consumed = int(final) - int(initial)\n",
"\n",
"#Condition evaluation\n",
"if consumed >= 200 and consumed <= 500 :\n",
" total = consumed * 3.50\n",
"else:\n",
" if consumed >= 100 and consumed <= 199:\n",
" total = consumed * 2.50\n",
" else :\n",
" if consumed < 100 :\n",
" total = consumed * 1.50\n",
"\n",
"#Result\n",
"print \"Total bill for %d unit is %f\"%(consumed,total)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Initial & Final Readings : 1200\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Initial & Final Readings : 1500\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total bill for 300 unit is 1050.000000\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.10, Page number: 73
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate energy bill by reading the starting and ending meter reading.\n",
"#If the consumed electricity energy is greater than or equal to 200 units\n",
"#the rate should be 2.50/unit otherwise 1.50/unit.\n",
"\n",
"\n",
"#Variable initialization\n",
"initial = raw_input(\"Initial & Final Readings : \")\n",
"final = raw_input(\"Initial & Final Readings : \")\n",
"\n",
"consumed = int(final) - int(initial)\n",
"\n",
"#Condition evaluation\n",
"if consumed >= 200 :\n",
" total = consumed * 2.50\n",
"else:\n",
" total = consumed * 1.50\n",
"\n",
"#Result\n",
"print \"Total bill for %d unit is %f\"%(consumed,total)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total bill for 100 unit is 150.000000\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.11, Page number: 73
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Sort six numbers and find the largest one by using ladder of if else\n",
"\n",
"#Variable initialization\n",
"a = int(raw_input(\"Enter 1st number : \"))\n",
"b = int(raw_input(\"Enter 2nd number : \"))\n",
"c = int(raw_input(\"Enter 3rd number : \"))\n",
"d = int(raw_input(\"Enter 4th number : \"))\n",
"e = int(raw_input(\"Enter 5th number : \"))\n",
"f = int(raw_input(\"Enter 6th number : \"))\n",
"\n",
"#Condition evaluation and Result\n",
"if a > b and a > c and a > d and a > e and a > f :\n",
" print \"Highest of six Number is : %d\"%(a)\n",
"else:\n",
" if b > a and b > c and b > d and b > e and b > f :\n",
" print \"Highest of six Number is : %d\"%(b)\n",
" else:\n",
" if c > a and c > b and c > d and c > e and c > f :\n",
" print \"Highest of six Number is : %d\"%(c)\n",
" else:\n",
" if d > a and d > b and d > c and d > e and d > f :\n",
" print \"Highest of six Number is : %d\"%(d)\n",
" else:\n",
" if e > a and e > b and e > c and e > d and e > f :\n",
" print \"Highest of six Number is : %d\"%(e)\n",
" else:\n",
" print \"Highest of six Number is : %d\"%(f)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter 1st number : 52\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter 2nd number : 74\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter 3rd number : 90\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter 4th number : 45\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter 5th number : 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter 6th number : 22\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Highest of six Number is : 90\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.12, Page number: 74
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find largest number out of three numbers. Read the numbers through the keyboard\n",
"\n",
"\n",
"#Variable initialization\n",
"x = int(raw_input(\"\\nEnter Three Numbers x,y,z : \"))\n",
"y = int(raw_input(\"\\nEnter Three Numbers x,y,z : \"))\n",
"z = int(raw_input(\"\\nEnter Three Numbers x,y,z : \"))\n",
"\n",
"print \"\\nLargest out of Three Numbers is : \"\n",
"\n",
"#Condition evaluation and Result\n",
"if x > y :\n",
" if x > z:\n",
" print \"x = %d\\n\"%(x)\n",
" else:\n",
" print \"z = %d\\n\"%(z)\n",
"else:\n",
" if z > y:\n",
" print \"z = %d\\n\"%(z)\n",
" else:\n",
" print \"y = %d\\n\"%(y)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Three Numbers x,y,z : 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Three Numbers x,y,z : 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Three Numbers x,y,z : 30\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Largest out of Three Numbers is : \n",
"z = 30\n",
"\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.13, Page number: 75
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the smallest out of the three numbers.\n",
"\n",
"\n",
"#Variable initialization\n",
"a = int(raw_input(\"\\nEnter Three Numbers : \"))\n",
"b = int(raw_input(\"\\nEnter Three Numbers : \"))\n",
"c = int(raw_input(\"\\nEnter Three Numbers : \"))\n",
" \n",
"#Condition evaluation and Result\n",
"if a < b :\n",
" if a < c:\n",
" smallest = a\n",
" else:\n",
" smallest = c\n",
"else:\n",
" if b < c:\n",
" smallest = b\n",
" else:\n",
" smallest = c\n",
"\n",
"#Result\n",
"print \"The smallest of %d %d %d is %d\\n\"%(a,b,c,smallest)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Three Numbers : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Three Numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Three Numbers : 8\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The smallest of 1 5 8 is 1\n",
"\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate the total interest based on the following.\n",
"\n",
"#Principle Amount(Rs.) Rate of Interest(Rs.)\n",
"# >= 10000 20%\n",
"# >= 8000 & <= 9999 18%\n",
"# < 8000 16%\n",
"\n",
"\n",
"#Variable initialization\n",
"princ = int(raw_input(\"\\nEnter Loan & No. of years :- \"))\n",
"nyrs = int(raw_input(\"\\nEnter Loan & No. of years :- \"))\n",
"\n",
"#Condition evaluation\n",
"if princ >= 10000 :\n",
" rate = 20\n",
"else:\n",
" if princ >= 8000 and princ <= 9999 :\n",
" rate = 18\n",
" else:\n",
" if princ < 8000:\n",
" rate = 16\n",
"\n",
"#Calculation\n",
"interest = princ * nyrs * rate/100\n",
"\n",
"#Result\n",
"print \"Loan : %6.2f\"%(princ)\n",
"print \"Years : %6.2f\"%(nyrs)\n",
"print \"Rate : %6.2f\"%(rate)\n",
"print \"Interest : %6.2f\"%(interest)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Loan & No. of years :- 5500\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Loan & No. of years :- 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Loan : 5500.00\n",
"Years : 3.00\n",
"Rate : 16.00\n",
"Interest : 2640.00\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.16, Page number: 78
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the average of six subjects and display the results as follows.\n",
"\n",
"# AVERAGE RESULT\n",
"# >34 & <50 Third Division\n",
"# >49 & <60 Second Division\n",
"# >60 & <75 First Division\n",
"# >75 & <100 Distinction\n",
"\n",
"\n",
"#Variable initialization\n",
"a = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n",
"b = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n",
"c = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n",
"d = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n",
"e = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n",
"f = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n",
"\n",
"#Calculation\n",
"#here sum1 is used instead of sum because sum is a function in python.\n",
"sum1 = a + b + c + d + e + f\n",
"avg = sum1/6\n",
"\n",
"print \"Total : %d \\nAverage : %.2f\"%(sum1,avg)\n",
"\n",
"#Condition evaluation and Result\n",
"if a < 35 or b < 35 or c < 35 or d < 35 or e < 35 :\n",
" print \"Result : Fail\"\n",
" exit\n",
"if avg >= 34 and avg < 50 :\n",
" print \"Result : Third Division \"\n",
"else:\n",
" if avg >= 49 and avg < 60:\n",
" print \"Result : Second Division\"\n",
" else:\n",
" if avg >= 60 and avg > 75:\n",
" print \"Result : First Division\"\n",
" else:\n",
" if avg >= 75 and avg <= 100:\n",
" print \"Result : Distinction\"\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Marks\n",
"P C B M E H\n",
"75\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Marks\n",
"P C B M E H\n",
"75\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Marks\n",
"P C B M E H\n",
"75\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Marks\n",
"P C B M E H\n",
"75\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Marks\n",
"P C B M E H\n",
"75\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Marks\n",
"P C B M E H\n",
"75\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total : 450 \n",
"Average : 75.00\n",
"Result : Distinction\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.17, Page number: 80
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Detect the entered number as to whether it is even or odd. Use goto statement.\n",
"\n",
"\n",
"#Variable initialization\n",
"x = int(raw_input(\"\\nEnter a Number : \"))\n",
"\n",
"#Condition evaluation\n",
"#There is no goto/label statement in python\n",
"\n",
"if x%2 == 0:\n",
" print \"\\n%d is Even Number.\"%(x)\n",
"else:\n",
" print \"\\n%d is Odd Number.\"%(x)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter a Number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"5 is Odd Number.\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.18, Page number: 81
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate telephone bill. Transfer controls at different places according\n",
"#to number of calls and calculate the total charges. Follow rates as per\n",
"#given in the table.\n",
"\n",
"# Telephone call Rate in Rs.\n",
"# < 100 No charges\n",
"# > 99 & < 200 1\n",
"# > 199 & < 300 2\n",
"# > 299 3\n",
"\n",
"\n",
"#Variable initialization\n",
"nc = int(raw_input(\"\\nEnter Number of Calls : \"))\n",
"\n",
"#Condition evaluation\n",
"#There is no goto/label statement in python\n",
"\n",
"if nc < 100:\n",
" print \"\\nNo charges\"\n",
"else:\n",
" if nc > 99 and nc < 200:\n",
" print \"\\nTotal Charges : %d Rs.\"%(nc*1)\n",
" else:\n",
" if nc > 199 and nc < 300:\n",
" print \"\\nTotal Charges : %d Rs.\"%(nc*2)\n",
" else:\n",
" print \"\\nTotal Charges : %d Rs.\"%(nc*3)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Number of Calls : 500\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Total Charges : 1500 Rs.\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.19, Page number: 82
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Check whether the entered year is a leap year or not. Use goto statement.\n",
"\n",
"#Variable initialization\n",
"leap = int(raw_input(\"\\nEnter Year : \"))\n",
"\n",
"#Condition evaluation\n",
"#There is no goto/label statement in python\n",
"\n",
"if leap%4 == 0:\n",
" print \"\\n%d is a leap year.\"%(leap)\n",
"else:\n",
" print \"%d is not a leap year.\"%(leap)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Year : 2000\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"2000 is a leap year.\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the names of the days of a week.\n",
"\n",
"\n",
"#Variable initialization\n",
"day = int(raw_input(\"\\nEnter a number between 1 to 7 : \"))\n",
"\n",
"#Condition evaluation\n",
"#there is no switch case statement in python, an alternative is to use\n",
"#dictionary and functions\n",
"\n",
"def first():\n",
" print \"\\n1st day of week is Sunday\"\n",
"\n",
"def second():\n",
" print \"\\n2nd day of week is Monday\"\n",
"\n",
"def third():\n",
" print \"\\n3rd day of week is Tuesday\"\n",
"\n",
"def fourth():\n",
" print \"\\n4th day of week is Wednesday\"\n",
"\n",
"def fifth():\n",
" print \"\\n5th day of week is Thursday\"\n",
"\n",
"def sixth():\n",
" print \"\\n6th day of week is Friday\"\n",
"\n",
"def seventh():\n",
" print \"\\n7th day of week is Saturday\"\n",
"\n",
"def default():\n",
" print \"\\nInvalid day\"\n",
"\n",
"options = { 1 : first,\n",
" 2 : second,\n",
" 3 : third,\n",
" 4 : fourth,\n",
" 5 : fifth,\n",
" 6 : sixth,\n",
" 7 : seventh,\n",
" 0 : default,\n",
" 8 : default,\n",
" 9 : default\n",
"}\n",
"for i in range(1,day+1):\n",
" options[i]()\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter a number between 1 to 7 : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1st day of week is Sunday\n",
"\n",
"2nd day of week is Monday\n",
"\n",
"3rd day of week is Tuesday\n",
"\n",
"4th day of week is Wednesday\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display number of days in calendar format of an entered month of year 2001.\n",
"\n",
"import sys\n",
"\n",
"#Variable initialization\n",
"m = int(raw_input(\"Enter Month No. of Year 2001 : \"))\n",
"\n",
"#Condition evaluation\n",
"#there is no switch case statement in python, an alternative is to use\n",
"#else-if ladder.\n",
"\n",
"print \"\\nMonth - %d - 2001\"%(m)\n",
"print \"\\n\\tSUN\\tMON\\tTUE\\tWED\\tTHU\\tFRI\\tSAT\"\n",
"\n",
"#Find a day any month of 2001\n",
"\n",
"if m == 1:\n",
" a = 2\n",
" j = 31\n",
"else:\n",
" if m == 2:\n",
" a = 5\n",
" j = 28\n",
" else:\n",
" if m == 3:\n",
" a = 5\n",
" j = 31\n",
" else:\n",
" if m == 4:\n",
" a = 1\n",
" j = 30\n",
" else:\n",
" if m == 5:\n",
" a = 3\n",
" j = 31\n",
" else:\n",
" if m == 6:\n",
" a = 6\n",
" j = 30\n",
" else:\n",
" if m == 7:\n",
" a = 1\n",
" j = 31\n",
" else:\n",
" if m == 8:\n",
" a = 4\n",
" j = 31\n",
" else:\n",
" if m == 9:\n",
" a = 7\n",
" j = 30\n",
" else:\n",
" if m == 10:\n",
" a = 2\n",
" j = 31\n",
" else:\n",
" if m == 11:\n",
" a = 5\n",
" j = 30\n",
" else:\n",
" if m == 12:\n",
" a = 7\n",
" j = 31\n",
" else:\n",
" print \"Invalid Month\"\n",
" exit\n",
"\n",
"#Starting day is to be adjusted under the respective day\n",
"#sys.stdout.write() function performs the same task as printf() function\n",
" \n",
"i = 1\n",
"if a == 1:\n",
" sys.stdout.write(\"\\t%d\"%(i))\n",
"else:\n",
" if a == 2:\n",
" sys.stdout.write(\"\\t\\t%d\"%(i))\n",
" else:\n",
" if a == 3:\n",
" sys.stdout.write(\"\\t\\t\\t%d\"%(i))\n",
" else:\n",
" if a == 4:\n",
" sys.stdout.write(\"\\t\\t\\t\\t%d\"%(i))\n",
" else:\n",
" if a == 5:\n",
" sys.stdout.write(\"\\t\\t\\t\\t\\t%d\"%(i))\n",
" else:\n",
" if a == 6:\n",
" sys.stdout.write(\"\\t\\t\\t\\t\\t\\t%d\"%(i))\n",
" else:\n",
" if a == 7:\n",
" sys.stdout.write(\"\\t\\t\\t\\t\\t\\t\\t%d\"%(i))\n",
"\n",
"\n",
"h = 8 - a #The starting day is subtracted from 8\n",
"for i in range(2,h+1): #to display the first row\n",
" sys.stdout.write(\"\\t%d\"%(i))\n",
"sys.stdout.write(\"\\n\")\n",
"b = 1\n",
"for i in range(h+1,j+1): #to continue with second row onwards\n",
" if b == 8:\n",
" sys.stdout.write(\"\\n\")\n",
" b = 1\n",
" sys.stdout.write(\"\\t%d\"%(i))\n",
" b += 1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Month - 1 - 2001\n",
"\n",
"\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n",
"\t\t1\t2\t3\t4\t5\t6\n",
"\t7\t8\t9\t10\t11\t12\t13\n",
"\t14\t15\t16\t17\t18\t19\t20\n",
"\t21\t22\t23\t24\t25\t26\t27\n",
"\t28\t29\t30\t31"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.27, Page number: 95
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert decimal to hexadecimal number.\n",
"\n",
"import sys\n",
"import turtle\n",
"\n",
"#Variable initialization\n",
"x = int(raw_input(\"Enter a number : \"))\n",
"y = 30\n",
"\n",
"#Condition evaluation\n",
"#there is no switch case statement in python, an alternative is to use\n",
"#else-if ladder.\n",
"\n",
"print \"\\nConversion of Decimal to Hexadecimal Number\"\n",
"\n",
"#for loop without condition is not supported in python. so while loop is used.\n",
"\n",
"c = 1\n",
"z = [0 for i in range(0,15)]\n",
"\n",
"while x != 0:\n",
" z[c] = x % 16\n",
" c = c + 1\n",
" x = x/16\n",
"\n",
"\n",
"for i in range(c-1,0,-1):\n",
" if z[i] == 10:\n",
" sys.stdout.write(\"A\")\n",
" else:\n",
" if z[i] == 11:\n",
" sys.stdout.write(\"B\")\n",
" else:\n",
" if z[i] == 12:\n",
" sys.stdout.write(\"C\")\n",
" else:\n",
" if z[i] == 13:\n",
" sys.stdout.write(\"D\")\n",
" else:\n",
" if z[i] == 14:\n",
" sys.stdout.write(\"E\")\n",
" else:\n",
" if z[i] == 15:\n",
" sys.stdout.write(\"F\")\n",
" else:\n",
" sys.stdout.write(\"%d\"%(z[i]))\n",
" \n",
" \n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a number : 31\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Conversion of Decimal to Hexadecimal Number\n",
"1F"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.28, Page number: 97
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Detect whether the entered number is even or odd. use nested switch-case\n",
"\n",
"\n",
"#Variable initialization\n",
"x = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Condition evaluation\n",
"#there is no switch case statement in python, an alternative is to use\n",
"#else-if ladder.\n",
"\n",
"if x == 0:\n",
" print \"Number is Even\"\n",
"else:\n",
" if x == 1:\n",
" print \"Number is odd\"\n",
" else:\n",
" y = x % 2\n",
" if y == 0:\n",
" print \"Number is Even\"\n",
" else:\n",
" print \"Number is odd\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Number is odd\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.29, Page number: 98
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Count number of 1s, 0s, blank spaces and others using nested\n",
"#switch() statement in a given stream\n",
"\n",
"import sys\n",
"\n",
"#Variable initialization\n",
"txt = raw_input(\"Enter Numbers : \")\n",
"\n",
"#Processing\n",
"x = 0\n",
"s = 0\n",
"a = 0\n",
"z = 0\n",
"o = 0\n",
"\n",
"while x < len(txt):\n",
" if txt[x] == ' ':\n",
" s = s + 1\n",
" else:\n",
" if txt[x] == '1':\n",
" a = a + 1\n",
" else:\n",
" if txt[x] == '0':\n",
" z = z + 1\n",
" else:\n",
" o = o + 1\n",
" x = x + 1\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nTotal Spaces : %d\"%(s))\n",
"sys.stdout.write(\"\\nTotal 1s : %d\"%(a))\n",
"sys.stdout.write(\"\\nTotal 0s : %d\"%(z))\n",
"sys.stdout.write(\"\\nOthers : %d\"%(o))\n",
"sys.stdout.write(\"\\nString Length : %d\"%(s+a+z+o))\n",
" \n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Numbers : 1110022 222\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Total Spaces : 1\n",
"Total 1s : 3\n",
"Total 0s : 2\n",
"Others : 5\n",
"String Length : 11"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.30, Page number: 99
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert integer to character using if condition\n",
"\n",
"\n",
"#Variable initialization\n",
"x = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Condition evaluation\n",
"#ord() function converts the character ASCII to integer\n",
"\n",
"if x == ord('A'):\n",
" print \"%c\"%(x)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 65\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"A\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 5.31, Page number: 100
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Use nested if..else statements in switch() statement. Also show the effect\n",
"#of conversion of integer to character\n",
"\n",
"\n",
"#Variable initialization\n",
"i = int(raw_input(\"Enter any ASCII Number : \"))\n",
"\n",
"#Condition evaluation\n",
"#there is no switch..case statement in python. alternative is to use\n",
"#if..else and else..if ladder\n",
"\n",
"if i == ord('A'):\n",
" print \"Capital A\"\n",
"else:\n",
" if i == ord('B'):\n",
" print \"Capital B\"\n",
" else:\n",
" if i == ord('C'):\n",
" print \"Capital C\"\n",
" else:\n",
" if i > 47 and i < 58:\n",
" print \"Digit : [%c]\"%(i)\n",
" else:\n",
" if i >= 58 and i <= 64:\n",
" print \"Symbol : [%c]\"%(i)\n",
" else:\n",
" if i > 64 and i < 91:\n",
" print \"Capital : [%c]\"%(i)\n",
" else:\n",
" if i > 96 and i < 123:\n",
" print \"Small : [%c]\"%(i)\n",
" else:\n",
" print \"Invalid Choice\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any ASCII Number : 65\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Capital A\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}