You are on page 1of 20

1: DATA TYPES AND VARIABLES

Q1. Write a Python statement to display the message “Python is interesting”.


Solution
print( '''Python is interesting''')
Q2. Write a Python script to display the message- Mike raised his hands to heaven
and said, " Don’t worry!!! He is watching all of this from above. He will help us."
Solution
print(''' Mike raised his hands to heaven and said, “ Don’t worry!!! He is watching all
of this from above” ''')
Q3. Write a Python script to input two numbers and find their sum.
Solution
a=oval(input(“enter a number”))
b=eval(input(“enter second number”))
sum=a+b
print(“the sum is”,sum)
Q4. Write a Python script to input side of a square and display its area.
Solution
a=eval(input(“enter side of a square”))
Area=a**2
print(“This is the area”,Area)
Q5. Write a Python script to input length and breadth of a rectangle and display its
area and perimeter.
Solution
l=eval(input(“enter length of a rectangle”))
b=eval(input(“enter breadth of a rectangle”))
p=2*(l+b)
a=l*b
print(“this is the perimeter”,p,”this is the area”,a)
Q6. Write a Python script to input an integer and display its square and cube.
Solution
a=eval(input(“enter a number”))
s=a**2
c=a**3
print(“this is the square”,s,”this is the cube”,c)
Q7. Write a Python script to swap two numbers.
Solution
a=int(input("enter 1st num"))
b=int(input("enter 2nd num"))
c=b
d=a
print("1st num is",c)
print("2nd num is",d)
Q8. Write a Python script to input a number and print its preceding and succeeding
number.
Solution
a=int(input(“enter a num”))
pre=a-1
suc=a+1
print(“the preceeding term is”,pre)
print(“the succeeding term is”,suc)
Q9. Write a Python script to input cost price and selling price of an item display
profit/loss.
Solution
cp=eval(input(“enter cost price”))
sp=eval(input(“enter selling price”))
if cp>sp:
print(“There is a loss of” cp-sp)
elif sp>cp:
print(“there is a profit of”,sp-cp)
else :
print(“there is no profit no loss”)
Q10. Write a Python script to input a number and print its first five multiples.
Solution
a=int(input(“enter a num”))
mul=a*2
mul2=a*3
mul3=a*4
mul4=a*5
print(“the first five multiples are”,a,mul,mul2,mul3,mul4)
Q11. Write a Python script to input name, class, section and subject and display all
information in the format of name tag.
Solution
name=input(“enter name”)
Class1=int(input(“enter class”)
sec=input(“enter section”)
sub=input(“enter subject”)
print(“Name:”,name,”\nClass:”,class1,”\nSection:”,sec,”\nSubject:”,sub)

2: OPERATORS AND EXPRESSIONS

Q1. Write a Python script to input roll number and marks in three subjects of a
student. Calculate and display the total and percentage.
Solution
roll=input("roll number")
e=eval(input("Enter marks of eng"))
a=eval(input("Enter marks of math"))
s=eval(input("Enter marks of science"))
num=(e+a+s)
Per=((num*100)/300)
print(“total marks”, num)
print("percentage", Per)
Q2. Write a Python script to input a temperature in Celsius and convert it into
Fahrenheit.
Solution
print("Temperature Converter")
a=eval(input("Temperature in Celsius"))
b=(9/5)*a
f=b+32
print("Temperature in Fahrenheit", f)
Q3. Write a Python script to input the values of a,b,c and calculate the roots of the
quadratic equation.
Solution
print("Solving Quadratic Equation")
a=eval(input("Enter coefficient of x^2: "))
b=eval(input("Enter coefficient of x: "))
c=eval(input("Enter constant value: "))
d=b**2
f=4*a*c
h=d-f
x1=(-b+h**0.5)/(2*a)
x2=(-b-h**0.5)/(2*a)
print("First root: ", x1)
print("Second root: ", x2)
Q4. Write a Python script to input sides of a triangle and calculate its area.
Solution
p=eval(input("Length of a"))
b=eval(input("Length of b"))
c=eval(input("Length of c"))
s=(p+b+c)/2
Area=((s*(s-p)*(s-b)*(s-c))**0.5)
print("the Area is: ", Area)
Q5. Write a Python script to input the values of Principal, Rate, and Time and
calculate Simple Interest and Compound Interest.
Solution
p=eval(input("Enter Principal:"))
r=eval(input("Enter Rate of Interest:"))
t=eval(input("Enter the time period:"))
s=(p*r*t)/100
print("Simple Interest: ", s)
c=(p*(1+r/100)**t)
print("Compound Interest: ", c)
Q6. Write a Python script to input employee number, basic, HRA, DA and IT of an
employee. Calculate and display the net salary. (net salary=basic+HRA+DA-IT)
Solution
n=(input("Enter Employee Name"))
a=eval(input("Enter Employee Number"))
b=eval(input("Enter Basic"))
m=eval(input("Enter HRA"))
k=eval(input("Enter DA"))
l=eval(input("Enter IT"))
tot=(b+m+k)-l
print("NET SALARY", tot)
Q7. Write a Python script to input Item number, Price/unit, quantity and discount.
Calculate and display the net amount to be paid.
Solution
input("Item name: ")
input("Item number: ")
a=eval(input("Price per item: "))
b=eval(input("Quanity: "))
d=eval(input("Discount: "))
f=a*b
m=d*a
e=m//100
p=f-e
print("FINAL AMOUNT:", p)
Q8. Write a Python script to input time in seconds and display the number of hours,
minutes and seconds in the given seconds.
Solution
s=int(input("Enter time in seconds: "))
h=s//3600
m=s%3600
m1=m//60
s2=m%60
print("Time: ", "Hours:", h, "Minutes:", m1, "Seconds:", s2)
Q9. Write a Python script to input length in meters and convert it into centimeters.
Solution
a=eval(input(“enter length in metres”))
b=a*100
print(“length in centimetres is”, b))
Q10. Write a Python script to input a three digits number and find the sum of its
digits.
Solution
num=eval(input("Enter a 3 digit number: "))
a=num%10
num=num//10
b=num%10
num=num//10
print("The sum of the digits is: ", a+b+num)

3: SELECTION STATEMENT
IF STATEMENT

Q1. Write a program to input a year in 4-digit form and check whether it is a leap
year or not and display proper message.
Hint: A leap year is exactly divisible by 4 except for century years (years ending with
00). The century year is a leap year only if it is perfectly divisible by 400.
Solution
y=int(input('Enter Year: '))
if y%4==0:
if y%100==0:
if y%400==0:
print('The year is leap year')
else:
print('Not a leap year')
else:
print('The year is leap year')
else:
print('The year is not leap year')
Q2. Write a program to input a character and swap it's case(i.e. If it is lowercase
letter, convert it to uppercase and vice versa).
Solution
ch=input("Enter a character")
if ch>="A" and ch<="Z":
print(chr(ord(ch)+32))
else:
print(chr(ord(ch)-32))
Q3. Write a program to input a number, check whether it is positive or negative and
display the appropriate message.
Solution
n=eval(input('Enter no. here '))
if n>0:
print('Positive')
else:
print('Negative')
Q4. Write a program to input cost price and selling price of an item and calculate
profit and loss on a transaction.
Solution
cp=eval(input('Enter Cost price here '))
sp=eval(input('Enter Selling price here '))
if sp>cp:
print('Profit=',sp-cp,'Rs.')
else:
print('Loss=',cp-sp,'Rs.')
Q5. Write a program to input a number and check whether it is even or odd and
display the appropriate message.
Solution
n=eval(input('Enter no. here '))
if n%2==0:
print('The no. is even')
else:
print('The no. is odd')
Q6. Write a program to input two numbers and check whether both are divisible by
5.
Solution
n1=eval(input('Enter the first no. here '))
n2=eval(input('Enter the second no. here ' ))
if n1%5==0 and n2%5==0:
print('Both the numbers are divisible by 5')
else :
print('Both the numbers are not divisible by 5')
Q7. Write a program to input a number and check whether it is divisible by 8 or not.
(If the last three digits of a whole number are divisible by 8, then the entire number
is divisible by 8.)
Solution
a=eval(input('Enter no. here '))
b=a%1000
if b%8==0:
print('The no. is divisible by 8')
else:
print('The no. is not divisible by 8')
Q8. Write a program to read the age of a candidate and determine whether it is
eligible for casting his/her own vote.
Solution
age=int(input('Enter your age '))
if age>=18:
print('You are eligible for voting')
else:
print('You are not eligible for voting')
Q9. Write a program to input a character and display whether it is a vowel or a
consonant.
Solution
ch=input('Enter character here')
if ch in 'AEIOUaeiou':
print('Vowel')
else:
print('Consonant')

4: IF-ELIF STATEMENT
Q10. Write a program to input co-efficients of a, b and c of a quadratic equation
ax2 + bx + c = 0, where a = 0. Discriminant, D = b2- 4ac.If discriminant is 0 then
print there is exactly one real root, if discriminant is positive then print there are two
distinct roots, if discriminant is negative then print there are no real roots.
Solution
a=eval(input("Enter the coefficient of x^2 "))
b=eval(input('Enter the coefficient of x '))
c=eval(input('Enter the constant term '))
D=b**2-4*a*c
if D>=0:
print('There are two distinct roots')
elif D==0:
print('There is exactly one real root')
else:
print('The equation have no real roots')
Q11. Write a program to input three sides of a triangle and display whether it is
EQUILATERAL, SCALENE, RIGHT ANGLED or ISOSCELES.
Solution
a=eval(input('Enter side 1 on triangle '))
b=eval(input('Enter side 2 on triangle '))
c=eval(input('Enter side 3 on triangle '))
if a==b==c:
print('The triangle is an equilateral triangle')
elif a**2==b**2+c**2 or c**2==a**2+b**2 or b**2==a**2+c**2:
print('The triangle is a right angled triangle')
elif a==b!=c or c==b!=a or a==c!=b:
print('The triangle is an isoceles triangle')
else:
print('The triangle is an scalene triangle')
Q12. Write a program to accept a date(dd/mm/yyyy) and check for its validity.
Solution
i=int(input("Enter today's date here in the format dd mm yyyy "))
d=i//1000000
i=i%1000000
m=i//10000
i=i%10000
if d<=31 and m in(1,3,5,7,8,10,12):
print(" Valid Date")
elif d<=30 and m in(4,6,9,11):
print(" Valid Date")
elif d<=29 and m==2:
print("Valid Date")
else:
print('The date is not valid')
Q13. Write a program to input temperature of water and display its state (Liquid,
solid or gas).
Solution
temp=eval(input('Enter temperature of water here '))
if temp>100:
print('Steam')
elif temp<0:
print('Ice')
else:
print('Liquid')
Q14. Write a program to input a character and check whether a character is an
alphabet, digit or special character.
Show/Hide Solution
wo=input('Enter a character to check what it is ')
if wo>='A' and wo<='Z' or wo>='a' and wo<='z':
print('Alphabet')
elif wo in '1234567890':
print('Number')
else:
print('Special character')
Q15. Write a program to accept a coordinate point in a XY coordinate system and
determine in which quadrant(Ist, IInd, IIrd or IVth) the coordinate point lies.
Show/Hide Solution
x=eval(input('Enter the x-coordinate '))
y=eval(input('Enter the y-coordinate '))
if x>0 and y>0:
print('I quadrant')
elif x<0 and y>0:
print('II quadrant')
elif x<0 and y<0:
print('III quadrant')
else:
print('IV quadrant')
Q16. Write a program to read any day number in integer and display day name in
the word.
Show/Hide Solution
day=int(input('Enter the day no. to check what day to is. [1 is for Sunday] '))
if day==1:
print('Sunday')
elif day==2:
print('Monday')
elif day==3:
print('Tuesday')
elif day==4:
print('Wednesday')
elif day==5:
print('Thursday')
elif day==6:
print('Friday')
else:
print('Saturday')
Q17. Write a program to input salary and calculate commission for the salesman.
The commission is calculated as follows:
Salary Comission
30001 onwards 15%
22001 - 30000 10%
12001 – 22000 7%
5001 – 12000 3%
0 - 5000 0%
Show/Hide Solution
print('Commission for the salesman')
a=int(input('Enter the amount in rupees= '))
if a>=30001:
print('Commission=Rs.',(15/100)*a)
elif a>=22001:
print('Commission=Rs.',(10/100)*a)
elif a>=12001:
print('Commission=Rs.',(7/100)*a)
elif a>=5001:
print('Commission=Rs.',(3/100)*a)
else:
print("Commission=Rs.",(0/100)*a)
Q18. Write a program to input the name, subject, salary and experience (in years) of
a teacher. An allowance is given according to the following condition:
If salary >10,000 or experience > 10 years, allowance is 20% of salary.
If salary is in the range 5000-10000 and experience >5years, allowance is 15% of
salary.
If salary >3000, allowance is 10%.
Otherwise if salary <3000, allowance is 6%.
Find the total salary (salary+allowance) and print it along with other details.
Show/Hide Solution
name=input('Enter your name ')
sub=input('Enter the subject you teach ')
sal=eval(input('Enter your salary '))
exp=int(input('Enter your experience in years '))
print('Your name- ',name)
print('Your subject- ',sub)
print('Your experience- ',exp)
print('Initial salary= ',sal)
if sal>10000 or exp>10:
allow=(20/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
elif 5000<sal<10000 and exp>5:
allow=(15/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
elif sal>3000:
allow=(10/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
else:
allow=(6/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
Q19. Write a program to calculate and print the Electricity bill of a given customer.
The customer id., name and unit consumed by the user should be taken from the
keyboard and display the total amount to pay to the customer. The charge are as
follow:
If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum bill
should be of Rs. 150/-
<Show/Hide Solution
id=eval(input("Enter the customer I'd here "))
name=input('Enter your name ')
unit=eval(input('Enter the no. of units consumed '))
c1=0
c2=0
if unit<=200:
c1=0
elif unit<=400:
c1=200*0+(unit-200)*1.20

elif unit<=600:
c1=200*0+(200)*1.20+(unit-400)*1.60

elif unit>600:
c1=200*0+(200)*1.20+(200)*1.60+(unit-600)*2.00

if c1<150:
c1=150
c2=0
elif c1>400:
c2=15/100*c1
print('Your total bill= Rs.',c2+c1)

4: PYTHON LISTS

Q1. Write Python script to display the element with maximum value from the given
list.
Show/Hide Program
list=[11,12,34,77,99,34,23,22]
a=max(list)
print(a)
Q2. Write Python script to display the element with minimum value from the given
list.
Show/Hide Program
list=[11,12,34,77,99,34,23,22]
b=min(list)
print(b)
Q3. Write Python script to display the sum of elements from the given list of
numbers.
Show/Hide Program
list=[11,12,34,77,99,34,23,22]
n=0
m=len(list)
for i in list:
n=n+i
print(list)
print("sum of the list elements is",n)
Q4. Write Python script to display the average of elements from the given list of
numbers.
Show/Hide Program
list=[11,12,34,77,99,34,23,22]
n=0
m=len(list)
for i in list:
n=n+i
print(list)
print("Average of the list elements is", n/m)
Q5. Writ a Python script to input a list of numbers and a number. Check whether the
given number is present in the list or not.
Show/Hide Program
a=eval(input(“enter a list “))
b=eval(input(“enter a number”))
flag=0
for i in a:
if i!=b:
continue
else:
flag=1
if flag==1:
print(“present”)
else:
print(“not present")
Q6. Write a Python script to input a list and find all duplicates and display number of
times they are present in the list.
Show/Hide Program
a=input(“enter a list”)
r=max(n)
for I in range(r+1):
c=a.count(I)
if c!=0:
print(I, “has appeared “,c ,”times in the list”)

5: PYTHON DICTIONARY

Q1. Write Python script to create a dictionary with class and the name of a class
teacher and delete a particular class and its teacher name.
Show/Hide Program
dict1={'6':'Mrs Sapna', '7':'Mrs Vanya', '8':'Mrs Shubha', '9':'Mr Ramesh'}
clas=input("Enter class")
dict1.pop(clas)
print(dict1)
Q2. Write Python script to input country name and capital of 5 countries. Add them
to a dictionary and also display it.
Show/Hide Program
cc={}
for i in range(5):
country=input("Enter Country name")
capital=input("Enter capital")
cc[country]=capital
print(cc)
Q3. Write Python script to create a dictionary with famous monuments and their
location. Input name of any monument and check whether that monument is present
in the dictionary or not. If the monument is not present in the dictionary then add it to
the dictionary.
Show/Hide Program
m1={'Taj Mahal':'Agra', 'Hawa Mahal': 'Jaipur', 'Victoria Memorial': 'Kolkata'}
monument=input("Enter monument name")
location=input("Enter location")
if monument in m1:
print(m1.get(monument,"Present"))
else:
m1[monument]=location
print(m1)
Q4. Write Python script to create a dictionary student with rollnumber as the key and
his name and marks in three subjects as the values corresponding to the each key.
Store data of five students in the dictionary and display it.
Show/Hide Program
student={}
for i in range(5):
rollno=input("Enter Roll number")
name=input("Enter Name")
m1=int(input("Enter marks in 1st subject"))
m2=int(input("Enter marks in 2nd subject"))
m3=int(input("Enter marks in 3rd subject"))
student[rollno]=[name,m1,m2,m3]
print(student)
Q5. Write Python script to create a dictionary batsman with batsman name as the
key and his single runs, fours, sixes and total score as the values corresponding to
the each key. Calculate total score as the sum of single runs, fours and sixes. Store
data of five batsman in the dictionary and display it.
Show/Hide Program
batsman={}
for i in range(5):
name=input("Enter name")
single=int(input("Enter single runs"))
fours=int(input("Enter fours"))
sixes=int(input("Enter sixes"))
totrun=single+4*fours+6*sixes
batsman[name]=[single, fours, sixes, totrun]
print(batsman)

6: PYTHON TUPLES
Q1. Write Python script to display the element with maximum value from the given
tuple.
Show/Hide Program
tup=(11,12,34,77,99,34,23,22)
print(max(tup) )
Q2. Write Python script to display the element with minimum value from the given
tuple.
Show/Hide Program
tup=(11,12,34,77,99,34,23,22)
print(min(tup) )
Q3. Write Python script to display the sum of elements from the given tuple of
numbers.
Show/Hide Program
tup=(11,12,34,77,99,34,23,22)
n=0
m=len(tup)
for i in tup:
n=n+i
print("sum of the elements of tuple is:", n)
Q4. Write Python script to display the average of elements from the given tuple of
numbers.
Show/Hide Program
tup=(11,12,34,77,99,34,23,22)
n=0
m=len(tup)
for i in tup:
n=n+i
print("Average of the elements of tuple is:", n/m)
Q5. Write a Python script to input a tuple with names of dance forms. Input a dance
form and add this dance form at end of the tuple.
Show/Hide Program
tup=input("enter a tuple of dance forms")
tup2=input("enter new dance form")
tup3=tup+(tup2,)
print(tup3)

7:PYTHON STRINGS

Q1. Write Python script to input a string and a character and count the number of
occurrences of the character in the string.
Show/Hide Solution
str=input("Enter a string")
chr=input("Enter a character")
c=str.count(chr)
print("Total number of occurrences are:", c)
Q2. Write Python script to input a string and display it in reverse order.
Show/Hide Solution
str=input("Enter a string")
rev=str[::-1]
print("Reversed string is:", rev)
Q3. Write Python script to input a string and check whether it is a palindrome or not.
Show/Hide Solution
str=input("Enter a string")
rev=str[::-1]
if str==rev:
print("Palindrome")
else:
print("Not a palindrome")
Q4. Write Python script to input a string and count the number of words in it.
Show/Hide Solution
str=input("Enter a string")
c=str.count(" ")
print("No. of words are:",c+1)
Q5. Write Python script to input a string and count the number of words beginning
with 'A' or 'a'.
Show/Hide Solution
str=input("Enter a string")
x=str.count(" A")
y=str.count(" a")
if str[0]=="a":
y=y+1
if str[0]=="A":
x=x+1
print("No. of words starting with A are:",x)
print("No. of words starting with a are:",y)
Q6. Write Python script to input a string and replace the first letter of every word to
uppercase and then display it.
Show/Hide Solution
str=input("Enter a string")
x=str.title()
print(x)
Q7. Write Python script to input a string and replace all occurrences of the word ‘the’
with ‘that’.
Show/Hide Solution
str=input("Enter a string")
x=str.replace("the","that")
print(x)
Q8. Write Python script to input a string and count and display the number of capital
alphabets, small alphabets and numbers.
Show/Hide Solution
str=input("Enter a string")
x,y,z=0,0,0
for i in str:
if i.isupper():
x=x+1
elif i.islower():
y=y+1
elif i.isdigit():
z=z+1
print("Upper case: ", x)
print("Lower case: ", y)
print("Numbers: ", z)
UnitCharge/unit
upto 199Free
200 and above but less than 400@1.20
400 and above but less than 600@1.60
600 and above@2.00

You might also like