Python Practice Star Questions
Python Practice Star Questions
Solution 1-
r=int(input("radius= "))
area=3.14*r**2
circumfrance=2*3.14*r
print(area,'is the area' )
print(circumfrance,'is the circumfrance')
Output-
Program 2-
WAP to make a simple calculator that reads two numbers and an operator. On selection of an
operator, perform operation and print the result as shown below-
Solution 2-
def add(x,y):
return x+y
def subtract(x,y):
return x-y
def multiply(x,y):
return x*y
def devide(x,y):
return x/y
operator=input('operator=(+ , - , * , /)')
if operator in('+','-','*','/'):
n1=int(input('first number='))
n2=int(input('second number='))
if operator=='+':
print('result=',n1,'+',n2,'=',add(n1,n2))
elif operator=='-':
print('result=',n1,'-',n2,'=',subtract(n1,n2))
elif operator=='*':
print('result=',n1,'*',n2,'=',multiply(n1,n2))
elif operator=='/':
print('result=',n1,'/',n2,'=',devide(n1,n2))
Output-
Program 3-
You are driving a little too fast and the police officer stops you and issues a ticket. Write code
to compute the result, encoded as an integer value: 0=no ticket, 1=small ticket, 2=big ticket.
If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If
speed is 81 or more, the result is 2. Unless it is your birthday, on that day, your speed can be
5 higher in all cases. WAP to model above scenario.
Solution 3-
print('0= no ticket')
print('1= small ticket')
print('2= big ticket')
s=int(input('SPEED='))
b=int(input('Is it your bday (0=yes,1=no)? '))
if b==1:
if s<=60:
print('no ticket')
elif s>=61 and s<=80:
print('small ticket')
elif s>=81:
print('big ticket')
if b==0:
if s<=65:
print('no ticket')
elif s>=68 and s<=85:
print('small ticket')
elif s>=86:
print('big ticket')
Output-
Program 4-
1. Write a program to print following patterns upto N lines
i) ii)
Solution 4-
A-n=int(input("How many stars in the central line?"))
for i in range(1,(n+1)):
star = 1
space = lines - 1
Output-
Program 5-
WAP that reads n digit number. After reading the number, compute and display the sum of
the odd positioned digits, multiply all even positioned digits and add these two numbers.
Solution 5-
def reverse(i):
rev = 0
while (i != 0):
rev = (rev * 10) + (i % 10)
i //= 10
return rev
def getSumProduct(i):
i = reverse(i)
sumOdd = 0
prodEven = 1
j = 1
while (i != 0):
if (j % 2 == 0):
prodEven *= i % 10
else:
sumOdd += i % 10
i //= 10
j += 1
print("Sum of odd =", sumOdd)
print("Product of even =", prodEven)
print('Ans=',(sumOdd+prodEven))
i = int(input('Number='))
getSumProduct(i)
Output-
Program 6-
Write a program which will find all such numbers which are divisible by 7 but are not
multiple of 5, between 2000 and 3200 (both included).The numbers obtained should be
printed in a comma-separated sequence on a single line.
Solution 6-
for i in range(2000,3201):
if (i%7==0) and (i%5!=0):
print(i,end=(','))
Output-
Program 7-
Develop a program to classify students amongst various categories as per their age entered.
Read age of N students and count no of students falling in each category as described below
print a report as follows –
Group A: 12 yrs and above but less than 15 yrs - XX
Group B: 15 yrs and above but less than 17 yrs - XX
Group C: 17 yrs and above but less than 19 yrs - XX
Group D: Lesser than 12 yrs - XX
Solution 7-
n=int(input('enter age'))
def agen(n):
count_A=0
count_B=0
count_C=0
count_D=0
if (n>=12) and (n<15):
count_A +=1
elif (n>=15) and (n<17):
count_B +=1
elif (n>=17) and (n<19):
count_C +=1
if (n<12):
count_D +=1
print('Group A: 12 yrs and above but less than 15 yrs=',count_A)
print('Group B: 15 yrs and above but less than 17 yrs=',count_B)
print('Group C: 17 yrs and above but less than 19 yrs=',count_C)
print('Group D: Lesser than 12 yrs=',count_D)
agen(n)
Output-
Program 8-
Write a program to find if a number entered is a palindrome or not
Solution 8-
Number = int(input("Please Enter any Number: "))
n=Number
Reverse = 0
while(Number > 0):
Reminder = Number %10
Reverse = (Reverse *10) + Reminder
Number = Number //10
Output-
Program 9-
According to a study, the approximate level of Intelligence of a person can be calculated using the
following formula:i=2(y+0.5x).Write program, which will produce a table of values of i,y and
xwhere y varies from 1 to 6 and for each value of y , x varies from 5.5 to 12.5 in steps of 0.5.
Solution 9-
y=1
x=5.5
while y>=1 and y<=6:
y+=0.5
while x>5 and x<13:
x+=0.5
i=(2*y)+x
print(i,end=",")
Output-
Program 10-
WAP to print the sum of n terms of thefollowing series
A)x+ x2/2 + x3/3 + x4/4 +....+xn/n
B) 1/ (sqrt(2) + 2 / sqrt(3) + 3/ sqrt(4) +.......n/sqrt(n+1)
Solution 10-
A-x=int(input('Enter the value of x'))
import math
sum=0
for i in range(1,n+1):
sum+=i/math.sqrt(i+1)
print('Sum of the series is',sum)
Output-
Program 11-
Read two strings and check if string1 is prefix, postfix or nothing from the string2.
Solution 11-
a='evergreen'
b='ever'
l=len(b)
if b==a[0:l]:
print('prefix')
elif b==a[l-1:]:
print('postfix')
Output-
Program 12-
Output-
Program 13-
In Cryptography, a Caesar cipher is a very simple encryption techniques in which each letter
in the plain text is replaced by a letter some fixed number of positions down the alphabet list
in a cyclic manner. For example, with a shift of 3, A would be replaced by D, B would
become E, and so on. The method is named after Julius Caesar, who used it to communicate
with his generals. ROT-13 ("rotate by 13 places").
Create menu driven program to encrypt and decrypt which takes in a string and rotation
(rotate by n places) and encrypts the string then decrypts the string. The program should be
menu driven. Also if rotation is not specified then the encryption should take place by 13.
Solution 13-
Program 14-
Write a program to sort a given list of numbers and print the total number of operations
taking place while sorting the given list using
Bubble Sort
Insertion Sort
Solution 14-
# bubble sort
data = [2, 45, 1, 0, 11, 9]
c=0
for i in range(len(data)):
for j in range(0, len(data) - i - 1):
if data[j] > data[j + 1]:
(data[j], data[j + 1]) = (data[j + 1], data[j])
c=c+1
print('Sorted list in Asc ending Order:', data)
print('NUmber of operation performed=',c)
# insertion sort
data = [9, 5, 1, 4, 3, 37]
c=0
for step in range(1, len(data)):
key = data[step]
j = step - 1
while j >= 0 and key < data[j]:
data[j + 1] = data[j]
j = j - 1
data[j + 1] = key
c=c+1
print('Sorted list in Asc ending Order:',data)
print('NUmber of operation performed=',c)
Output-
Program 15-
Write a program which takes 2 digits, X, Y as input and generates a 2-dimensional array. The
element value in thei-th row and j-th column of the array should be i*j.
Solution 15-
print(multi_list)
Output-
Program 16-
Write a program to generate a random 6 digits secure OTP. The digits should not repeat
Solution 16-
import random
import math
digits = [i for i in range(0, 10)]
random_str = ""
for i in range(6):
index = math.floor(random.random() * 10)
random_str += str(digits[index])
print("OTP=",random_str)
Output-
Program 17-
Output-
Program 18-
Write a program that takes a list of words and creates a dictionary with word as the key and
number of occurrences of the word as the value
Solution 18-
def word_count(str):
counts = dict()
words = str.split()
print(words)
return counts