首页 > 其他分享 >数据类型

数据类型

时间:2022-12-22 19:33:40浏览次数:36  
标签:11 22 study 数据类型 dic test print

input里面返回的是字符串

数字转成字符串用str

字符串转成数字用int

单行注释用#

多行注释用“”““””

一个Tab键等于四个空格

严格缩进

 字符串

#1、拼接
name = "zd"
age = "25"
nation = "han"  #nation是民族
hometown = "ay"
addition = name + age + nation + hometown
print(addition)
#2、倍增
name = "zd"
redouble = name * 10
print(redouble)
#3、首字母大写用capitalize
test = "zd"
v = test.capitalize()
print(v)
#4、字符串里面全部变小写
v1 = test.casefold()#使用于很多未知的
print(v1)
v2 = test.lower()#适用于常见的
print(v2)
#5、设置宽度将内容居中center
v3 = test.center(20,"*")#20代表总长度、*代表空白位置填充并且只能填充一个字符
print(v3)
#6、去字符串中寻找值出现的次数、开始位置到结束位置
v4 = test.count('z')
print(v4)
#7、判断以什么结尾endswith
v5 = test.endswith("d")
print(v5)
#8、判断以什么开始startswith
v6 = test.startswith("z")
print(v6)
#9、从开始往后找到第一个后获取其位置用find
v7 = test.find("z")
print(v7)
#10、格式化、将一个字符串中的占位符替换为指定的值用format
test = "i am {name},age {a}"
v8 = test.format(name = "zd",a = "25")
print(v8)
#11、format_map适用于字符串格式中可变数据参数来源于字典等映射关系数据时才可以使用
v9 = test.format_map({"name":"zd","a":25})
print(v9)
#12、判断字符串中只能是字母或数字isalnum
test = "zd123"
v10 = test.isalnum()
print(v10)
#13、expandtabs是扩充标签,默认是8个空格,可以指定参数在括号里面加20。
test = "username\temail\tpassword\txzzxczxc\tcxzczxc"
v11 = test.expandtabs(20)
print(v11)
#14、判断isalpha是否只是字母
test = "czxczx"
v12 = test.isalpha()
print(v12)
#15、判断是否是数字isdecimal和isdigit
test = "②"
v13 = test.isdecimal()
v14 = test.isdigit()
print(v13,v14)
#16、大小写转换swapcase
test = "ZD"
v15 = test.swapcase()
print(v15)
#17、isidentifier判断变量里面是否字母开头
a = "def"
v16 = a.isidentifier()
print(v16)
#18、是否存在不可显示的字符isprintable
test = "zd"
v17 = test.isprintable()
print(v17)
#19、判断是否全部空格
test = " "
v18 = test.isspace()
print(v18)
#20、把字符串转换成标题title
test = "zd name age num tel ss"
v19 = test.title()
print(v19)
#20、判断字符串是否是标题istitle
test = "Zd Name Age Num Tel Ss"
v20 = test.istitle()
print(v20)
#21、将字符串中的每一个元素按照指定分隔符进行拼接join
test = "我写的代码没BUG"
v21 = "_".join(test)
print(v21)
#22、左填充ljust
test = "zd"
v22 = test.ljust(20,"*")
print(v22)
#23、右填充rjust
test = "zd"
v23 = test.rjust(20,"*")
print(v23)
#24、左边去除空白lstrip
test = " zd"
v24 = test.lstrip()
print(print(v24))
#25、右边去除空白rstrip
test = "zd "
v25 = test.rstrip()
print(v25)
#26、全部去除空白strip
test = " zd "
v26 = test.strip()
print(v26)
#27、根据对应关系进行替换maketrans和translate
v27 = "abcdefghijklmn"
v28 = str.maketrans("abcd","1234")
v29 = v27.translate(v28)
print(v29)
#28、分割三份partition括号里面是以什么值作为分隔完毕以后是元组形式。
test = "abcdefghj"
v30 = test.partition("c")
print(v30)
#29、分割split以参数值作为分隔完毕以后是列表形式。
test = "abcdefghji"
v31 = test.split("c")
print(v31)
#30、换行分割splitlines完毕以后是列表形式。
test = "dsfsdf\nfdsfdsfds\ndsadasd\nvcxvcx"
v32 = test.splitlines()
print(v32)
#31、通过索引获取值
test = "zd"
v33 = test[1]
print(v33)
#32、切片是顾头不顾尾
test = "我写的代码没BUG"
v34 = test[0:-1]
print(v34)
#33、获取长度len
test = "zd"
v35 = len(test)
print(v35)
#34、while循环输出
test = "我写的代码没BUG"
index = 0
while index < len(test):
v36 = test[index]
print(v36)
index += 1
print("======================================")
#35、我写的代码没BUGfor循环输出
test = "我写的代码没BUG"
for sc in test:
print(sc)
#37、range系列
for i in range(10):
print(i)
#38、enumerate枚举将文件对应的索引打印出来得到的是元组形式。
test = input("请输入内容:")
for i in enumerate(test):
print(i)
39、replace替换:
name = "zdzzzzzzzzdz"
print(name.replace(name,"zs"))#全部替换
print(name.replace("zd","zs"))#部分替换
40、切片分割
name = "zslsww"
name.split('') # 默认按照空格进行分隔,从前往后分隔?
name.rsplit() # 从后往前进行分隔?
41、index查找索引位置
namess = "zdzs"
print(namess.index("d"))

数字:

#1、创建
age = 25
print(age)
#2、加
a = 1
b = 1
c = a + b
print(c)
#3、减
a = 1
b = 1
c = a - b
print(c)
#4、乘
a = 1
b = 1
c = a * b
print(c)
print(c)
#5、除
a = 1
b = 1
c = a/b
print(c)
#6、次方
a = 2 ** 5
print(a)
#7、获取余数
a= 39 % 8
print(a)
#8、用if判断奇偶数
a = input("请输入一个数字:")
if int(a) % 2 == 0 :
print("这个数是偶数")
else:
print("这个数是奇数")
#9、取商用双正斜杠线
a = 39 // 8
print(a)
#11、查看数据类型用type
print(type(b))
#12、转换成二进制用int
num = "0011"#10是2、11是3
v = int(num,base = 2)
print(v)
#13、一个字节等于8位、一个汉字在utf8里面是3个字节
#14、当前数字的二进制至少用几位表示用bit_length()
age = 5
r = age.bit_length()
print(r)

列表:

#1、中括号、逗号分隔
li1 = [1,2,3,4,"zd"]
#2、嵌套
li2 = [1,2,["zd"],3]
#3、通过索引取值
li3 = ["zd","zs","ls"]
print(li3[1])
#4、通过切片取值也是列表、顾头不顾尾
li4 = ["zs","ls","ww","zl"]
print(li4[1:3])
#5、列表可以直接for循环
for i in ["zs","ls","ww","zl"]:
print(i)
#6、通过索引位置修改
li5 = ["zs","ls","ww","zl"]
li5[1] = "xx"
print(li5)
#6.1通过切片区间修改
li5 = ["zs","ls","ww","zl"]
li5[1:3] = ["qq","ee"]
print(li5)
#7、删除del按索引位置
li6 = ["zs","ls","ww","zl"]
del li6[1]
print(li6)
#7.1切片按区间删除
li6 = ["zs","ls","ww","zl"]
del li6[1:3]
print(li6)
#8、支持in判断
li7 = ["zs","ls","ww","zl"]
print("ls" in li7)
#9、字符串转列表直接list按每个字符分开
li8 = list("cxczxcvzxvxzcx")
print(li8)
#10、追加append直接追加到列表最后的位置
li9 = [11,22,33,44]
li10 = li9.append(5)
print(li9)
#11、清空clear
li11 = [11,22,33]
li12 = li11.clear()
print(li11)
#12、复制copy
li13 = [11,22,3,4]
li14 = li13.copy()
print(li14)
#13、计数count
li15 = [11,22,33,44,44]
print(li15.count(44))
#14、extend可迭代增加也是按照列表里面的末尾顺序接着增加
li16 = [11,22]
li16.extend([9898,"8989"])
print(li16)
#15、只找第一个值的索引位置用index
li17 = [11,22,33,44]
li18 = li17.index(33)
print(li18)
#16、insert是按照索引位置插入
li19 = [11,22,33,44]
li19.insert(0,99)
print(li19)
#17、pop默认删除最后一个可以有返回值、可以在括号里面加参数也就是列表里面值的索引
li20 = [11,22,33,44]
li20.pop()
print(li20)
#17.1按索引位置删除
li21 = [11,22,33,44]
li21.pop(1)
print(li21)
#18、remove删除列表中指定的值默认第一个
li22 = [11,22,22,33,44]
li22.remove(22)
print(li22)
#19、翻转reverse
li23 = [11,22,33,44]
li23.reverse()
print(li23)
#20、排序sort默认是从小到大
li24 = [11,44,22,33]
li24.sort()
print(li24)
#20.1从大到小加reverse = True
li
=
[
'alex'
,
'eric'
,
'rain'
]

​ for​​​ ​​i,j ​​​​in​​​ ​​enumerate​​​​(li):​

​print​​​​(i​​​​+​​​​100​​​​,j)​

23、列表的函数和方法:

a = [1,2,3,4,5]

len(a)  # 列表元素的个数
max(a) # 返回列表元素最大值
min(a) # 返回列表元素最小值
list(tuple) #将一个可迭代对象转换为列表

元组:

#1、创建,不可被修改、删除、增加,最后加逗号
tu1 = (11,22,33,44,)
print(tu1)
#2、索引取值顾头不顾尾
tu2 = (11,2,33,44)
print(tu2[1])
print(tu2[0:2])
print(tu2[0:1])
#3、for循环
tu3 = (11,22,33,44,)
for i in tu3:
print(i)
可以直接for循环
for i in (11,22,33,44)
  print(i)
#4、字符串转元组用tuple
st = "fdfdsfds"
print(tuple(st))
#5、列表转元组用tuple
li = [11,22,33]
print(tuple(li))
#6、元组转列表list
tu4 = (1,2,3,4)
print(list(tu4))
#7、元组转字符串用str
tu5 = (9,8,7,6,4)
print(str(tu5))

字典:

#1、创建键值对出现
dic = {"k1":"v1",
"k2":"v2"
}
print(dic)
#2、字典嵌套列表
info = {
"k1":18,
"k2":True,
"k3":[
11,
22,
33,
{
"kk1":"vv1",
"kk2":"vv2",
"kk3":(11,22),
}
],
"k4":(11,22,33,44)
}
print(info)
#3、数字、字符串、布尔值、元组可以作为字典的key值
#4、取值
info = {
"k1":18,
"k2":True
}
print(info)
print(info["k1"])
#5、支持del删除
#6、for循环默认是key
info1 = {"k1":"v1","k2":"v2"}
for i in info1:
print(i,info1[i])
#7、根据序列,创建字典并指定统一的值fromkeys
info2 = dict.fromkeys(["k1","k3","999"],123)
print(info2)
#8、根据key获取值,key不存在时可以指定默认值None或指定的值get
info3 = {"k1":"v1"}
info4 = info3.get("k3",111)
print(info4)
#9、删除并获取返回值pop
info5 = {"k1":"v1"}
v = info5.pop("k1",90)
print(info5,v)
#10、设置值依据当前是否存在获取当前key对应的值setdefault
dic = {"k1":"v1","k2":"v2"}
v = dic.setdefault("k111111","123")
print(dic,v)
#11、update更新替换
dic = {"k1":"v1","k2":"v2"}
v = dic.update({"k1":"11111","k3":"3442342"})
print(dic,v)
12、replace替换
a = "zdxx"
print(a.replace("d","s"))
13、for循环取出key
dic = {"k1":"alex","k2":"aric","k3":"Alex","k4":"Tony"}
for i in dic:
print(i)
14、for循环取出value
dic = {"k1":"alex","k2":"aric","k3":"Alex","k4":"Tony"}
for i in dic.values():
print(i)
15、for循环set去重
dic = {"k1":"alex","k2":"aric","k3":"Alex","k4":"Tony","k5":"Tony"}
for i in set(dic.values()):
print(i)
16、for循环取出键值对
dic ={"k1":"alex","k2":"aric","k3":"Alex","k4":"Tony","k5":"Tony"}
for i in dic.items():
  print(i)
17、append追加值
dic = {"k1":"v1","k2":"v2","k3":[11,22,33]}
dic["k3"].append(44)
print(dic)
18、insert根据对应位置插入
dic = {"k1":"v1","k2":"v2","k3":[11,22,33]}
dic["k3"].insert(1,18)
print(dic)
19、for循环if判断append
li = [11,22,33,44,55,66,77,88,99,90]
dic = {"k1":[],"k2":[]}
for i in li:
if i > 66:
dic["k1"].append(i)
elif i < 66:
dic["k2"].append(i)
print(dic)

布尔值:

None、空字符串""、空列表[]、空元组()、空字典{}全部是False

集合

#1、创建,不同元素、无序、不可变类型、集合是不重复
s = {1,2,3,4,4,4,4,45}
print(s)
print(type(s))
#2、for循环
s1 = {"zs","ls","ww","zl"}
for i in s1:
print(i)
#3、set定义(前提是可迭代的)
s2 = set("hello")
print(s2)
#4、增加add
s3 = {1,2,3}
s3.add("6")
s3.add(6)
s3.add(6)
print(s3)
#5、清空clear
s4 = {1,2,3,4}
s4.clear()
print(s4)
#6、删除pop随机删除
s5 = {1,2,3,4,5,6}
s5.pop()
print(s5)
#7、指定删除remove
s6 = {1,2,3,6,7,8}
s6.remove(8)
print(s6)
#8、discard删除不会报错可以程序继续运行
s7 = {1,2,3,4,7,9}
s7.discard(4)
print(s7)
#9、交集intersection或&
study_python = {"zs","ls","ww","zl"}
study_linux = {"zs","ls"}
print(study_python.intersection(study_linux))
print(study_python&study_linux)
#10、并集union或|
study_python = {"zs","ls","ww","zl"}
study_linux = {"zs","ls"}
print(study_python.union(study_linux))
print(study_python|study_linux)
#11、差集difference
study_python = {"zs","ls","ww","zl"}
study_linux = {"zs","ls","xh"}
print("差集",study_python - study_linux)
print(study_linux - study_python)
print(study_python.difference(study_linux))
print(study_linux.difference(study_python))
#12、列表转集合用set
study_python1 = ["zs","ls","ww","zl"]
study_linux1 = {"zs","ls"}
list_study_python = set(study_python1)
list_study_linux = set(study_linux)
print(list_study_python)
print(list_study_linux)
#13、交叉补集是去除掉共同的部分后的集合symmetric_difference
study_python = {"zs","ls","ww","zl"}
study_linux = {"zs","ls","xh"}
print("交叉补集",study_python.symmetric_difference(study_linux))
s = study_linux - study_python
s.difference_update(study_python)
print(s)
#14、交集得空返回true用isdisjoint
s1 = {1,2}
s2 = {3,5}
print(s1.isdisjoint(s2))
#15、issubset是否子集或夫集返回true或false
s1 = {1,2}
s2 = {1,2,3}
print(s1.issubset(s2))
#16、判断是否父集issuperset
s1 = {1,2,}
s2 = {1,2,3}
print(s2.issuperset(s1))
#17、update
s1 = {1,2}
s2 = {1,2,3}
#18、frozenset
s = frozenset("hello")
print(s)
list(s)
print(s)



标签:11,22,study,数据类型,dic,test,print
From: https://blog.51cto.com/u_15920572/5963223

相关文章

  • typesc03_ts中的数据类型1基本数据类型
     ts类型中的基本数据类型:1.number:任意数字2.string:任意字符3.Boolean:布尔值4.字面量:限制变量的值就是该字面量的值5.any:任意类型,设置类型为any后相当于对改变量......
  • SQL Server 2019 常用数据类型
    SQLServer2019常用数据类型一、常用数据类型一般存储数据就是两种:数字和字符串数字分为整形和浮点型,根据精度和范围又可以细分字符串分为定长和不定长字符串,根据长......
  • SQL Server 2019 数据类型timestamp和datetime2的区别
    SQLServer2019数据类型timestamp和datetime2的区别一、数据类型timestamp数据类型:timestamp的值是二进制数。在插入或更新数据时,自动添加timestamp值,而且还是唯一的......
  • 用c++代码实现golang里面的map数据类型
    因为之前写过一篇golang数据类型分析的文章。包含slice、map、channel等。想写一篇用其它语言实现golang数据类型的代码,于是选中map作为实验对象。笔者之前写过5年的c++,......
  • JavaSE-day04-基本概念-数据类型&标识符&键盘录入
    Java基本概念本章包含数据类型,标识符以及键盘录入!数据类型数据类型的分类1.基本数据类型Java中基本数据类型分为4类8种。1.整数:byte,short,int,long细节:整数的......
  • java数据类型转换有哪几种?看这篇就够了!
    前言在上一篇文章中,壹哥给大家讲解了Java中的数据类型,从此大家就知道了基本类型和引用类型,尤其是8种基本类型的使用和各自特点。但实际上数据类型的使用还有很多更深入的......
  • 数据类型
    注释Java中的注释由三种:单行注释,多行注释,文档注释单行注释://多行注释:/**/文档注释:/***/标识符和关键字Java所有的组成部分都需要名字。类名,变量名以及方法名都被......
  • 组合数据类型
    集合集合创建集合的特点:(1)互异性(2)无序性(3)确定性。创建空集合只能用set() ,用{}创建的是一个空字典。集合本身是可变类型,但要求放入集合中的元素是不可变类型。​#使......
  • 第02期:MySQL 数据类型的艺术-大对象字段
    我以前分享过一篇​​《MySQL大对象一例》​​,今天就来详细说下大对象的优缺点以及使用场景。我们把MySQL的大对象类型分TEXT/BLOB和JSON两部分来说明。 一、TEX......
  • C语言各数据类型所占字节数
      编写C程序时需要考虑每种数据类型在内存中所占的内存大小,即使同一种数据类型在不同平台下所占内存大小亦不相同。为了得到某个类型在特定平台上的准确大写,可以使用s......