For loops in python Watch
The question is to do with a for loop for a matrix so that the matrix's simension can vary.
Im really really stuck and any advice would make me feel a lot better.
I don't know how to write a matrix in this so I could email you
Question: What's the difference between a list on its own, and a list which exists inside a list?
Answer: Nothing! They work in exactly the same way.
So look at how a simple one-dimensional list works, and then apply that to a list inside a list.
# length could be anything, but 5 is a good number. length = 5 # First, create the list oneDimensional =  # Secondly, populate the list, one element at a time from 0 up to 5 for i in range(length): oneDimensional.append(i);
A list-of-lists is just the same as above, but repeated many times. So if a two-dimensional list is a list repeated many times, then the 'for' loop also needs to be repeated many times. A list-within-a-list involves a loop-within-a-loop.
# The "outer" two-dimensional list can be any width, but 4 is a good number width = 4 # Create the outer list twoDimensional =  # Now create each of the individual lists and put them inside the outer list. # This will create 4 inner-lists for j in range(width): # As before, 5 for an inner-list seems like a good number. length = 5 # As before, create a new list oneDimensional =  # As before, populate the list with data for i in range(length): oneDimensional.append(i * j) # Lastly, add the new inner-list to the outer-list twoDimensional.append(oneDimensional)
Have a read here for some more examples. You'll see some examples using list comprehensions here, which shortens the syntax a lot:
Or here (more examples using the shortened list comprehension format too):