2020-11-12

# python操作一
 @Time      :2020/11/12 19:08     
 @Author    :Conrad6
 @File      :不同数据类型的操作.py
 @Software  :PyCharm
## 创建
a = " "   #空字符串print(a,type(a))>> <class 'str'>
a = str() #空字符串print(a,type(a))>> <class 'str'>
a = []    #空列表print(a,type(a))>>[] <class 'list'>
a = list()#空列表print(a,type(a))>>[] <class 'list'>
a = ()#空元组print(a,type(a))>>() <class 'tuple'>
a = tuple()   #空元组print(a,type(a))>>() <class 'tuple'>
a = {} #空字典print(a,type(a))>>{} <class 'dict'>
a = dict()#空字典print(a,type(a))>>{} <class 'dict'>
a = set()#空集合print(a,type(a))>>set() <class 'set'>
### 小结:空字符、列表、元组和字典都有两种创建的方法,分别是直接创建和用函数创建,
#而字典只能用函数创建,不能直接创建。
## append()方法
 a = ''
 a.append('b')#无效print(a)>>AttributeError: 'str' object has no
 # attribute 'append'
 a = ()
 a.append('b')#无效print(a)>>AttributeError: 'tuple' object has no
 attribute 'append'
 a = set()
 a.append('b')#无效print(a)>>AttributeError: 'tuple' object has no
 ## attribute 'append'
a.append('b')#无效print(a)>>AttributeError: 'tuple' object has no
# attribute 'append'
 a = {}
 a.append('b')#无效print(a)>>AttributeError: AttributeError: 'dict' object has no
 # attribute 'append'
 a = []
 a.append('b')#添加成功print(a)>>['b']
 ### 小结:append()方法只适用于给列表末尾添加元素,不适用于其他(字符串、元组、字典和集合)
## insert()方法
 a = 'python'
 a.insert(2,'c')#插入元素失败print(a)>>AttributeError: 'str' object has no
 # attribute 'insert'
 a = (1,2,3,4)
 a.insert(2,'c')#插入元素失败print(a)>>AttributeError: 'tuple' object has no
 # attribute 'insert'
 a = {1,2,3,4}
 a.insert(2,8)#插入元素失败print(a)>>AttributeError: 'set' object has no
 # attribute 'insert'
 a = {'name':1,'key':4,'hello':8}
 a.insert(2,'aaa',8)#插入元素失败print(a)>>AttributeError: 'dict' object has no
 # attribute 'insert'
 a = [1,2,3,4]
 a.insert(2,9)#插入元素成功print(a)>>[1, 2, 9, 3, 4]
### 小结:list1.insert(n,b)只适用于向列表list1的2个元素前插入元素b,对其他字符串、元组、
# 字典和集合无效。
## extend()方法插入元素
 a = [1,2,3,4]
 b = [7,8]
 a.extend(b) #插入成功print(a)>>[1, 2, 3, 4, 7, 8]
 a ={1:2,2:4}
 b = {4:2,3:4}
 a.extend(b)
### 小结:a.extend(b)只适用于将列表b中从元素全部插入到列表a的后面,不适用对元组、集合、字符串和字典。
## update()插入元素
 a ={1:2,2:4}
 b = {4:2,3:4}
 a.update(b)#将字典b中的元素插入到字典a末尾print(a)>>{1: 2, 2: 4, 4: 2, 3: 4}
 a ={1,2,3}
 b = {3,54}
 a.update(b)#将集合b中的元素插入到集合a末尾并去重print(a)>>{1, 2, 3, 54}
 a ='abcd'
 b = 'ef'
 a.update(b)
### 小结:a.update(b)只适用于将字典(集合)b中的元素插入到字典(集合)a的末尾
 (并去重),不适用于列表、元组和字符串
## get()获取元素
a = {'name':3,'jkey':5}
 b=a.get('name')#取值成功print(b)>>3
### 小结:a.get('name')只适用于提取字典中键名为'name'的值,不适用于列表、元组、集合和字符串
## remove()删除元素
 a = {1,2,3}
 a.remove(1)#删去集合中的元素1,无返回值
 a = [1,2,3]
 a.remove(2)
### 小结:a.remove(b)适用于集合和列表,意思是删除a中的b元素。
 不适用于元组、字符串、字典
## pop()删除元素
 a = {'name':3,'jkey':5}
 a.pop(1)
### 小结:a.pop(n)用于删除a中下标(索引)为n的元素,并返回该元素,
 不写n时默认删除最后一个元素。只适用于列表,不适用于字符串、元组、集合
## reverse()逆序和sort()升、降序排列
 a = [2,3,1,5]
 a.reverse()#逆序排列print(a.reverse())>>[5,1,3,2]
 a.sort(reverse=True) #降序排列  >>[5,3,2,1]
 a.sort(reverse=False)#升序排列  >>[1,2,3,5]
 a.sort()             #升序排列  >>[1,2,3,5]
### 小结:reverse()逆序和sort()升、降序排列只适用于列表,a.reverse()表示对列表a进行逆序排列,不适用于字符串、
 元组、集合和字典
## clear()清空操作
 a = {'name':1,'ni':3}
 a.clear()
### 小结:a.clear()表示清空操作,适用于集合、列表、字典,不适用于元组、字符串。
## 用[索引/下标]取值操作
 b = (1,2,3)
 a = b[1]
 print(a)
### 小结:用下标取值适用于字符串、列表、元组,不适用于集合和字典(因为集合无序,字典无索引)
## setdefault()添加元素
a = {'na':1,'me':4}
a.setdefault('key',4)
### 小结:a.setdefault('name',value)只适用于将键名为'name',值为value的元素插入到
#字典a的末尾,如果原字典中有该键名的元素,则对该键值在元位置处进行修改。不适用于其他数据类型。
## del删除命令
a = {'ame':2,"he":2}
del a['he']#删除字典中键名为'he'的键值对,不值返回
### 小结:del命令不能删除字符串、元组、集合中的元素,可以删除列表和字典中的元素,
# 列表中的元素以索引号删除,而字典中的键值对用键名删除
## copy()复制操作
 a = list('hello')
 b = a.copy()
 print(a is b)#>>False
 a = set('hello')
 b = a.copy()
 print(a is b)#>>False
 a = dict(hello=1,he=3)
 b = a.copy()
 print(a is b) #>>False
### 小结:copy()适用于列表、集合和字典,不适用于字符串、元组
## add()添加元素
 a = {1,2,3}
 a.add(5)
 print(a)
###小结:a.add(e)表示在集合a中添加元素e,如果有该元素则停止添加
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值