- List
- Example: a=[1, 2, 3, 4, 5]
- Mutable (support modification of objects like a.append(6)
- Support stack operations like "pop", "append"
- looping (1 expression only) squares = [x**2 for x in range(10)]
- looping using traditional for
for x in [1,2,3]:
x = x**2 - Tuple
- Example: tup = (1, 2, 3, 'wahaha') (Always enclosed with parentheses!)
- Immutable -> tup[0] = 100 -> Not allowed!!
- Can contain lists
tup = (['a', 'b'], [1, 2]) - Tuple with single element should add trailing comma
tupSingle = (a,) - Sets
- Example: a = set([1, 2, 3])
- No element duplicate
- Example: 1 in a , Output: True (Fast testing)
- Mutable
- Dictionary
- Pretty similar to associative arrays in PHP
- {} -> Empty dict
- Mutable
- Example: cord = {'x' : 123.4, 'y' : 278.5}
- Get keys -> cord.keys() -> return list ['x', 'y']
- Fast Testing -> 'in'
2014年12月27日 星期六
[Python] Basic data types
There are several data structures in Python, the following are some key notes for reference
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言