Sie sind auf Seite 1von 12

Chapter 6 - Python Fundamentals

[Solutions for the book "Cs with Python"


by Sumita Arora for class 11]

Page 174

Q1) Write a program that generates the following output :


5
10
9

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 :

#takes radius and prints area


a=input("Enter radius ")
res=3.14*(a**2)
print "Area of circle is :",res

Q3) Write a python program that accepts marks in 5 subjects and outputs average
marks.

Solution :

a=input("Enter Marks of subject 1 ")


b=input("Enter Marks of subject 2 ")
c=input("Enter Marks of subject 3 ")
d=input("Enter Marks of subject 4 ")
e=input("Enter Marks of subject 5 ")
avg=(a+b+c+d+e)/2
print "Average marks : ",avg

Q4) Write a short program that asks for your height in centimeters and then converts
your height to feet and inches.

Solution :

a=input("Enter your height in centimeters


")
inch= a/2.54
foot= inch/12
print "Your height in inches is
:",inch,"in foot :",foot

Q5) Write a program to read a no. n and print n², n³ and n⁴.

Solution :

n=input("Enter any number :")


print n,n**2,n**3,n**4

Q6) Write a program to find area of a triangle.

Solution :

h=input("Enter height of the triangle :


")
b=input("Enter length of base of triangle
:")
area= 0.5*b*h
print "Area of the triangle is :",area

Q7) Write a program to compute simple interest and compound interest .

Using above formula for compound interest

Solution :

#For Simple Interest


p=input("Enter Principal amount ")
r=input("Enter rate ")
t=input("Enter time (in years) ")
si=(p*r*t)/100
print "Simple interest is : ",si
#For Compound Interest
p=input("Enter Principal amount ")
r=input("Enter rate ")
t=input("Enter time ")
n=input("Enter no. of times it is
compounded ")
q=n*t
A=(p*(1+(r/n)))**q
print "Compound interest is : ",A
Q8) Write a program to input a number and print its first five multiples.

Solution :

n=input("Enter the no. to calculate


multiples ")
print n*1 #Can also be done using range
but this chapter
teaches python fundamentals.
print n*2
print n*3
print n*4
print n*5

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 :

n=raw_input("Enter name ")


c=raw_input("Enter Class ")
a=input("Enter age ")
print n,c,a
print
print
print n
print c
print a

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 :

a=input("Enter any no. ")


b=input("Enter another no. ")
c=input("Enter another number ")
a=a+b
b=b+c
print a,b,c
Chapter 7 - Data Handling
[Solutions for the book "Cs with Python"
by Sumita Arora for class 11]

Page 214

Q1) Write a program to obtain principal amount, rate of interest and time from user and
compute simple interest.

Solution :

p=input("Enter principal amount")


r=input("Enter rate : ")
t=input("Enter time : ")
si=(p*r*t)/100
print "The simple interest is ",si
Q2) Write a program to obtain temperatures of 7 days and then display average
temperatures of the week.

Solution :

a=input("Enter temp. for Monday ")


b=input("Enter temp. for Tuesday ")
c=input("Enter temp. for Wednesday ")
d=input("Enter temp. for Thursday ")
e=input("Enter temp. for Friday ")
f=input("Enter temp. for Saturday ")
g=input("Enter temp. for Sunday ")
avg=(a+b+c+d+e+f+g)/2
print "The avg. temp. for the week was
",avg
Q3)Write a program to obtain x,y,z from user and calculate expression :
4x^4+3y^3+9z+6π

Solution :

x=input("Enter value for x ")


y=input("Enter value for y ")
z=input("Enter value for z ")
res=(4*(x**4))+(3(y**3))+(9*z)+6(3.14)
print "Value of the expression is :",res
Q4)Write a program that inputs two numbers a and ab and computes |a-b|/(a+b)

Solution :

a=input("Enter value for a ")


b=input("Enter value for b ")
n=a-b
res=abs(n)/(a+b) #abs here is used to
keep a-b positive
Q5)Write a program that reads a number of seconds and prints it in the form: mins and
seconds.

Solution :

a=input("Enter in seconds ")


mins=a//60 #as // returns only quotient
secs=a%60 #as % returns only remainder
print "The time in mins and secs is ",
mins,"Mins.", secs,"Secs."
Q6)Write a program that reads from user (i) an hour between 1 to 12 and (ii) no.s of
hours ahead. The program should then print the time after those many hours.

Solution :

x=input("Enter hour between 1-12 ")


y=input("Enter how many hours ahead? ")
a=x+y
if a>12:
d=a-12
print "Time at that time would be
",d,"O'clock"
else:
print "Time at that time would be
",a,"O'clock"
Chapter 8 - Introducing Functions & Modules
[Solutions for the book "Cs with Python"
by Sumita Arora for class 11]
Page 240
Q1)Define a function called Jimmy....

Solution :

Lack of time [Just use print command in


every line and design it.]
Q4) For admission in a school, there are 10 applicants... (Pretty long question tho..)

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 :

def nonvoiddtor(x,y): #Nonvoid


res=x*y
return res
x=input("Enter the conversion price ")
y=input("Enter amount to be converted
($$) ")
print "The prince in Rupee is : ",
voiddtor(x,y)

def voiddtor(x,y): #Void


res=x*y
print "The price converted in Rupee
is : ",res
x=input("Enter the conversion price ")
y=input("Enter amount to be converted
($$) ")
nonvoiddtor(x,y)
Q2) Write a function to calculate volume of a box with appropriate default values for its
parameters. Your function should have the following input parameters: (a) length of box;
(b) width of box; (c)height of box.
Test it by writing complete program to invoke it.

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 :

a=raw_input("Enter your phone no. in


format [eg - 942-559-2555] ")
if len(a)==12:
if a[3]=='-' and a[7]=='-':
print "Valid"
else:
print "Invalid"
else:
print "You've not entered correct
phone no./ proper format "
Q2) Write a program that (i) Prompt the user for a string (ii) extract all the digits from the
string. (iii) If there are digits sum the collected digits else print "The string has no digits"

Solution :

a=raw_input("Enter no.s ")

Das könnte Ihnen auch gefallen