#Check whether the entered number is less than 10? if yes, display the same
#Variable initialization
v = raw_input("Enter the number : ")
#In python, raw_input() is used to read values
v = int(v)
#Result
if v < 10 :
print '\nNumber entered is less than 10'
#Check equivalence of two numbers. Use if statement.
#Variable initialization
m = raw_input("Enter two numbers : ")
n = raw_input("Enter two numbers : ")
#In python, raw_input() is used to read values
m = int(m)
n = int(n)
#Result
if m - n == 0 :
print '\nTwo numbers are equal'
#Check whether the candidate's age is greater than 17 or not. If yes, display
#message "Eligible for Voting"
#Variable initialization
age = raw_input("Enter age : ")
age = int(age)
#In python, raw_input() is used to read values
#Result
if age > 17 :
print '\nEligible for Voting.'
#Use curly brace in the if block. Enter only the three numbers and calculate
#their sum and multiplication
#Variable initialization
#in python,the map(int,raw_input().split()) function splits the input characters
#and converts it into integers and returns a value if all the values are integers
#otherwise an exception will be generated and initializes v as 0.
#using exception handling, this result can be achieved.
#Exception handling
try:
#use space to separate the input characters
v = raw_input("Enter Three Numbers : ")
a,b,c = map(int,v.split())
x = 3
except:
x = 0
#Result
#in python, block of statements are identified using indentation
if x == 3:
print "Addition : %d"%(a+b+c)
print "Multiplication : %d"%(a*b*c)
#Read the values of a,b,c through the keyboard. Add them and after addition
#check if it is in the range of 100 & 200 or not. Print separate message for each.
#Variable initialization
a = raw_input("Enter Three Numbers a b c : ")
b = raw_input("Enter Three Numbers a b c : ")
c = raw_input("Enter Three Numbers a b c : ")
a = int(a)
b = int(b)
c = int(c)
print 'a = %d b = %d c = %d'%(a,b,c)
#Calculation
d = a + b + c
#Condition evaluation and Result
if d <= 200 and d >= 100 :
print "Sum is %d which is in between 100 & 200"%(d)
else:
print "\nSum is %d which is out of range"%(d)
#Find the roots of a quadratic equation by using if else condition
#Variable initialization
a = raw_input("Enter Values for a, b, c : ")
b = raw_input("Enter Values for a, b, c : ")
c = raw_input("Enter Values for a, b, c : ")
a = int(a)
b = int(b)
c = int(c)
#Condition evaluation and Result
if b * b > 4 * a * c :
x1 = b + sqrt(b*b - 4*a*c)/2*a
x2 = b - sqrt(b*b - 4*a*c)/2*a
print "\nx1 = %f x2 = %f"%(x1,x2)
else:
print "\nRoots are Imaginary"
#Calculate the square of those numbers only whose least significant digit is 5.
#Variable initialization
s = raw_input("Enter a Number : ")
s = int(s)
d = s % 10;
#Condition evaluation and Result
#There is no increment/decrement operator in python.
if d==5 :
s = s/10
s += 1
print "\nSquare = %d%d"%(s*(s-1),d*d)
else:
print "\nInvalid Number"
#Calculate the salary of medical representative based on the sales. Bonus and
#incentive to be offered to him will be based on total sales. If the sale
#exceeds Rs.100000/- follow the particulars of table (1) otherwise (2)
# 1. TABLE 2. TABLE
# Basic = Rs. 3000 Basic = Rs. 3000
# HRA = 20% of basic HRA = 20% of basic
# DA = 110% of basic DA = 110% of basic
# Conveyance = Rs. 500 Conveyance = Rs. 500
# Incentive = 10% of sales Incentive = 5% of sales
# Bonus = Rs. 500 Bonus = Rs. 200
#Variable initialization
sale = raw_input("Enter Total Sales in Rs. : ")
sale = int(sale)
#Condition evaluation
if sale >= 100000 :
bs = 3000
hra = 20 * bs/100
da = 110 * bs/100
cv = 500
incentive = sale * 10/100
bonus = 500
else:
bs = 3000
hra = 20 * bs/100
da = 110 * bs/100
cv = 500
incentive = sale * 5/100
bonus = 200
#Result
ts = bs + hra + da + cv + incentive + bonus
print "\nTotal Sales : %.2f"%(sale)
print "\nBasic Salary : %.2f"%(bs)
print "\nHra : %.2f"%(hra)
print "\nDa : %.2f"%(da)
print "\nConveyance : %.2f"%(cv)
print "\nIncentive : %.2f"%(incentive)
print "\nBonus : %.2f"%(bonus)
print "\nGross Salary : %.2f"%(ts)
#Calculate energy bill using the starting and ending meter reading.
#The charges are as follows:
# No. of Units consumed Rates in (Rs.)
# 200 - 500 3.50
# 100 - 200 2.50
# Less than 100 1.50
#Variable initialization
initial = raw_input("Initial & Final Readings : ")
final = raw_input("Initial & Final Readings : ")
consumed = int(final) - int(initial)
#Condition evaluation
if consumed >= 200 and consumed <= 500 :
total = consumed * 3.50
else:
if consumed >= 100 and consumed <= 199:
total = consumed * 2.50
else :
if consumed < 100 :
total = consumed * 1.50
#Result
print "Total bill for %d unit is %f"%(consumed,total)
#Calculate energy bill by reading the starting and ending meter reading.
#If the consumed electricity energy is greater than or equal to 200 units
#the rate should be 2.50/unit otherwise 1.50/unit.
#Variable initialization
initial = raw_input("Initial & Final Readings : ")
final = raw_input("Initial & Final Readings : ")
consumed = int(final) - int(initial)
#Condition evaluation
if consumed >= 200 :
total = consumed * 2.50
else:
total = consumed * 1.50
#Result
print "Total bill for %d unit is %f"%(consumed,total)
#Sort six numbers and find the largest one by using ladder of if else
#Variable initialization
a = int(raw_input("Enter 1st number : "))
b = int(raw_input("Enter 2nd number : "))
c = int(raw_input("Enter 3rd number : "))
d = int(raw_input("Enter 4th number : "))
e = int(raw_input("Enter 5th number : "))
f = int(raw_input("Enter 6th number : "))
#Condition evaluation and Result
if a > b and a > c and a > d and a > e and a > f :
print "Highest of six Number is : %d"%(a)
else:
if b > a and b > c and b > d and b > e and b > f :
print "Highest of six Number is : %d"%(b)
else:
if c > a and c > b and c > d and c > e and c > f :
print "Highest of six Number is : %d"%(c)
else:
if d > a and d > b and d > c and d > e and d > f :
print "Highest of six Number is : %d"%(d)
else:
if e > a and e > b and e > c and e > d and e > f :
print "Highest of six Number is : %d"%(e)
else:
print "Highest of six Number is : %d"%(f)
#Find largest number out of three numbers. Read the numbers through the keyboard
#Variable initialization
x = int(raw_input("\nEnter Three Numbers x,y,z : "))
y = int(raw_input("\nEnter Three Numbers x,y,z : "))
z = int(raw_input("\nEnter Three Numbers x,y,z : "))
print "\nLargest out of Three Numbers is : "
#Condition evaluation and Result
if x > y :
if x > z:
print "x = %d\n"%(x)
else:
print "z = %d\n"%(z)
else:
if z > y:
print "z = %d\n"%(z)
else:
print "y = %d\n"%(y)
#Find the smallest out of the three numbers.
#Variable initialization
a = int(raw_input("\nEnter Three Numbers : "))
b = int(raw_input("\nEnter Three Numbers : "))
c = int(raw_input("\nEnter Three Numbers : "))
#Condition evaluation and Result
if a < b :
if a < c:
smallest = a
else:
smallest = c
else:
if b < c:
smallest = b
else:
smallest = c
#Result
print "The smallest of %d %d %d is %d\n"%(a,b,c,smallest)
#Calculate gross salary for the conditions given below
# BASIC SALARY(Rs.) DA(Rs.) HRA(Rs.) CONVEYANCE(Rs.)
# >= 5000 110% of basic 20% of basic 500
# bs>=3000 & bs<5000 100% of basic 15% of basic 400
# bs<3000 90% of basic 10% of basic 300
#Variable initialization
bs = int(raw_input("\nEnter Basic Salary : "))
#Condition evaluation
if bs >= 5000 :
hra = 20 * bs/100
da = 110 * bs/100
cv = 500
else:
if bs >= 3000 and bs < 5000 :
hra = 15 * bs/100
da = 100 * bs/100
cv = 400
else:
if bs < 3000:
hra = 10 * bs/100
da = 90 * bs/100
cv = 300
#Calculation
ts = bs + hra + da + cv
#Result
print "Basic Salary : %5.0f"%(bs)
print "Hra : %5.0f"%(hra)
print "Da : %5.0f"%(da)
print "Conveyance : %5.0f"%(cv)
print "Gross Salary : %5.0f"%(ts)
#Calculate the total interest based on the following.
#Principle Amount(Rs.) Rate of Interest(Rs.)
# >= 10000 20%
# >= 8000 & <= 9999 18%
# < 8000 16%
#Variable initialization
princ = int(raw_input("\nEnter Loan & No. of years :- "))
nyrs = int(raw_input("\nEnter Loan & No. of years :- "))
#Condition evaluation
if princ >= 10000 :
rate = 20
else:
if princ >= 8000 and princ <= 9999 :
rate = 18
else:
if princ < 8000:
rate = 16
#Calculation
interest = princ * nyrs * rate/100
#Result
print "Loan : %6.2f"%(princ)
print "Years : %6.2f"%(nyrs)
print "Rate : %6.2f"%(rate)
print "Interest : %6.2f"%(interest)
#Find the average of six subjects and display the results as follows.
# AVERAGE RESULT
# >34 & <50 Third Division
# >49 & <60 Second Division
# >60 & <75 First Division
# >75 & <100 Distinction
#Variable initialization
a = int(raw_input("\nEnter Marks\nP C B M E H\n"))
b = int(raw_input("\nEnter Marks\nP C B M E H\n"))
c = int(raw_input("\nEnter Marks\nP C B M E H\n"))
d = int(raw_input("\nEnter Marks\nP C B M E H\n"))
e = int(raw_input("\nEnter Marks\nP C B M E H\n"))
f = int(raw_input("\nEnter Marks\nP C B M E H\n"))
#Calculation
#here sum1 is used instead of sum because sum is a function in python.
sum1 = a + b + c + d + e + f
avg = sum1/6
print "Total : %d \nAverage : %.2f"%(sum1,avg)
#Condition evaluation and Result
if a < 35 or b < 35 or c < 35 or d < 35 or e < 35 :
print "Result : Fail"
exit
if avg >= 34 and avg < 50 :
print "Result : Third Division "
else:
if avg >= 49 and avg < 60:
print "Result : Second Division"
else:
if avg >= 60 and avg > 75:
print "Result : First Division"
else:
if avg >= 75 and avg <= 100:
print "Result : Distinction"
#Detect the entered number as to whether it is even or odd. Use goto statement.
#Variable initialization
x = int(raw_input("\nEnter a Number : "))
#Condition evaluation
#There is no goto/label statement in python
if x%2 == 0:
print "\n%d is Even Number."%(x)
else:
print "\n%d is Odd Number."%(x)
#Calculate telephone bill. Transfer controls at different places according
#to number of calls and calculate the total charges. Follow rates as per
#given in the table.
# Telephone call Rate in Rs.
# < 100 No charges
# > 99 & < 200 1
# > 199 & < 300 2
# > 299 3
#Variable initialization
nc = int(raw_input("\nEnter Number of Calls : "))
#Condition evaluation
#There is no goto/label statement in python
if nc < 100:
print "\nNo charges"
else:
if nc > 99 and nc < 200:
print "\nTotal Charges : %d Rs."%(nc*1)
else:
if nc > 199 and nc < 300:
print "\nTotal Charges : %d Rs."%(nc*2)
else:
print "\nTotal Charges : %d Rs."%(nc*3)
#Check whether the entered year is a leap year or not. Use goto statement.
#Variable initialization
leap = int(raw_input("\nEnter Year : "))
#Condition evaluation
#There is no goto/label statement in python
if leap%4 == 0:
print "\n%d is a leap year."%(leap)
else:
print "%d is not a leap year."%(leap)
#Print lines by selecting the choice.
print "\n[1] ............"
print "\n[2] ____________"
print "\n[3] ************"
print "\n[4] ============"
print "\n[5] EXIT"
#Variable initialization
ch = int(raw_input("\nENTER YOUR CHOICE : "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#dictionary and functions
def dot():
print "\n......................................................"
def line():
print "\n______________________________________________________"
def star():
print "\n******************************************************"
def equal():
print "\n======================================================"
def ex():
exit
options = {1 : dot,
2 : line,
3 : star,
4 : equal,
5 : ex
}
options[ch]()
#Provide multiple functions such as 1. addition 2.Subtraction 3. Multiplication
#4.Division 5.Remainder calculation 6.Larger out of two by using switch statement
print "\n============================="
print "\n\t\tMENU"
print "\n============================="
print "\n\t[1] ADDITION"
print "\n\t[2] SUBTRACTION"
print "\n\t[3] MULTIPLICATION"
print "\n\t[4] DIVISION"
print "\n\t[5] REMAINDER"
print "\n\t[6] LARGER OUT OF TWO"
print "\n\t[0] EXIT"
print "\n============================="
#Variable initialization
ch = int(raw_input("\nENTER YOUR CHOICE : "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#dictionary and functions
if ch <= 6 and ch > 0:
a = int(raw_input("Enter Two Numbers : "))
b = int(raw_input("Enter Two Numbers : "))
def add():
c = a + b
print "\nAddition : %d"%(c)
def sub():
c = a - b
print "\nSubtraction : %d"%(c)
def mul():
c = a * b
print "\nMultiplication : %d"%(c)
def div():
c = a/b
print "\nDivision : %d"%(c)
def rem():
c = a % b
print "\nRemainder : %d"%(c)
def larger():
if a > b:
print "\n%d (a) is larger than %d (b)"%(a,b)
else:
if b > a:
print "\n%d (b) is larger than %d (a)"%(b,a)
else:
print "\n%d (a) & %d (b) are same"%(a,b)
def ex():
print "\nTerminated by choice"
exit
def default():
print "\nInvalid Choice"
options = { 1 : add,
2 : sub,
3 : mul,
4 : div,
5 : rem,
6 : larger,
7 : ex,
0 : default,
8 : default,
9 : default
}
options[ch]()
#Convert years into Minutes, Hours, Days, Months and Seconds using switch()
#statement
print "[1] MINUTES"
print "[2] HOURS"
print "[3] DAYS"
print "[4] MONTHS"
print "[5] SECONDS"
print "[0] EXIT"
#Variable initialization
ch = int(raw_input("\nEnter Your Choice : "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#dictionary and functions
if ch > 0 and ch < 6:
yrs = int(raw_input("Enter Years : "))
#Calculation
#since min is a keyword in python, min1 is used instead.
mon = yrs * 12
ds = mon * 30
hrs = ds * 24
min1 = hrs * 60
se = min1 * 60
def minute():
print "\nMinutes : %ld"%(min1)
def hours():
print "\nHours : %ld"%(hrs)
def days():
print "\nDays : %ld"%(ds)
def months():
print "\nMonths : %ld"%(mon)
def seconds():
print "\nSeconds : %ld"%(se)
def ex():
print "\nTerminated by choice"
exit
def default():
print "\nInvalid Choice"
options = { 1 : minute,
2 : hours,
3 : days,
4 : months,
5 : seconds,
6 : ex,
0 : default,
8 : default,
9 : default
}
options[ch]()
#Display the names of the days of a week.
#Variable initialization
day = int(raw_input("\nEnter a number between 1 to 7 : "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#dictionary and functions
def first():
print "\n1st day of week is Sunday"
def second():
print "\n2nd day of week is Monday"
def third():
print "\n3rd day of week is Tuesday"
def fourth():
print "\n4th day of week is Wednesday"
def fifth():
print "\n5th day of week is Thursday"
def sixth():
print "\n6th day of week is Friday"
def seventh():
print "\n7th day of week is Saturday"
def default():
print "\nInvalid day"
options = { 1 : first,
2 : second,
3 : third,
4 : fourth,
5 : fifth,
6 : sixth,
7 : seventh,
0 : default,
8 : default,
9 : default
}
for i in range(1,day+1):
options[i]()
#Perform following operations
#1. Display any numbers or stars on the screen by using for loop
#2. Display the menu containing the following
#a) Whole screen b)Half screen c)Top 3 lines d)Bottom 3 lines
import os
import sys
import turtle
#Print numbers in the whole screen
for i in range(1,700):
sys.stdout.write("%d"%i)
print "\nCLEAR MENU"
print "\t1] Whole Screen\t2] Half Screen\t3]Top 3 lines\t4] Bottom 3 lines\t5] Exit"
#Variable initialization
c = int(raw_input("Enter Your Choice : "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#dictionary and functions
def one():
os.system('cls')
def two():
for i in range(0,190):
turtle.goto(i,1)
sys.stdout.write("\t")
def three():
for i in range(1,100):
turtle.goto(i,1)
sys.stdout.write("\t")
def four():
for i in range(1,120):
turtle.goto(i,21)
sys.stdout.write("\t")
def five():
exit
options = { 1 : one,
2 : two,
3 : three,
4 : four,
5 : five
}
options[c]()
#Display the files of current directory
import os
print "\nFILE LISTING MENU"
print "1] .EXE"
print "2] .BAT"
print "3] .OBJ"
print "4] .BAK"
#Variable initialization
c = int(raw_input("Enter Your Choice -: "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#dictionary and functions
def one():
os.system('dir *.exe')
def two():
os.system('dir *.c')
def three():
os.system('dir *.obj')
def four():
os.system('dir *.bak')
options = { 1 : one,
2 : two,
3 : three,
4 : four
}
options[c]()
#Display number of days in calendar format of an entered month of year 2001.
import sys
#Variable initialization
m = int(raw_input("Enter Month No. of Year 2001 : "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#else-if ladder.
print "\nMonth - %d - 2001"%(m)
print "\n\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT"
#Find a day any month of 2001
if m == 1:
a = 2
j = 31
else:
if m == 2:
a = 5
j = 28
else:
if m == 3:
a = 5
j = 31
else:
if m == 4:
a = 1
j = 30
else:
if m == 5:
a = 3
j = 31
else:
if m == 6:
a = 6
j = 30
else:
if m == 7:
a = 1
j = 31
else:
if m == 8:
a = 4
j = 31
else:
if m == 9:
a = 7
j = 30
else:
if m == 10:
a = 2
j = 31
else:
if m == 11:
a = 5
j = 30
else:
if m == 12:
a = 7
j = 31
else:
print "Invalid Month"
exit
#Starting day is to be adjusted under the respective day
#sys.stdout.write() function performs the same task as printf() function
i = 1
if a == 1:
sys.stdout.write("\t%d"%(i))
else:
if a == 2:
sys.stdout.write("\t\t%d"%(i))
else:
if a == 3:
sys.stdout.write("\t\t\t%d"%(i))
else:
if a == 4:
sys.stdout.write("\t\t\t\t%d"%(i))
else:
if a == 5:
sys.stdout.write("\t\t\t\t\t%d"%(i))
else:
if a == 6:
sys.stdout.write("\t\t\t\t\t\t%d"%(i))
else:
if a == 7:
sys.stdout.write("\t\t\t\t\t\t\t%d"%(i))
h = 8 - a #The starting day is subtracted from 8
for i in range(2,h+1): #to display the first row
sys.stdout.write("\t%d"%(i))
sys.stdout.write("\n")
b = 1
for i in range(h+1,j+1): #to continue with second row onwards
if b == 8:
sys.stdout.write("\n")
b = 1
sys.stdout.write("\t%d"%(i))
b += 1
#Convert decimal to hexadecimal number.
import sys
import turtle
#Variable initialization
x = int(raw_input("Enter a number : "))
y = 30
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#else-if ladder.
print "\nConversion of Decimal to Hexadecimal Number"
#for loop without condition is not supported in python. so while loop is used.
c = 1
z = [0 for i in range(0,15)]
while x != 0:
z[c] = x % 16
c = c + 1
x = x/16
for i in range(c-1,0,-1):
if z[i] == 10:
sys.stdout.write("A")
else:
if z[i] == 11:
sys.stdout.write("B")
else:
if z[i] == 12:
sys.stdout.write("C")
else:
if z[i] == 13:
sys.stdout.write("D")
else:
if z[i] == 14:
sys.stdout.write("E")
else:
if z[i] == 15:
sys.stdout.write("F")
else:
sys.stdout.write("%d"%(z[i]))
#Detect whether the entered number is even or odd. use nested switch-case
#Variable initialization
x = int(raw_input("Enter a Number : "))
#Condition evaluation
#there is no switch case statement in python, an alternative is to use
#else-if ladder.
if x == 0:
print "Number is Even"
else:
if x == 1:
print "Number is odd"
else:
y = x % 2
if y == 0:
print "Number is Even"
else:
print "Number is odd"
#Count number of 1s, 0s, blank spaces and others using nested
#switch() statement in a given stream
import sys
#Variable initialization
txt = raw_input("Enter Numbers : ")
#Processing
x = 0
s = 0
a = 0
z = 0
o = 0
while x < len(txt):
if txt[x] == ' ':
s = s + 1
else:
if txt[x] == '1':
a = a + 1
else:
if txt[x] == '0':
z = z + 1
else:
o = o + 1
x = x + 1
#Result
sys.stdout.write("\nTotal Spaces : %d"%(s))
sys.stdout.write("\nTotal 1s : %d"%(a))
sys.stdout.write("\nTotal 0s : %d"%(z))
sys.stdout.write("\nOthers : %d"%(o))
sys.stdout.write("\nString Length : %d"%(s+a+z+o))
#Convert integer to character using if condition
#Variable initialization
x = int(raw_input("Enter a Number : "))
#Condition evaluation
#ord() function converts the character ASCII to integer
if x == ord('A'):
print "%c"%(x)
#Use nested if..else statements in switch() statement. Also show the effect
#of conversion of integer to character
#Variable initialization
i = int(raw_input("Enter any ASCII Number : "))
#Condition evaluation
#there is no switch..case statement in python. alternative is to use
#if..else and else..if ladder
if i == ord('A'):
print "Capital A"
else:
if i == ord('B'):
print "Capital B"
else:
if i == ord('C'):
print "Capital C"
else:
if i > 47 and i < 58:
print "Digit : [%c]"%(i)
else:
if i >= 58 and i <= 64:
print "Symbol : [%c]"%(i)
else:
if i > 64 and i < 91:
print "Capital : [%c]"%(i)
else:
if i > 96 and i < 123:
print "Small : [%c]"%(i)
else:
print "Invalid Choice"