0% found this document useful (0 votes)
144 views11 pages

Python Practice Star Questions

The programs provide solutions to various problems involving basic programming concepts like loops, conditional statements, functions etc. Program 1 calculates area and circumference of a circle given radius. Program 2 implements a basic calculator that reads operands and operator and performs the calculation. Program 3 models a traffic ticket scenario based on speed and birthday.

Uploaded by

harshit suri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
144 views11 pages

Python Practice Star Questions

The programs provide solutions to various problems involving basic programming concepts like loops, conditional statements, functions etc. Program 1 calculates area and circumference of a circle given radius. Program 2 implements a basic calculator that reads operands and operator and performs the calculation. Program 3 models a traffic ticket scenario based on speed and birthday.

Uploaded by

harshit suri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Program 1-

WAP to calculate the area and circumference of a circle if radius is given.

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-

Enter first number: 5


Enter second number: 7
Enter operator: *
Result: 5 * 7 = 35

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)):

print(" "*(n-1),"* "*i)

B- lines = int(input('Enter number of lines: '))

star = 1
space = lines - 1

for row in range(1, lines + 1):


print((' ' * space) + ('*' * star))
space -= 1
star += 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

print("Reverse of entered number is = ",Reverse)


if (Reverse==n):
print('It is a Palendrome')
else:
print('It is not a Palendrome')

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'))

n=int(input('Enter the value of n'))


import math
sum=0
for i in range(1,n+1):
sum+=math.pow(x,i)/i
print('Sum of the series is',sum)
B-n=int(input('Enter the value of n'))

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-

Write a program to read a string and print


i)Frequency of all charactersii) Word of highest length
iii) The string in title case iv) Read full name as a string and print only the initials
Solution 12-

s='I study in Amity International School'


print('Frequency of all characters= option1')
print('Word of highest length= option2')
print('The string in title case= option3')
print('Read full name as a string and print only the initials= option4'
)
r=int(input('enter number:'))
if r==1:
all_freq = {}
for i in s:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print ("Count of all characters in string is :",str(all_freq))
elif r==2:
biggest=max(s.split(),key=len)
print('Word of highest length=',biggest)
elif r==3:
c=s.title()
print(c)
elif r==4:
l = s.split()
new = ""
for i in range(len(l)-1):
s = l[i]
new += (s[0].upper()+'.')
new += l[-1].title()
print(new)
else:
print('enter a valid option')

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-

text=input('enter text: ')


r=int(input('no of rotations(for default enter "0")= '))
cipher=' '
print("For encryption press 1")
print("For decryption press 2")
o=int(input('Chose option (1/2): '))
if r=='none':
if o==1:
for char in text:
if char==' ':
cipher+=char
elif char.isupper():
cipher+=chr((ord(char)+13-65)+65)
elif char.islower():
cipher+=chr((ord(char)+13-97)+97)
print("Encrypted text=",cipher)
elif o==2:
for ch in text:
if ch==' ':
cipher+=ch
elif ch.isupper():
cipher+=chr((ord(ch)-13-65)+65)
elif ch.islower():
cipher+=chr((ord(ch)-13-97)+97)
print("Decrypted text=",cipher)
else:
if o==1:
for char in text:
if char==' ':
cipher+=char
elif char.isupper():
cipher+=chr((ord(char)+r-65)+65)
elif char.islower():
cipher+=chr((ord(char)+r-97)+97)
print("Encrypted text=",cipher)
elif o==2:
for ch in text:
if ch==' ':
cipher+=ch
elif ch.isupper():
cipher+=chr((ord(ch)-r-65)+65)
elif ch.islower():
cipher+=chr((ord(ch)-r-97)+97)
print("Decrypted text=",cipher)
Output-

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.

Note: i=0,1.., X-1; j=0,1,¡Y-1.


Example
Suppose the following inputs are given to the program:
3,5
Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

Solution 15-

row_num = int(input("Input number of rows: "))


col_num = int(input("Input number of columns: "))
multi_list = [[0 for col in range(col_num)] for row in range(row_num)]

for row in range(row_num):


for col in range(col_num):
multi_list[row][col]= row*col

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-

Write a program to find the second largest number in a given list.


Solution 17-

list1 = [10, 20, 4, 45, 99]


new_list = list1
new_list.remove(max(new_list))
print("Second biggest no=",max(new_list))

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)

for word in words:


if word in counts:
counts[word] += 1
else:
counts[word] = 1

return counts

print( word_count('the of an is an the'))

You might also like