Mar-23-2017, 08:01 AM
Discuss how lists containing lists can be used to represent tables in Python. Write a program in Python that asks user to input 5 lines of numbers. Your program should represent these lines as a table.
NOTE: Think carefully about which lists will have a fixed size and which lists will have a variable size.
For example: Enter some numbers: 1 2 3 4 5
Enter some numbers: 5 6 7
Enter some numbers: 1 1 1 1
Enter some numbers: 1.5 6 7.5 2
Enter some numbers: 0
[[1, 2, 3, 4, 5], [5, 6, 7], [1, 1, 1, 1], [1.5, 6, 7.5, 2], [0]]
NOTE: As the input function returns a string, you will be required to split the string.
Once you have some experience with tables as lists of lists, consider the following line of code:
new_table = [[1,2,3]] * 5
Using the techniques you have learnt so far, work out what this code does. As a pair discuss what you think will happen when you change the first element of the first list to 0.
new_table[0][0] = 0
What happened? Did the table behave as expected? If not, what possible explanation could there be for the behaviour of the table? Discuss your findings with your demonstrator.
** Currently stuck with this question, this doesn't carry any assignment marks in my semester so i'm just requesting for help here, currently i'm only able to print numbers which can result exactly like this [[1, 2, 3, 4, 5], [5, 6, 7], [1, 1, 1, 1], [1.5, 6, 7.5, 2], [0]] , but the problem with mine is that mine has the double quotes which means it's a string, but i can't think of a way to convert it to integers .. need help :)
NOTE: Think carefully about which lists will have a fixed size and which lists will have a variable size.
For example: Enter some numbers: 1 2 3 4 5
Enter some numbers: 5 6 7
Enter some numbers: 1 1 1 1
Enter some numbers: 1.5 6 7.5 2
Enter some numbers: 0
[[1, 2, 3, 4, 5], [5, 6, 7], [1, 1, 1, 1], [1.5, 6, 7.5, 2], [0]]
NOTE: As the input function returns a string, you will be required to split the string.
Once you have some experience with tables as lists of lists, consider the following line of code:
new_table = [[1,2,3]] * 5
Using the techniques you have learnt so far, work out what this code does. As a pair discuss what you think will happen when you change the first element of the first list to 0.
new_table[0][0] = 0
What happened? Did the table behave as expected? If not, what possible explanation could there be for the behaviour of the table? Discuss your findings with your demonstrator.
** Currently stuck with this question, this doesn't carry any assignment marks in my semester so i'm just requesting for help here, currently i'm only able to print numbers which can result exactly like this [[1, 2, 3, 4, 5], [5, 6, 7], [1, 1, 1, 1], [1.5, 6, 7.5, 2], [0]] , but the problem with mine is that mine has the double quotes which means it's a string, but i can't think of a way to convert it to integers .. need help :)