Taking integer input in an array using Python is a common task when dealing with numerical datasets. This tutorial will walk you through the process of taking integer input, storing it in an array, and working with the data using Python code.
In this tutorial, we’ll be using lists as arrays. Lists are the most versatile of Python’s built-in data structures and they can store different types of data, including integers.
To begin, let’s take a look at the required steps to take integer input in an array using Python:
Step 1: Initialize an empty list
First, initialize an empty list which will act as the array to store the input integers. To create an empty list, use the square brackets []
.
1 |
integer_array = [] |
Step 2: Ask the user for the number of integers to be added to the array
Before taking integer input, it’s necessary to know how many integers the user wants to input. This can be done using input()
function.
1 |
num_of_integers = int(input("Enter the number of integers to be added to the array: ")) |
Here, the input()
function takes a string as a prompt and reads data from the user. Then, int()
function is used to convert the user-inputted data to an integer.
Step 3: Loop through and store the user input integers in the list
Now that we have an empty list and the number of integers to be added, it’s time to loop through and collect the user-inputted integers.
Using a for
loop, we can iterate over a defined range based on the number of integers the user wants to add. Inside the loop, use input()
function again to collect the integer inputs and append them to the list.
1 2 3 |
for i in range(num_of_integers): user_input = int(input("Enter integer {0}: ".format(i + 1))) integer_array.append(user_input) |
At this point, you have taken integer input from the user and stored it in a list called integer_array
.
Here’s the full code:
1 2 3 4 5 6 7 |
integer_array = [] num_of_integers = int(input("Enter the number of integers to be added to the array: ")) for i in range(num_of_integers): user_input = int(input("Enter integer {0}: ".format(i + 1))) integer_array.append(user_input) |
Example of output:
Enter the number of integers to be added to the array: 5 Enter integer 1: 7 Enter integer 2: 4 Enter integer 3: 10 Enter integer 4: 12 Enter integer 5: 56
At this point, the list integer_array
will contain the integer inputs [7, 4, 10, 12, 56]
.
You can manipulate the integer array using various list methods and functions available in Python. Some examples of manipulation include:
- Accessing elements using index, e.g.,
integer_array[0]
- Calculating the length of the list using the
len()
function, e.g.,len(integer_array)
- Sorting the list using the
sorted()
function, e.g.,sorted(integer_array)
- Slicing the list to obtain a sub-list, e.g.,
integer_array[1:4]
Conclusion
In this tutorial, you learned how to take integer input and store it in a list (acting as an array) using Python. This simple process can be extended and modified to suit various use-cases, including taking user input for multi-dimensional arrays or processing numerical datasets.
Remember to leverage Python’s built-in list methods and functions when working with integer arrays for efficient output generation and manipulation.