In this article, we will explore how to take input from the user to create a 2D array in Python. We will cover different approaches to gather user input for 2D arrays, along with code examples and explanations.
One way to take input from the user for a 2D array is to treat it as a list of lists. Each inner list represents a row, and the elements within that list represent the column values.
Here's an example code snippet:
# Taking input as a list of lists
rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))
# Initialize an empty 2D array
my_array = []
# Take input for each element
for i in range(rows):
row = []
for j in range(columns):
element = int(input(f"Enter the element for row {i+1}, column {j+1}: "))
row.append(element)
my_array.append(row)
# Display the 2D array
for row in my_array:
print(row)
Another approach is to take input from the user as a matrix. The user can enter the elements rowwise, separated by spaces or commas.
Here's an example code snippet:
# Taking input as a matrix
rows, columns = map(int, input("Enter the number of rows and columns: ").split())
# Initialize an empty 2D array
my_array = []
# Take input for the matrix
for i in range(rows):
row = list(map(int, input(f"Enter the elements for row {i+1}: ").split()))
my_array.append(row)
# Display the 2D array
for row in my_array:
print(row)
In this code, we first ask the user to enter the number of rows and columns for the 2D array. Using 'split()' and 'map()', we separate the input values and convert them to integers.
Next, we initialize an empty list 'my_array' to store the elements. We then use a loop to take input for each row. The user is prompted to enter the elements for each row, which are split and converted to integers using 'split()' and 'map()'. The completed row is appended to 'my_array'.
Finally, we display the 2D array by iterating over each row and printing it.
Let's now solve a couple of sample problems that involve taking input from the user for a 2D array:
Problem 1: Finding the Maximum Element
Given a 2D array, write a program to find the maximum element present in it.
# Problem 1: Finding the Maximum Element
rows, columns = map(int, input("Enter the number of rows and columns: ").split())
# Take input for the matrix
my_array = []
for i in range(rows):
row = list(map(int, input(f"Enter the elements for row {i+1}: ").split()))
my_array.append(row)
# Find the maximum element
maximum = my_array[0][0]
for row in my_array:
for element in row:
if element > maximum:
maximum = element
print("Maximum element:", maximum)
Problem 2: Summing the Diagonal Elements
Given a square 2D array, write a program to find the sum of its diagonal elements.
# Problem 2: Summing the Diagonal Elements
size = int(input("Enter the size of the square matrix: "))
# Take input for the matrix
my_array = []
for i in range(size):
row = list(map(int, input(f"Enter the elements for row {i+1}: ").split()))
my_array.append(row)
# Find the sum of diagonal elements
diagonal_sum = 0
for i in range(size):
diagonal_sum += my_array[i][i]
print("Sum of diagonal elements:", diagonal_sum)
You've learned how to take input from the user to create a 2D array in Python. We explored two different approaches: taking input as a list of lists and taking input as a matrix. Additionally, we solved a couple of sample problems to practice the concepts. Use this knowledge to interactively create 2D arrays and solve various programming problems!
49 videos38 docs18 tests

49 videos38 docs18 tests


Explore Courses for Software Development exam
