基础-02元组和字典类型

目录

1 元组类型 

1.1元祖创建

1.2 空元祖类型创建:

1.3 元组修改

1.4 元组的删除

2 字典类型


1 元组类型 

1.1元祖创建

不需要括号可以但是一个元素就当成了字符串类型了

>>> tup1="a";

>>> type(tup1)

<class 'str'>

>>> tup2="a","b";

>>> type(tup2)

<class 'tuple'>

>>> tup3=(1,2,3,4);

>>> tup3

(1, 2, 3, 4)

>>> tup4=('zx','xkd',100)

>>> tup4

('zx', 'xkd', 100)

 

1.2 空元祖类型创建:

>>> tup=()

>>> tup

()

创建一个元素的元祖:

元祖是一个元素时元素后面须有一个,号,不然就当作整形处理

>>> tup=(30)

>>> tup

30

>>> type(tup)

<class 'int'>

>>> tup=(20,)

>>> type(tup)

<class 'tuple'>

 

1.3 元组修改

元组元素是不允许修改的,但可以进行元组的连接组合

>>> tup=(1,2,3)

>>> tup1=(2,3,4)

>>> tup2=tup+tup1

>>> print(tup2)

(1, 2, 3, 2, 3, 4)

 

1.4 元组的删除

>>> tup

(1, 2, 3)

>>> del tup

>>> tup

Traceback (most recent call last):

  File "<pyshell#6>", line 1, in <module>

    tup

NameError: name 'tup' is not defined

2 字典类型

dict = {'Name': 'zx', 'Age': 7, 'Class': 'First','Name': 'xkd'}
print ("dict['Name']: ", dict['Name'])#不允许同一个键被赋值两次,如果赋值则后一个被记住
dict['Age'] = 8;               # 更新 Age
dict['School'] = "haha"  # 添加信息

print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])
del dict['Name']
print('输出删除后的字典:',dict)
dict.clear() #清空字典
print('输出清空后的字典:',dict)
del dict # 删除字典
print('输出删除后的字典:',dict)
print('输出删除后的字典某键值对:',dict[age]) #出错 因为字典不存在

输出:

dict['Name']:  xkd
dict['Age']:  8
dict['School']:  haha
输出删除后的字典: {'Age': 8, 'Class': 'First', 'School': 'haha'}
输出清空后的字典: {}
输出删除后的字典: <class 'dict'>
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\2.py", line 15, in <module>
    print('输出删除后的字典某键值对:',dict[age]) #出错 因为字典不存在
NameError: name 'age' is not defined

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值