Computer Science - Informatics Practices Class XI/XII Python List Solved Test Part 6

 

Computer Science - Informatics Practices Class XI/XII Python List Solved Test  Part 6

Q1. Write a program to create list of the

    following (take input from the user)

a.     Any five students name

b.     Any five numbers

c.     Any five alphabets

d.     Any five name of colors.

Q2. Write a program to accept 10 numbers from the user, if the number is odd, and then add that number to the list.

(input numbers are : 1,2,3,4,5,6,7,8,9,10

L = [1, 3, 5, 7, 9])

Q3. Write a program to find the largest number from the following list.(without using inbuilt function)

A = [23, 12, 45, 67, 55]

Q4. Write a program to find the second largest number from the following list.

A = [23, 12, 45, 67, 55]

Q5. Explain the extend() function with example.

Q6. Write any one difference between insert() and append() function.


SOLUTIONS

Q1. Write a program to create list of the

    following (take input from the user)

a.     Any five students name

b.     Any five numbers

c.     Any five alphabets

d.     Any five name of colors.


Ans a)

a = []
for i in range(5):
nm=input("Enter student name")
a.append(nm)
print(a)

b)
a = []
for i in range(5):
num=int(input("Enter any number"))
a.append(num)
print(a)

c)
a = []
for i in range(5):
al=input("Enter any alphabet")
a.append(al)
print(a)

d)

a = []
for i in range(5):
cl=input("Enter name of any color")
a.append(cl)
print(a)

Q2. Write a program to accept 10 numbers from the user, if the number is odd, and then add that number to the list.

(input numbers are : 1,2,3,4,5,6,7,8,9,10

L = [1, 3, 5, 7, 9])

Ans.

a = []
for i in range(10):
num=int(input("Enter any number"))
if(num%2!=0):
a.append(num)
print(a)

Q3. Write a program to find the largest number from the following list.(without using inbuilt function)

A = [23, 12, 45, 67, 55]

Ans

A = [23, 12, 45, 67, 55]
max=A[0]
for i in range(len(A)):
if A[i]>max:
max=A[i]
print(max)

Q4. Write a program to find the second largest number from the following list.

A = [23, 12, 45, 67, 55]

Ans.

A = [23, 12, 45, 67, 55]
A.sort()
print(A[-2])

Q5. Explain the extend() function with example.

Ans. extend() function add complete list or specified elements of list at the end of another list. for example

A = [23, 12, 45, 67, 55]
B = ["Amit",2,3]
A.extend(B)
print(A)

OUTPUT : [23, 12, 45, 67, 55, 'Amit', 2, 3]

Q6. Write any one difference between insert() and append() function.

Ans. insert() function add elements at the specified index of list while append() function add the elements at the end of the list.

No comments:

Post a Comment

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet