Python 入門 - その2



#!/usr/bin/env python 3


# get length
print (len*1 # answer : 6
print (len("purposefully")) # answer : 12


x = ["cat", 77, -333, "Ryu", 123]
# y = ("dog", 55, -111, "Day", 789)


x.append("Good")
# y.append("Bad") # answer : process down here


print (x) # answer : ['cat', 77, -333, 'Ryu', 123, 'Good']
# print (y)

1
6
12
['cat', 77, -333, 'Ryu', 123, 'Good']

1
6
12
['cat', 77, -333, 'Ryu', 123, 'Good']

#!/usr/bin/env python 3


# get length
print (len*2 # answer : 6
print (len("purposefully")) # answer : 12


x = ["cat", 77, -333, "Ryu", 123]
y = ("dog", 55, -111, "Day", 789)


x.append("Good")
y.append("Bad") # answer : process down here


print (x) # answer : ['cat', 77, -333, 'Ryu', 123, 'Good']
# print (y)

1
6
12
Traceback (most recent call last):
File "collection2.py", line 12, in
y.append("Bad") # answer : process down here
AttributeError: 'tuple' object has no attribute 'append'

1
6
12
Traceback (most recent call last):
File "collection2.py", line 12, in
y.append("Bad") # answer : process down here
AttributeError: 'tuple' object has no attribute 'append'


  • エラー理由 : 'tuple' object has no attribute 'append'

*1:"two",))) # answer : 1 print (len([9, 8, 7, 6, "kuma", 5]

*2:"two",))) # answer : 1 print (len([9, 8, 7, 6, "kuma", 5]