+ 1

What is the problem in this code?

c=(6.0,4,7) c.append (3) c.append(8) print(c) I know that I must put these [ ] instead of these( ) but why? what is the different between them?

25th Apr 2018, 11:31 AM
Omar
Omar - avatar
10 Answers
+ 5
[] make you create a list and like Vivek said lists are mutable, meaning they can be changed, values can be added or deleted from them... () make you create a tuple and again, like Vivek said those are immutables, meaning they cannot be changed, values cannot be added or deleted from them. So that is why you should have used [] if you knew you would be adding values to it
25th Apr 2018, 11:38 AM
cyk
cyk - avatar
+ 5
Yes. () to create or declare tuples and [] to declare lists
25th Apr 2018, 11:46 AM
cyk
cyk - avatar
+ 4
Tuples are immutable; they can't be changed. So you need to convert it to a list, append values and then back to a tuple again.
25th Apr 2018, 11:33 AM
Vivek Sivaramakrishnan
Vivek Sivaramakrishnan - avatar
+ 3
Look at this code. First part with list works and second part with tuple not. https://code.sololearn.com/cmpZISauuRrS/?ref=app
25th Apr 2018, 11:50 AM
cyk
cyk - avatar
+ 3
And look at the answers in this discussion. They answer your question about the benefits of using tuples sometimes https://www.sololearn.com/Discuss/36164/?ref=app
25th Apr 2018, 11:52 AM
cyk
cyk - avatar
+ 2
and tubles is constant so I cannot add any addition numbres to them but lists isnot constant so I can add numres ti them and that is why the code didnot work??
25th Apr 2018, 11:46 AM
Omar
Omar - avatar
+ 2
Yes again
25th Apr 2018, 11:46 AM
cyk
cyk - avatar
+ 1
so we use ( ) in tubles and [ ] in lists?
25th Apr 2018, 11:44 AM
Omar
Omar - avatar
+ 1
Ok cyk thank u😃
25th Apr 2018, 11:53 AM
Omar
Omar - avatar
0
so what is the benifet of these ( ) tuples when I cannot add any numbres to them?
25th Apr 2018, 11:49 AM
Omar
Omar - avatar