Question

In Python Write a program to fill a square list with 5 rows and 5 columns,...

In Python

Write a program to fill a square list with 5 rows and 5 columns, placing the integer 1 along the diagonal.

Print the list.

Here is what the output should look like:

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Explanation:

First te square list l is created using nested loops, whenever i equals j, 1 is appended , otherwise 0 is appended to the row. temp list denotes each row which is finally appended to the l everytime after inner loop exits.

Then, another nested loop is used to print the square list as mentioned below.

Please provide comments if you have any doubts!

Code:

1 = 11 for i in range(5): temp = [] for j in range(5): if(i==j): temp.append(1) else: temp.append() 1.append(temp) for i in r

l = []

for i in range(5):
temp =[]
for j in range(5):
if(i==j):
temp.append(1)
else:
temp.append(0)
l.append(temp)

for i in range(5):
for j in range(5):
print(l[i][j], end = ' ')
print()


  

Output:

1OOOO 01000 OO 100 OOO 10 OOOO 1 . . .Program finished with exit code 0 Press ENTER to exit console.

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
In Python Write a program to fill a square list with 5 rows and 5 columns,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in python: Write a program that uses a two-dimensional list that has 4 rows, with 5...

    in python: Write a program that uses a two-dimensional list that has 4 rows, with 5 columns in each row. The program should do the following things 1. Create or fill in the list so that it has random numbers between 0 and 50. 2. Print the entire list using a loop.

  • python - Write a program that converts square feet to square yards. Prompt the user to...

    python - Write a program that converts square feet to square yards. Prompt the user to enter a value in square feet and then print the converted output along with a message similar to "1 sq. ft. is .11111 sq. yards."

  • 9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...

    9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown in the following figure · 15 4 9 2-15 3 5 7-15 81 615 15 15 15 15 Write a program that simulates a magic square using...

  • using python3 Write a Python program that will ask the user for: 1. The length of...

    using python3 Write a Python program that will ask the user for: 1. The length of a rectangle. 2. The width of a rectangle and will print out the length of the diagonal of the rectangle. So your program should look like this: Please enter the length of the rectangle: Please enter the width of the rectangle: The diagonal of the rectangle is: So the user enters the length, width and then the answer is printed.

  • PYTHON CODE In a program, write a function that accepts two arguments: a list, and a...

    PYTHON CODE In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. Output should look like: Number: 5 List of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List of numbers that are larger than 5: [6, 7, 8, 9, 10]

  • Please solve it by Python 3 Write a program that asks the user for a positive...

    Please solve it by Python 3 Write a program that asks the user for a positive integer limit and prints a table to visualize all factors of each integer ranging from 1 to limit. A factor i of a number n is an integer which divides n evenly (i.e. without remainder). For example, 4 and 5 are factors of 20, but 6 is not. Each row represents an integer between 1 and 20. The first row represents the number 1,...

  • Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the...

    Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Function Description Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums. diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr) Parameters: a_size_rows: number of rows in array a_size_cols: number of columns in array a: array of integers to process Returns: integer value that was calculated Constraints -100 < = elements of the matrix < = 100...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a Python...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a Python statement that creates a two-dimensional list named values with 3 rows and 2 columns. Then write nested loops that get an integer value from the user for each element in the list. Then display the content of the list. Please see the outcome below: Outcome run: Enter the 1 element in the 1 row :1 Enter the 2 element in the 1 row :2...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a Python...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a Python statement that creates a two-dimensional list named values with 3 rows and 2 columns. Then write nested loops that get an integer value from the user for each element in the list. Then display the content of the list. Please see the outcome below: Outcome run: Enter the 1 element in the 1 row :1 Enter the 2 element in the 1 row :2...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT