0% found this document useful (0 votes)
5 views16 pages

Python Programming Questions

The document contains a series of Python programming questions focused on string, list, and tuple manipulation. Each question is accompanied by a solution that demonstrates the implementation of various programming concepts such as loops, conditionals, and string methods. The document serves as a practical guide for learning and practicing Python programming skills.

Uploaded by

rockmamaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Python Programming Questions

The document contains a series of Python programming questions focused on string, list, and tuple manipulation. Each question is accompanied by a solution that demonstrates the implementation of various programming concepts such as loops, conditionals, and string methods. The document serves as a practical guide for learning and practicing Python programming skills.

Uploaded by

rockmamaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMMING QUESTIONS

STRING MANIPULATION

1. WAP to display the pattern:-

R
RI
RIV
RIVE
RIVER

Solution:-

s="RIVER"
k=""
for i in s:
k+=i
print(k)

2. WAP to input a word and check it is palindrome or not.

Solution:-

n=input("Enter a word:- ")


org=n
if org==n[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

3. WAP to input a sentence and print reverse of every word.

Solution:-
n=input("Enter a sentence:- ")
d=[Link]()
str=" "
for i in d:
str=str+i[::-1]+" "
print(str)

4. WAP to input a string and convert every alternative string into uppercase.

Solution:-

n=input("Enter a string:- ")


s=" "
for i in range(0,len(n)):
if i%2==0:
s=s+n[i].upper()
else:
s=s+n[i]
print(s)

Notes Prepared by Abhishek Sir 1


5. WAP to input a sentence and display the longest and the smallest word.

Solution:-

n=input("Enter a sentence:- ")


max=0
b=" "
c=" "
d=[Link]()
min=len(d[0])
for i in d:
if len(i)>max:
b=i
max=len(i)
if len(i)<min:
c=i
min=len(i)
print("Longest word:- ",b)
print("Smallest word:-",c)

6. WAP to Input a sentence and print number of words.

Solution:-

c=0
n=input("Enter a sentence:- ")
d=[Link]()
for i in d:
c=c+1
print("Total number of words", c)

7. WAP to input a sentence and display the longest word in it.

Solution:-

n=input("Enter a sentence:- ")


max=0
b=" "
d=[Link]()
for i in d:
if len(i)>max:
b=i
max=len(i)
print("Longest word:- ",b)

8. WAP to input a sentence and display those words having length more than 4 and first and last character are
same.

Solution:-
n=input("Enter a string:-")
d=[Link]()
for i in d:
if len(i)>4 and i[0]==i[-1]:
print(i)

Notes Prepared by Abhishek Sir 2


9. WAP to input a string and replace all the spaces with @ sign.

Solution:-

n=input("Enter a string:-")
s1=""
for i in n:
if [Link]():
s1=s1+"@"
else:
s1=s1+i
print(s1)

10. WAP to input a string and create a new string from the first letter of every word.

Solution:-

n=input("Enter a string:- ")


s=""
d=[Link]()
for i in d:
print(i[0])
s=s+i[0]
print(s)

11. WAP to input a string and count total no of uppercase, lowercase and digits in it.

Solution:-

n=input("Enter a string:-")
u=l=d=0
for i in n:
if [Link]():
u+=1
if [Link]():
l+=1
if [Link]():
d+=1
print("upper case:- ",u)
print("lower case:- ",l)
print("digit:- ",d)

12. WAP to input a sentence and count total no of “is” and “are” in it.

Solution:-
n=input("Enter a string:-")
c=c1=0
d=[Link]()
for i in d:
if i =="is":
c+=1
if i=="are":
c1=c1+1
print("Total no of is:-",c)
print("Total no of are :-",c1)

Notes Prepared by Abhishek Sir 3


PRACTICE QUESTIONS

1. Write a Python program to calculate the length of a string.


2. Write a Python program to get a string from a given string where all occurrences of its first
char have been changed to '$', except the first char itself.
Sample String : 'restart'
Expected Result : 'resta$t'
3. Write a program that inputs a line of text and prints out the count of vowels in it.
4. WAP to input a string that contains number in it, display the sum of that number.
5. WAP to input full name and display it in the form –
Eg. Rajesh Kumar Shukla
O/P- [Link]
6. WAP to input a string in lowercase. Replace the letter “e” with “*” in the given string. Display
the new string.
7. WAP to input a string and change the case of each word.
8. Write a program to input a string and display the ASCII value of each character.
9. Display the patterns:-
ABCDE
ABCD
ABC
AB
A
10. WAP to accept a string and display a new string after removing the vowels present in it.

Notes Prepared by Abhishek Sir 4


LIST MANIPULATION

1. WAP to input 5 names in a list and display only those names whose length is more than 5.

Solution

l=[]
for i in range(5):
n=input("Enter Names:- ")
[Link](n)
print("Names more than 5 characters:- ")
for i in l:
if len(i)>5:
print(i)

2. WAP to input 5 names in an list display all the palindromic names.

Solution

l=[ ]
for i in range(5):
n=input("Enter Names:- ")
[Link](n)
print("Palindromic Names are:- ")
for i in l:
if i==i[::-1]:
print(i)

3. WAP to input two list and remove the duplicate values and store in another list.

Solution
l=[10,10,20,30]
l1=[40,40,50]
l3=l+l1
res=[]
for i in l3:
if i not in res:
[Link](i)
print(res)

4. WAP to input 10 numbers in a list and swap first five numbers with last five and vice-versa.

Solution

l=[]
for i in range(10):
n=int(input("Enter:- "))
[Link](n)
print("Original list")
print(l)
m=len(l)//2
for i in range(len(l)//2):
l[i],l[m]=l[m],l[i]
m=m+1
print(l)

Notes Prepared by Abhishek Sir 5


5. WAP to input a list and count the frequency of each value in it.

Solution

l=[10,10,20,20,30,40,50]
r={}
for i in l:
if i in r:
r[i]+=1
else:
r[i]=1
print(r)

6. WAP to input 5 numbers in list1 and 5 numbers in list2 and append all the values in list1 and display it

Solution

1st1=[]
print("enter 5 number in list:-")
for i in range(5):
nu=int(input("enter a number:-"))
[Link](nu)
1st2=[]
print("enter 5 number in list:-")
for i in range(5):
nu=int(input("enter a number:-"))
[Link](nu)
[Link](1st2)|
print(1st1)

7. WAP to input 10 numbers in a list and also input a number search it is present in the list or not using
binary search.

Solution l=[]
print("Enter 10 number's:- ")
for i in range(10):
n=int(input())
[Link](n)
print("Enter a Number to Search:")
search = int(input())
first = 0
last = 9
middle = (first+last)/2
middle = int(middle)
while first <= last:
if l[middle]<search:
first = middle+1
elif l[middle]==search:
print("The Number Found at Position:")
print(middle+1)
break
else:
last = middle-1
middle = (first+last)/2
middle = int(middle)
if first>last:
print("The Number is not Found in the List")
Notes Prepared by Abhishek Sir 6
8. WAP to input 5 numbers in a list and display values that end with digit 3.

Solution
lst=[]
print("Enter 5 numbers in a list:- ")
for i in range(5):
nu=int(input("Enter number:- "))
[Link](nu)
print("Values end with digit 3")
for i in lst:
if i%10==3:
print(i)

9. WAP to input 5 numbers in a list and display sum whose last digit is ends with 2.

Solution

lst=[];s=0
print("Enter 5 numbers in a list:- ")
for i in range(5):
nu=int(input("Enter number:- "))
[Link](nu)
print("Values end with digit 3")
for i in lst:
if i%10==2:
s=s+i
print(“Sum is:- ”,s)

10. WAP to input 10 numbers in a list and display all the prime numbers present in it.

Solution

l=[]
for i in range(5):
n=int(input("Enter values:- "))
[Link](n)
for i in l:
c=0
for k in range(2,i):
if i%2==0:
c+=1
if c==0:
print("prime no:-",i)

Notes Prepared by Abhishek Sir 7


PRACTICE QUESTIONS

1. WAP to input 10 numbers in a list and display its sum and product.
2. WAP to input 10 numbers in a list and display the maximum and minimum value from it.
3. WAP to input two list and display the common values from it.
4. WAP to input 5 names in a list and display only those names which start and end with same character
and length is more than 4.
5. WAP to input 10 alphabets in a list count the total no of Vowels and Consonant in it.
6. WAP to input 5 words in a list. Convert them into uppercase letters. Display only those words that
begin and end with a vowel.
7. WAP to accept 5 words in a list. Display those words that end with the letter “s”.
8. WAP to input 10 numbers in a list. Add 1 with all the even indices and subtract 1 from all the odd
indices.
9. WAP to input 10 numbers in a list. Display all the composite numbers from the given list.
10. WAP to input 10 numbers in a list containing positive and negative numbers. Separate it by using two
list.

Notes Prepared by Abhishek Sir 8


TUPLE MANIPULATION

1. WAP to input name, class, rollno. of 5 students in a tuple and display it.

Solution
t=()
for i in range(2):
n=input("Enter name:- ")
r=int(input("Enter rollno- "))
c=int(input("Enter Class:- "))
s=(n,c,r)
t=t+(s,)
print(t)

2. WAP to input 5 numbers in a tuple and display the second largest number from it.

Solution
t=()
print("Enter any 5 numbers:- ")
for i in range(5):
n=int(input())
t=t+(n,)
d=sorted(t)
print("Second Largest Number:- ",d[-2])

3. WAP to input a number from the user and store the first 10 multiples of the number in a tuple.

Solution
t=()
print("Enter a number:- ")
n=int(input())
for i in range(1,11):
t=t+(n*i,)
print(t)

4. WAP to input a string and convert it into a tuple.

Solution
n=input("Enter a string:- ")
print(tuple(n))

5. WAP to display the sum of marks of the students in a tuple.


T= (("raj",50),("sweta",56),("aritro",88))

Solution
T=(("raj",50),("sweta",56),("aritro",88))
s=0
for i in T:
s=s+i[1]
print("Sum of marks:- ",s)

Notes Prepared by Abhishek Sir 9


6. WAP to input 5 names in a tuple and create a new tuple after swap casing each names into a new tuple.

Solution
t=("raja","Amit","Mohan","Sumit","DoDO")
t1=()
l=list(t)
l2=[]
for i in l:
[Link]([Link]())
t1=tuple(l2)
print(t1)

7. WAP to print the square of all the odd values from the given tuples.
T=(3,55,6,8,18,67)

Solution

T=(3,55,6,8,18,67)
for i in T:
if i%2==1:
print(i,"\t",i*i)

8. WAP to input 5 numbers in two different tuples and show those values which are common in them.

Solution

T=(1,2,3,4,5)
T1=(3,4,6,7)
print("Common Values are:- ")
for i in T:
if i in T1:
print(i)

9. WAP to create a new tuple after removing the duplicate values from both the tuples.
T= (1,2,3,4)
T2= (3,4,5)
T3= (1,2,3,4,5)

Solution

t=()
t1=(1,2,3,4)
t2=(3,4,5)
t3=t1+t2
for i in t3:
if i not in t:
t=t+(i,)
print("After removing duplicate values:-", t)

Notes Prepared by Abhishek Sir 10


10. WAP to input 10 numbers in an tuple and count the frequency of each number and display in the form
of a dictionary.

Solution

t=()
print("Enter 10 numbers in a tuple- ")
d={}
for i in range(10):
n=int(input("Enter number:- "))
t=t+(n,)
for i in t:
c=0
for j in t:
if i==j:
c=c+1
d[i]=c
print(d)

Notes Prepared by Abhishek Sir 11


PRACTICE QUESTIONS

1. WAP to input 10 numbers in a tuple, display the sum of even and odd indices.
[Link] input a number from the user and delete the value from the given tuple. t=(12,13,14,22,25)
[Link] to input 5 numbers in a tuple. Count and display all the Niven Numbers in it.
( Eg. 126=1+2+6=9 and 126 is divisible by 9)
[Link] to input some alphabets and numbers in a tuple. Display the sum of the numbers.
[Link] to input some values in two different tuples of same size. Interchange the tuple values and
display the result.
[Link] to input 10 numbers in a tuple and display the Armstrong number available in the tuple.
Eg.:- 153=) 13+53+33=153
[Link] to input 10 numbers in a tuple. Also input a number and check it is present in the tuple or not.
[Link] a program that inputs two tuples and creates a third, that contains all elements of the first
followed by all elements of the second.
[Link] a tuple pairs = ((2, 5), (4, 2), (9, 8), (12, 10)), count the number of pairs (a, b) such that both
a and b are even.
10. Write a program to create a nested tuple to store roll number, name and marks of students.

Notes Prepared by Abhishek Sir 12


DICTIONARY MANIPULATION

1. WAP to input name and marks of 5 students and store it in a dictionary.

Solution

d={}
print("Enter names and marks of 5 students ")
for i in range(5):
nm=input("Enter name:- ")
ma=int(input("Enter marks:- "))
d[nm]=ma
print(d)

2. WAP to input name and marks of 5 students and calculate sum of their marks and check whether the given
marks is perfect number or not.

Solution
d={}
print("Enter names and marks of 5 students ")
for i in range(5):
nm=input("Enter name:- ")
ma=int(input("Enter marks:- "))
d[nm]=ma
s=0
for i in d:
s=s+d[i]
org=s
su=0
for i in range(1,s):
if s%i==0:
su=su+i
if org==su:
print("Perfect no",org)
else:
print("Not perfect no",org)

3. WAP to input ID as a key and name, class and rollno. as a value and display it.

Solution

d={}
print("Enter ID,name, class and rollno of 5 students ")
for i in range(2):
i_d=int(input("Enter Id:- "))
nm=input("Enter name:- ")
cl=int(input("Enter class:- "))
rn=int(input("Enter rollno:- "))
d[nm]=[i_d,nm,cl,rn]
print(d)

Notes Prepared by Abhishek Sir 13


4. WAP to input ID as a key and designation, batch and salary as a value and display the sum of the salary whose
designation is manager.

Solution
d={}
print("Enter ID,designation,batch and salary")
for i in range(3):
i_d=int(input("Enter Id:- "))
de=input("Enter designation:- ")
ba=int(input("Enter batch:- "))
sa=int(input("Enter salary:- "))
d[i_d]=[de,ba,sa]
s=0
for i in d:
if d[i][0] in "manager":
s=s+d[i][2]
print(s)

5. WAP to input 10 numbers in an tuple and count the frequency of each number and display in the form of a
dictionary.

Solution

t=()
print("Enter 10 numbers in a tuple- ")
d={}
for i in range(10):
n=int(input("Enter number:- "))
t=t+(n,)
for i in t:
c=0
for j in t:
if i==j:
c=c+1
d[i]=c
print(d)

6. WAP to input name and marks of 5 students in a dictionary and display the names of only those
students whose marks is more than 75.

Solution

d={}
print("Enter name and marks for 5 students:- ")
for i in range(5):
nm=input("Enter name:- ")
n=int(input("Enter marks:- "))
d[nm]=n
for i in d:
if d[i]>75:
print(i)

Notes Prepared by Abhishek Sir 14


7. WAP to create two different list one with key and another with the value in the form of a dictionary.
d={‘a’:1, ‘b’:2, ‘c’:3}

Solution

keys=["a","b","c"]
values=[1,2,3]
d=dict(zip(keys,values))
print(d)

8. WAP to input 5 numbers in a list and also create another by multiplying each value with 5 and create a new
dictionary by using the two list.

Solution

l=[10,20,30,40,50]
l1=[]
for i in l:
[Link](i*5)
d=dict(zip(l,l1))
print(d)

9. WAP to input 2 dictionaries and concatenate into a new dictionary.

Solution

d={"a":1,"b":2,"c":3}
d1={"d":4,"e":5}
d3={}
for i in d,d1:
[Link](i)
print(d3)

10. WAP to input employeeno, name, designation and salary where employeeno is the key and rest are
the values, enter the employeeno that is to be searched and display its salary.

Solution :-

d={}
for i in range(2):
empno=int(input("Enter Employee number:- "))
n=input("Name:- ")
de=input("designation:- ")
sal=int(input("Salary:- "))
k=[n,de,sal]
d[empno]=k
emno=int(input("Enter employee number to be searched:- "))
for i in d:
if emno==i:
print("Salary is:- ",d[i][2])

Notes Prepared by Abhishek Sir 15


PRACTICE QUESTIONS

1. WAP to display those values whose length of the city name is more than 5 from the given dictionary.
d={1:”Bokaro”,2:”Odisha”,3:”Jaipur”, 4:”Jamshedpur”}
2. WAP to display sum of all the values of given dictionary :-
d={1:2,2:20,3:30,4:40,5:50}
3. WAP to display all the palindrome numbers from the given d={“a”:21, “b”:121,”c”:33}
4. WAP to input Product_id as key and Product_name, price and QTY as value also display it.
5. WAP to accept a key from the user and remove that key from the dictionary if present.
d={1:2,2:90,3:50,4:40}

______________________X_____________________________

Notes Prepared by Abhishek Sir 16

You might also like