Igw2d 5fhfr
Igw2d 5fhfr
[Set – 1]
1. Write a program that accepts a list from user and print the alternate element of list.
ANS
mylist = []
size = int(input('How many elements you want to enter? '))
print('Enter',str(size),'elements')
for i in range(size):
data = int(input())
mylist.append(data)
for i in range(0,size,2):
print(mylist[i])
Output
2. Write a program that accepts a list from user. Your program should reverse the content of list and
display it. Do not use reverse() method.
ANS
mylist = []
print('Enter',str(size),'elements')
1|Page
for i in range(size):
data = int(input())
mylist.append(data)
for i in range(size//2):
#swapping elements
Output
Enter 6 elements
10
20
30
40
50
60
3. Find and display the largest number of a list without using built-in function max(). Your program
should ask the user to input values in list from keyboard.
ANS
mylist = []
print('Enter',str(size),'positive numbers')
for i in range(size):
data = int(input())
mylist.append(data)
max = 0
max = data
34
45
19
23
4. Write a program that rotates the element of a list so that the element at the first index moves to
the second index, the element in the second index moves to the third index, etc., and the element in
the last index moves to the first index.
ANS
mylist = []
print('Enter',str(size),'elements')
for i in range(size):
data = input()
mylist.append(data)
temp = mylist[size-1]
for i in range(size-1,0,-1):
mylist[i] = mylist[i-1]
mylist[0] = temp
Output
Enter 4 elements
sunday
monday
tuesday
wednesday
3|Page
list before shifting ['sunday', 'monday', 'tuesday', 'wednesday']
5. Write a program that input a string and ask user to delete a given word from a string.
ANS
words = text.split()
status = False
if word == data:
words.remove(word)
status = True
if status:
else:
Output
6. Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It
should print the date in the form March 12, 2021.
ANS
datelist = mydate.split('/')
month = int(datelist[0])
day = int(datelist[1])
year = int(datelist[2])
4|Page
if month == 1:
month = 'January'
elif month == 2:
month = 'February'
elif month == 3:
month = 'March'
elif month == 4:
month = 'April'
elif month == 5:
month = 'May'
elif month == 6:
month = 'June'
elif month == 7:
month = 'July'
elif month == 8:
month = 'August'
elif month == 9:
month = 'September'
elif month == 2:
month = 'October'
elif month == 2:
month = 'November'
month = 'December'
print(newdate)
Output
June 22,2020
5|Page
7. Write a program with a function that accepts a string from keyboard and create a new string after
converting character of each word capitalized. For instance, if the sentence is "stop and smell the
roses." the output should be "Stop And Smell The Roses"
ANS
8. Find the sum of each row of matrix of size m x n. For example for the following matrix output will
be like this :
ANS
matrix = []
for i in range(n):
data =[]
for j in range(m):
data.append(int(input()))
matrix.append(data)
for i in range(n):
for j in range(m):
print()
for i in range(n):
6|Page
sum = 0
for j in range(m):
print('Sum of row',i+1,':',sum)
Output
11
12
15
10
42
2 11 7 12
5 2 9 15
8 3 10 42
Sum of row 1 : 32
Sum of row 2 : 31
Sum of row 3 : 63
Sum of row 1 = 32
Sum of row 2 = 31
Sum of row 3 = 63
7|Page
9. Write a program to add two matrices of size n x m.
ANS
ANS
List = ["P",20,"R",10,"S",30]
Times = 0
Alpha = ""
Sum = 0
for I in range(1,6,2):
Times = Times + I
print(Times,Sum,Alpha)
ANS
9 60 P#R#S#
8|Page