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 1
Oldest  Newest  Rating
+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 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?
0 Marco Alexis · August 18, 2015
Yes I am a Filipino.

Thanks for the answers. It really helped a lot. :)
0 Halcyon Abraham Ramirez · August 18, 2015
@MarcoAlexis dang man. welcome to the world of python. I asked that because Im Filipino too and I knew somebody with a family name of Maghuyop In Bohol. Now if you happen to live in Bohol too. that would seriously blow my mind. oh and yeah. welcome to t the new boston 
0 Marco Alexis · August 19, 2015
Yes my father is from bohol and we always spend Christmas there.
Thanks mate. Im glad there are people willing to help whenever someone raises questions here. This is my first week learning python.
0 Halcyon Abraham Ramirez · August 19, 2015
That's pretty friggin mind blowing holy sh*t "he is from boho"l? so I assume that you guys  aren't living in bohol?

but still what are the odds man! do you know cebuano?

yeah there. there is www.stackoverflow.com too. that place is a goldmine of tech info. just be careful because they don't really take too well newbie questions. but here it's all fine we're all newbies.

what got you into python? 
0 Marco Alexis · August 20, 2015
yup kabalo ko mag cebuano.ikaw?

hmm..thru suggestions in the internet. how long have you been learning python?
0 Halcyon Abraham Ramirez · August 20, 2015
Atay kuyawa ani bai. What are the odds bro taga bohol bya ko. Pro naa nako sa US ron. Mga 5-6 months nasad.

Sayon rna bai. Sisiw rnang python. Ga skwela kag comsci?
0 Marco Alexis · August 21, 2015
ahaha! bisaya jud d ay. wala bai. mechanical ko. gusto lang ko makat.on programming para hobby bah. hehe
unsa imong trabaho dha bai?
0 Marco Alexis · August 21, 2015
How did the indention vary the results?Can someone explain the difference please. :)
This;
def unli_numbers(*args):
start = 0
for a in args:
start += a
print(start)


unli_numbers(4, 6, 40, 50, 34, 566, 345, 678, 4, 567, 43, 55, 45, 88, 89)


2614

From this;
def unli_numbers(*args):
start = 0
for a in args:
start += a
print(start)


unli_numbers(4, 6, 40, 50, 34, 566, 345, 678, 4, 567, 43, 55, 45, 88, 89)


4
10
50
100
134
700
1045
1723
1727
2294
2337
2392
2437
2525
2614

Thanks :)
  • 1
  • 2

Python

107,046 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator