python的数据类型

基本数据类型:

>>数和字符串

列表:

>>python中没有数组的概念,与数组最接近的概念是列表和元祖,列表是存储一连串元素的容器

>>> str = ['ab','cd','ef','g']
>>> print(str[3])
g

>>修改列表中的内容

 

>>> str = ['ab','cd','ef','g']
>>> str[3] = 'gh'
>>> print (str)
['ab', 'cd', 'ef', 'gh']

 

元祖:python中没有数组的概念,与数组最接近的概念是列表和元祖,元祖是存储一连串元素的容器

 

>>> str = ('ab','cd','ef','g')
>>> print(str[1])
cd

 

>>只读取不能修改

>>> str = ('ab','cd','ef','g')
>>> abc = str[2]
>>> print(abc)
ef
>>> str[0] = 'a'
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    str[0] = 'a'
TypeError: 'tuple' object does not support item assignment

 

集合:

>>python中集合主要有两个功能:

 >>建立关系

>>> a = set('abcdef')
>>> b = set('abc')
>>> x = a&b  #交集
>>> print(x)
{'c', 'a', 'b'}
>>> y = a|b  #并集
>>> print(y)
{'b', 'e', 'f', 'c', 'a', 'd'}
>>> z = a-b  #差集
>>> print(z)
{'f', 'd', 'e'}

 

 >>消除重复元素

>>> a = ('abcdabcabc')
>>> new = set(a)
>>> print(new)
{'c', 'b', 'a', 'd'}

 

字典:

也叫作关联数组,在{}以键值对的形式存储

>>> dic = {'name':'xiaoxiao','age':1,'city':'beijing'}
>>> print(dic)
{'age': 1, 'city': 'beijing', 'name': 'xiaoxiao'}
>>> print(dic['name'])
xiaoxiao

>>添加修改字典

>>> dic = {'name':'xiaoxiao','age':1}
>>> print(dic)
{'age': 1, 'name': 'xiaoxiao'}
>>> dic['city'] = 'beijing'
>>> print(dic)
{'age': 1, 'city': 'beijing', 'name': 'xiaoxiao'}
>>> dic['age'] = 0
>>> print(dic)
{'age': 0, 'city': 'beijing', 'name': 'xiaoxiao'}

  

转载于:https://www.cnblogs.com/airener/p/5973853.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值