pls help. beginner question.

+1 Marco Alexis · August 17, 2015
I'm just starting to watch the tutorials and after every episode I tried each topic, but when I came in lists topic, it gives me error

>>> players[:3] = []
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    players[:3] = []
TypeError: 'tuple' object does not support item assignment

>>> players.append[11]
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    players.append[11]
AttributeError: 'tuple' object has no attribute 'append'

What have I done wrong?

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Marco Alexis · August 18, 2015
Yes I am a Filipino.

Thanks for the answers. It really helped a lot. :)
+1 Halcyon Abraham Ramirez · August 18, 2015
@Marco Alexis Maghuyop 



lists =["are mutable"]

tuples = ("are not mutable")


basically you can ,append,delete,insert,replace items in a list #lists have these [   ] brackets 
but you can't do any of the above things to a tuple and basically a tuple is a just a list that you can't change,add,delete, or modify # tuples have (        )   parenthesis

now you'll be wondering so if a tuple is just a list and it can't be modified and a list can be modified why use a tuple?
somebody correct me if I'm wrong but tuples are faster and uses less memory? you'll only really have to be worrying about this when you're wanting to have fast and efficient code but if you're starting out. you can skip that for now

off topic question
do you happen to be Filipino by any chance?
+1 wernich wilsenach · August 17, 2015
you get lists and you get tuples. 

a python list is like a shopping list you make. you put items on your list that you'd like to buy, then your wife comes along and removes the beer, changes the full cream milk to low fat and adds perfume, magazines and a box of chocolates. a list is a collection of items that is "mutable" (you can change it).

example of a list:

>>> players = [1, 2, 3, 'bob', 'alan']
>>> players
[1, 2, 3, 'bob', 'alan']



you can make changes to the list:

>>> players[0] = 'steve'
>>> players
['steve', 2, 3, 'bob', 'alan']
>>> players.append('banana')
>>> players
['steve', 2, 3, 'bob', 'alan', 'banana']


and you can get sections of the list:


>>> players[2:4]
[3, 'bob']
>>> players[2:]
[3, 'bob', 'alan', 'banana']
>>> players[:4]
['steve', 2, 3, 'bob']
>>> 



a tuple is a list that cannot change. imagine a shopping list your wife makes. you can't change anything on there, because what she said is final.

example of a tuple:


>>> scores = (100, 200, 300, 400, 500)
>>> scores
(100, 200, 300, 400, 500)
>>> scores[2]
300
>>> scores[2:]
(300, 400, 500)
>>> scores[:2]
(100, 200)


you can read the values from a tuple in pretty much the same way as you do with a list, however, when you try to make any kinds of changes, you get that error you got:


>>> scores[3] = 123
Traceback (most recent call last):
  File "", line 1, in
    scores[3] = 123
TypeError: 'tuple' object does not support item assignment
>>> scores.append(123)
Traceback (most recent call last):
  File "", line 1, in
    scores.append(123)
AttributeError: 'tuple' object has no attribute 'append'



so:

list -> read and write. you can change it after you create it.
tuple -> once you create it, that's it. you can't add items, can't take any away and you can't change any of them.


the way to spot which one is being used is when it's created or when you print it out:

when you see (   ), then you have a tuple
when you see [    ], then you have a list.


hope that helps. ;)
  • 1
  • 2

Python

124,458 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator