1、 The basic method .
lst = [1, 2, 3, 4, 5]
2、 Initializing consecutive numbers .
>>> lst = [n for n in range(5, 10)] >>> print(lst) [5, 6, 7, 8, 9]
3、 initialization n Same value .( Two ways )
>>> lst = ['x' for n in range(5)] >>> print(lst) ['x', 'x', 'x', 'x', 'x'] >>> lst = ['z']*5 >>> print(lst) ['z', 'z', 'z', 'z', 'z'] >>> lst = [0]*3 >>> print(lst) [0, 0, 0]
4、Python Dictionary of four data types 、 aggregate 、 list 、 Tuples , Use curly brackets 、 brackets 、 Parentheses indicate . Such as :
Dictionaries :dic={'a':12, 'b':34} aggregate :s = {1, 2, 3, 4} list :li=[1, 2, 3, 3] Tuples :tup=(1, 2, 3, 4) # Tuples are immutable lists
5、 Orderly dictionary ( collections.OrderedDict)
from collections import OrderedDict >>> OrderedDict([('b', 2), ('a', 1)])
Python 3.6 in dict Realized. PEP 468, So that the default dictionary can be kept in order , However, the function of saving order is considered not to be too dependent , The future may change .