Python Is This Python Multiplication Table Code Correct?

AI Thread Summary
A user seeks to create a Python function that generates a multiplication table for numbers 0 to 20 using nested lists. The initial implementation appends all products to a single list, resulting in a flat structure. Another participant confirms the function works but suggests modifying it to display results in separate rows for each multiplier. The discussion highlights that the placement of the list `Prod` should be inside the inner loop to ensure each row corresponds to the multiples of the respective number, thereby achieving the desired format.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! 😊

I want to write a function in Python that returns the multiplication table $20\times 20$.

We do that using lists in lists, right? I have written the following :

Code:
def mul_table() : 
    Prod = []  
    Table = [] 
    for i in range(21) : 
        for j in range(21) : 
            Prod.append(i*j) 
        Table.append(Prod)
    return Table 
    
print(mul_table())
Is that correct? :unsure:
 
Technology news on Phys.org
Hey mathmari!

It works for me. (Nod)
 
Klaas van Aarsen said:
It works for me. (Nod)

Could we make a change so that the result gives in different rows? I means all multiplers of 0 in the first row, in the second row all multiples of 1,etc? :unsure:
 
mathmari said:
Could we make a change so that the result gives in different rows? I means all multiplers of 0 in the first row, in the second row all multiples of 1,etc?
Ah, I see now that Prod = [] is in the wrong spot.
It should be inside the for-loop. (Worried)

Then the first row will contain only all multiples of 0. 🤔
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top