python dict tuple moduleちがい まとめ

~ python

Python 2.7.6 (default, Sep  9 2014, 15:04:36)

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> a = 1

>>> a

1

>>> a = "abc"

>>> a

'abc'

>>> a = [1,2,"abc"]

>>> a

[1, 2, 'abc']

>>> a[2]

'abc'

>>> a[1:2]

[2]

>>> a[0:3]

[1, 2, 'abc']

>>> a[-1]

'abc'

>>> a[:-1]

[1, 2]

>>> a = (1,2,3) #'tuple' 中身を変えられない

>>> b=[1,2,3] #これは中身を変えられる

>>> b[1]=4

>>> b

[1, 4, 3]

>>> a[1]=4

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'tuple' object does not support item assignment

 

<dictとは>

>>> a = {"aaa":1, "bbb":2,"ccc":3}

>>> a["aaa"]

1

>>> config = {

...    'database': 'testtwitter',

...    'user': 'root',

...    'password': 'password',

...    'host': 'localhost'

... }

>>> config.database

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'dict' object has no attribute 'database'

>>> config["database"]

'testtwitter'

>>>とすると呼べる。