Python 入門 - その2
- Hello Python!
- tuple : イミュータブル
- list : ミュータブル
- 参考 => イミュータブル - Wikipedia
#!/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']
-
-
- MacBook% python3 collection.py
-
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'
-
-
- MacBook% python3 collection.py
-
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'
- 補足 : y が tuple が イミュータブル だから