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.
1# Take n as input2n = int(input("Enter number of elements: "))34# Input array5arr = []6print("Enter elements:")7for i in range(n):8 element = int(input())9 arr.append(element)1011# Print array12print("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
Read the element count and create arr.
- 2
Convert and append each of n responses.
- 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.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum