python-数据类型

目录

一、空对象None

二、与数据类型有关的函数

1.type()

2.bool()

3.内置数字函数及math库

4.高精:decimal.Decimal(str)

每次都写decimal.Decimal太麻烦,改进:

三、数据类型

1.列表

列表的特点

操作列表的方法 

2.元组

定义单个元素

与列表区别:不可修改

操作方法:

 应用:

3.字符串

操作方法:

4.序列的切片

5.集合

定义集合:

集合特点:

集合操作方法:

6.字典

特点:

定义空字典:

字典操作方法:

7.range

 四、数据容器对比总结

五、数据容器的通用操作


一、空对象None

Python中还有一个特殊的类型称之为空对象,表示一个特殊的常量,表示什么都没有。

二、与数据类型有关的函数

1.type()

2.bool()

3.内置数字函数及math库

结果
3.2
(1, 2)
1024
4
1.24
True
False
3
11.0
11.0

12
(1+2j)
(1.2+0j)
(1.2+2j)
2.718281828459045
3.141592653589793
4
2.0
3
8
7.38905609893065
1.0
0.0
0.5

4.高精:decimal.Decimal(str)

浮点数误差:

解决方案:decimal模块里的Decimal构造函数

非常容易忘'',且不报错,非常坑!!!

每次都写decimal.Decimal太麻烦,改进:

三、数据类型

1.列表
列表的特点

操作列表的方法 


mylist=["zhouhy",666,True]
index=mylist.index("zhouhy")
print("zhouhy在列表中的下标索引值是"+str(index))
#index=mylist.index("hello")
#print(index)
mylist[1]=666666
print(f"列表被修改元素值后其为:{mylist}")
mylist.insert(1,"best")
print(f"列表插入元素后,结果是:{mylist}")
mylist.append("woshinb")
print(f"列表在追加了元素后,其结果是{mylist}")
mylist2=[1,2,3]
mylist.extend(mylist2)
print(f"列表在追加了一个新列表后,结果是{mylist}")
mylist=["zhouhy",666,True]
delmylist[2]
print(f"列删除元素之后结果是:{mylist}")
element=mylist.pop(1)
print(f"列删除元素之后结果是:{mylist}")
print(f"删除的元素是{element}")
mylist=[3,2,3,4,2,4]
mylist.remove(2)
print(f"after deleting the element'2',the list is:{mylist}")
mylist.clear()
print(f"the list is empty after being cleared:{mylist}")
mylist=[2,3,4,3,2,1]
count=mylist.count(3)
print(f"the number of element'3' in the list is:{count}")
count=len(mylist)
print(f"the number of element in the list is:{count}")
list=[1,2,3,4,5,6,7,8,9,10]
mylist=[]
index=0
while index<len(list):
element=list[index]
if(element%2==0):
mylist.append(element)
index+=1
print(mylist)
list=[1,2,3,4,5,6,7,8,9,10]
mylist=[]
for element in list:
if element%2==0:
mylist.append(element)
print(mylist)
2.元组
定义单个元素

与列表区别:不可修改

特例:如果元组里有一个列表,那么列表可以修改

t1=("hello",(2,3,4),(6,5,3),[2,3,4,5])
t1[3][2]='hello'
print(t1)

操作方法:

tuple.index()

tuple.count()

len(tuple)

t1=("hello",(2,3,4),(6,5,3))
index=0
while index<len(t1):
    element=t1[index]
    print(f"the element of the list is:{element}")
    index+=1
for element in t1:
    print(f"the element of the list is:{element}")
 应用:
t1=('周杰伦',11,['football','music'])
num1=t1.index(11)
print(num1)
print(t1[0])
#t1[2].pop(0)
del t1[2][0]
t1[2].append("coding")
print(t1)
3.字符串

不可修改的字符数据容器(老字符串)

操作方法:

str.index()

str.replace(str1,str2) 本质上返回了一个新的字符串

str.split()

str.count(子字符串)

str.find(子字符串)

str.rfind(子字符串)

str.strip()

str.lstrip()  str.rstrip()

‘ char’.join(List) 为split()逆方法

str="today i'm here to let you know who is the best person in the world"
list=str.split(" ")
print(list)

['today', "i'm", 'here', 'to', 'let', 'you', 'know', 'who', 'is', 'the', 'best', 'person', 'in', 'the', 'world']

4.序列的切片
string='万过薪月,员序程马黑来,nohtyP学'
string=string[::-1]
string=string[9:14]
print(string)

string1='万过薪月,员序程马黑来,nohtyP学'
string1=string1[5:10]
string1=string1[::-1]
print(string1)

string2='万过薪月,员序程马黑来,nohtyP学'
list=string2.split(',')
string2=list[1].replace('来','')
string2=string2[::-1]
print(string2)
5.集合
定义集合:
my_set_empty=set()
my_set={1,2,3,3,2,1,2,3,1}
print(my_set)
print(type(my_set))
print(my_set_empty)
print(type(my_set_empty))

集合特点:

无序(不支持下表索引访问),去重,允许修改,只支持for循环,不是序列

集合操作方法:

set.add(element)

set.remove(element)

set.pop()

set.clear()

set3=set1.difference(set2)

set1.difference_update(set2)

set3=set1.union(set2)

len(set)

6.字典
特点:

Key:Value 键值对组成,Key为不可变的数据类型,Value则可以为任意数据类型(包括字典),Key不允许重复添加,否则覆盖。字典不可由下标索引,而是通过Key去检索Value(Value=dict[Key])

定义空字典:

1.my_dict=dict()

2.my_dict={}

字典操作方法:

增加元素:dict[Key]=Value

更新元素:dict[Key]=Value

删除元素并且得到其Value:Value=dict.pop(element)

获取全部keys,遍历字典:

my_dict={"zhou":99,"lin":88,"wang":77}
keys=my_dict.keys()
print(keys)

for key in keys:
    print(key+' '+str(my_dict[key]))

直接从my_dict里获取keys,遍历字典:

for key in  my_dict:
    print(key + ' ' + str(my_dict[key]))

len(dict)

7.range

 range也可以通过下标索引,不可修改,range(i,j,k)表示从[i,j-1]步进为k的range

 四、数据容器对比总结

五、数据容器的通用操作

len()

max()

min()

list()

tuple()

str()

set()

sorted(容器名)从小到大排序

sorted(容器名,reverse=true) 从大到小排序

  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值