0% found this document useful (0 votes)
19 views8 pages

Igw2d 5fhfr

Lists of Ai

Uploaded by

muktidnath
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)
19 views8 pages

Igw2d 5fhfr

Lists of Ai

Uploaded by

muktidnath
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
You are on page 1/ 8

LIST

Lists LAB QUESTION

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

print('Alternate elements are:')

for i in range(0,size,2):
print(mylist[i])

Output

How many elements you want to enter? 6


Enter 6 elements
10
20
30
40
50
60
Alternate elements are:
10
30
50

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 = []

size = int(input('How many elements you want to enter? '))

print('Enter',str(size),'elements')

1|Page
for i in range(size):

data = int(input())

mylist.append(data)

#reverse the list

for i in range(size//2):

#swapping elements

mylist[i],mylist[len(mylist)-1-i] = mylist[len(mylist)-1-i], mylist[i]

print('Reverse list:', mylist)

Output

How many elements you want to enter? 6

Enter 6 elements

10

20

30

40

50

60

Reverse list: [60, 50, 40, 30, 20, 10]

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 = []

size = int(input('How many elements you want to enter? '))

print('Enter',str(size),'positive numbers')

for i in range(size):

data = int(input())

mylist.append(data)

max = 0

for data in mylist:

if data > max:

max = data

print('The largest number in list is', max)


2|Page
Output

How many elements you want to enter? 5

Enter 5 positive numbers

34

45

19

23

The largest number in list is 45

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 = []

size = int(input('How many elements you want to enter? '))

print('Enter',str(size),'elements')

for i in range(size):

data = input()

mylist.append(data)

print('list before shifting', mylist)

temp = mylist[size-1]

for i in range(size-1,0,-1):

mylist[i] = mylist[i-1]

mylist[0] = temp

print('list after shifting', mylist)

Output

How many elements you want to enter? 4

Enter 4 elements

sunday

monday

tuesday

wednesday

3|Page
list before shifting ['sunday', 'monday', 'tuesday', 'wednesday']

list after shifting ['wednesday', 'sunday', 'monday', 'tuesday']

5. Write a program that input a string and ask user to delete a given word from a string.

ANS

# Write a program that input a string and ask

# user to delete a given word from a string.

text = input('Enter a string: ')

words = text.split()

data = input('Enter a word to delete: ')

status = False

for word in words:

if word == data:

words.remove(word)

status = True

if status:

text = ' '.join(words)

print('String after deletion:',text)

else:

print('Word not present in string.')

Output

Enter a string: Aman is a good boy.

Enter a word to delete: good

String after deletion: Aman is a boy.

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

mydate = input('Enter a date(mm/dd/yyyy): ')

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'

elif month == 12:

month = 'December'

newdate = month + ' ' + str(day) + ',' + str(year)

print(newdate)

Output

Enter a date(mm/dd/yyyy): 06/22/2020

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

n = int(input("Enter the number of rows:"))

m = int(input("Enter the number of columns:"))

matrix = []

print("Enter values in matrix :")

# For user input

for i in range(n):

data =[]

for j in range(m):

data.append(int(input()))

matrix.append(data)

# For printing the matrix

for i in range(n):

for j in range(m):

print(matrix[i][j], end = " ")

print()

# For printing row wise sum

for i in range(n):

6|Page
sum = 0

for j in range(m):

sum = sum + matrix[i][j]

print('Sum of row',i+1,':',sum)

Output

Enter the number of rows:3

Enter the number of columns:4

Enter values in matrix :

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

10. Write a program to multiply two matrices

ANS

Find the output of the following program.

List = ["P",20,"R",10,"S",30]

Times = 0

Alpha = ""

Sum = 0

for I in range(1,6,2):

Times = Times + I

Alpha = Alpha + List[I-1]+"#"

Sum = Sum + List[I]

print(Times,Sum,Alpha)

ANS

9 60 P#R#S#

8|Page

You might also like