Solutions To Python by S Arora
Solutions To Python by S Arora
Solutions To Python by S Arora
Page 174
Solution :
a=5
print a
print a*2
print (a*2)-1
Q2) Write a Python program that accepts radius of a circle and prints its area.
Solution :
Q3) Write a python program that accepts marks in 5 subjects and outputs average
marks.
Solution :
Q4) Write a short program that asks for your height in centimeters and then converts
your height to feet and inches.
Solution :
Q5) Write a program to read a no. n and print n², n³ and n⁴.
Solution :
Solution :
Solution :
Solution :
Q9) Write a program to read details like name, class, age of a student and then print the
details firstly in same line and then in separate lines.
Have 2 blank lines in these 2 different types of prints.
Solution :
Q10) Write a program to read three numbers in three variables and swap first to
variables with the sums of first and second, second and third numbers respectively.
Solution :
Page 214
Q1) Write a program to obtain principal amount, rate of interest and time from user and
compute simple interest.
Solution :
Solution :
Solution :
Solution :
Solution :
Solution :
Solution :
Solution :
import random
print "Lucky Draw Started!"
a=random.randint(101,111)
print "Selected candidate 1 is ",a
b=random.randint(101,111)
if a==b:
print"Both draws came same so re-
drawing another one"
c=random.randint(101,110) #just a
preventive measure
print "Selected candidate 2 is ",c
else:
print "Selected candidate 2 is ",b
Chapter 9 - User Defined Functions
[Solutions for the book "Cs with Python"
by Sumita Arora for class 11]
Page 277
Q1) Write a function that takes amount in dollars and dollars to rupee conversion price; it
then returns the amount converted to rupees. Create the function in both void and non-
void forms.
Solution :
Solution :
def boxvolume(l,b,h):
l=20.0
b=25.5
h=40.0
res=l*b*h
print res," is the volume of box. "
x=input("Enter length : ")
y=input("Enter breadth : ")
z=input("Enter height : ")
boxvolume(x,y,z)
Q3) Write a program to have following functions :
(i) a function that takes a number as argument and calculates cube for it. The function
does not return a value. If there is no value passed to the function in function call, the
function should calculate the cube of 2.
(ii) a function that takes two char arguments and return True if both the arguments are
equal otherwise False.
Test bot these function by giving appropriate function call statements.
Solution :
def cuberoot(x=2):
print x**3, "is the value "
x=input("Enter the no. to calculate cube
")
cuberoot(x)
Part (ii)
def charcomp(a,b):
if a==b:
print "True"
else:
print "False"
x=raw_input("Enter a character ")
y=raw_input("Enter second character ")
charcomp(x,y)
Ques 4) Write a function that receives two numbers and generates a random number
from that range. Using this function, the main program should be able to print three
numbers randomly.
Solution :
import random
def myfunc(a,b):
c=random.randint(a,b)
print a,c,b
x=input("Enter a number ")
y=input("Enter 2nd number ")
myfunc(x,y)
Ques 5) Write a function that receives two string arguments and checks wether they are
same length strings (returns True in this case otherwise false)
Solution :
def stringcomp(a,b):
if len(a)==len(b):
print "True"
else:
print "False"
x=raw_input("Enter a string ")
y=raw_input("Enter another string ")
stringcomp(x,y)
Ques 6) Write a function namely nthRoot() that receives two parameters x and n and
returns the nth root of x. I.e x^1/n
Solution :
def nthRoot(x,n):
res=x**(1.0/n)
print res
a=input("Enter x ")
b=input("Enter n ")
nthRoot(a,b)
Ques 7) Write a function that takes a number n and then returns a randomly generated
number hacing exactly n digits(not starting with zero) e.g. if n is 2 then function can
randomly return a number 10-99 but 07,02 etc are not valid 2 digit no.s
Solution :
import random
def myfunc(x):
start=int('1'+'0'*(x-1))
print start
stop=int('9'*x)
print stop
print random.randrange(start,stop+1)
a=input("Enter any no. ")
myfunc(a)
[OR]
from random import randint
def randomdigits(n):
rangestart=10**(n-1)
rangeend=(10**n)-1
print randint(rangestart,rangeend)
x=input("Enter a digit ")
randomdigits(x)
Ques 8) Write a function that takes two numbers and returns the number that has
minimum one's digit.
Solution :
def onesplace(a,b):
lastdigita=a%10
lastdigitb=b%10
if lastdigita
print a
else:
print b
x=input("Enter any number ")
y=input("Enter another number ")
onesplace(x,y)
Chapter 12 - String Manipulation
[Solutions for the book "Cs with Python"
by Sumita Arora for class 11]
Page 377
Q)Write a program that prompts for a phone number of 10 digits and two dashes, with
dashes after area code and the next three numbers.
Solution :
Solution :