w3resource

Python: Create an array of 5 integers and display the array items


1. Create an Array and Access Elements

Write a Python program to create an array of 5 integers and display the array items. Access individual elements through indexes.

Pictorial Presentation:

Python Exercises: Create an array of 5 integers and display the array items


Sample Solution:

Python Code :

from array import *
array_num = array('i', [1,3,5,7,9])
for i in array_num:
    print(i)
print("Access first three items individually")
print(array_num[0])
print(array_num[1])
print(array_num[2])

Sample Output:

1                                                                      
3                                                                      
5                                                                      
7                                                                      
9                                                                      
Access first three items individually                                  
1                                                                      
3                                                                      
5

For more Practice: Solve these Related Problems:

  • Write a Python program to create an array of 5 integers using the array module and then print each element by accessing its index.
  • Write a Python program to initialize an array with 5 random integers and display each element on a new line using negative indexing.
  • Write a Python program to create an array from a list of numbers and then use a loop to print the first three elements individually.
  • Write a Python program to create an array of integers and use slicing to print the first two and last two elements.

Go to:


Previous: Python Array Exercise Home.
Next: Write a Python program to append a new item to the end of the array.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Based on 748 votes, average difficulty level of this exercise is Medium .

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.