创建元组
字典的创建
使用
,分割pythont = 1, 2, 'hello', 'world' print(type(t)) # <class 'tuple'> print(t) # (1, 2, 'hello', 'world')使用小括号
(、)pythont = (1, 2, "hello", "world") print(type(t)) # <class 'tuple'> print(t) # (1, 2, 'hello', 'world')注意: 当元组中仅有一个元素,使用小括号的形式创建元组时,需要在后面添加逗号","。
pythont = ("hello",) print(type(t)) # <class 'tuple'> print(t) # ('hello',)使用内置函数
tuple()pythont = tuple((1, 2, "hello", "world")) print(type(t)) # <class 'tuple'> print(t) # (1, 2, 'hello', 'world')
创建空元组
使用小括号方式
pythont = () print(type(t)) # <class 'tuple'> print(t) # ()使用内置函数
tuplepythont = tuple() print(type(t)) # <class 'tuple'> print(t) # ()