English

Write a program to input n numbers and to insert any number in a particular position.

Advertisements
Advertisements

Question

Write a program to input n numbers and to insert any number in a particular position.

Code Writing
Advertisements

Solution

Store n numbers in a list and use insert(position, number) to insert a number at the specified position.

n = int(input("Enter number of elements: "))
numbers = []

for i in range(n):
    number = int(input("Enter a number: "))
    numbers.append(number)

position = int(input("Enter position: "))
number = int(input("Enter number to insert: "))
numbers.insert(position, number)

print("List after insertion:", numbers)
 
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Review of Phython - EXERCISE [Page 27]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 27. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×