Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Input N Integers and Print Them

Read n integers into a list and print them in order. Beginners trace append, a second traversal, and the difference between a list and its displayed values.

BeginnerPhase 4 - Level 1: Basic ArraysExample 1 of 10
input-n-integers-and-print-them.py
1# Take n as input
2n = int(input("Enter number of elements: "))
3
4# Input array
5arr = []
6print("Enter elements:")
7for i in range(n):
8 element = int(input())
9 arr.append(element)
10
11# Print array
12print("Array elements:")
13for element in arr:
14 print(element, end=" ")
15print()

Output

Enter number of elements: 5
Enter elements:
10
20
30
40
50
Array elements:
10 20 30 40 50

In this one

  • Append converted values
  • Traverse stored data
  • Preserve insertion order

How it works

  1. 1

    Read the element count and create arr.

  2. 2

    Convert and append each of n responses.

  3. 3

    Traverse arr to print the stored values.

Edge cases

What if n = 0?

What if n = 0?

No elements are read or displayed.

Does append sort the list?

Does append sort the list?

No. It keeps insertion order.

What's going on

The first loop converts each response with int and appends it to arr. A Python list is used as the exercise's array.

The second loop visits stored values in input order. end keeps them on one line, then a final print supplies the newline. Input and output are separate passes, so the elements remain available after collection.

What if n = 0? No elements are read or displayed.

Does append sort the list? No. It keeps insertion order.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is stored after int(input())?

Pick an answer to check it.

Question 2

What if n = 0?

Pick an answer to check it.

Questions people ask

What if n = 0?

No elements are read or displayed.

Does append sort the list?

No. It keeps insertion order.

What is stored after int(input())?

An integer.