字典變數名稱 = {鍵1 : 值1, 鍵2 : 值2, 鍵3 : 值3 …}
注意:「鍵」必須唯一,若重覆使用,後面的「鍵」會覆蓋前面的「鍵」
字典取值的語法:
字典變數名稱['鍵']
範例:
- # -*- coding: utf-8 -*-
- """
- Created on Tue Jan 22 21:54:36 2019
- @author: 軟體罐頭
- """
- stock = {'1101':'台泥','1102':'亞泥','1103':'嘉泥','1104':'環泥','1108':'幸福','1109':'信大','1110':'東泥'}
- print('stock 的資料型態為:',type(stock))
- print()
- print('stock =',stock)
- print()
- print('1003 = ',stock['1103'])
執行結果: