python数据类型

Posted 三千丝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python数据类型相关的知识,希望对你有一定的参考价值。

本文目录

 

数字类型 

 整型:int

   用途:记录年龄、等级、号码

# int(\'abadf\') #报错
# int(\'10.1\') #报错
# int(\'101\') #int只能将字符串中包含纯数字转成整型
# 其他进制转成十进制
# 二进制:0,1
# 10 #1*(2**1) + 0*(2**0)
# # 十进制:0-9
# 371 #3*(10**2) + 7*(10**1) + 1*(10**0)
# # 八进制:0-7
# 371 #3*(8**2) + 7*(8**1) + 1*(8**0)
# # 十六进制:0-9 A-F
# 371 #3*(16**2) + 7*(16**1) + 1*(8**0)

# 十进制转成其他进制
# print(bin(12))
# print(oct(12)) #14 =>1*(8**1) + 4*(8**0)
# print(hex(16))
进制转换(了解)

 

浮点型:float

  用途:记录身高、体重、薪资

x=10.3
print(id(x))
x=10.4
print(id(x))

"""
2228681159088
2228681158848
"""

整型类型:存一个值,不可变

 

 

字符类型:str
  作用:记录描述性质的数据,比如人的名字、性别、家庭地址、公司简介
  定义:在引号内按照从左到右的顺序依次包含一个个字符,引号可以是单引号、双引号、三引号(可以存多行)  

  注意引号的配对

print("my name is \'sanqiansi\'")
print(\'my name is "sanqiansi"\')

 强调:
1、字符串之间可以相加:
\'1111\'+2222
2、字符串相加是新申请内存空间然后拷贝相加的字符串到新的空间中,效率不高

print(\'my name is \'+\'sanqiansi\'+\' my age is \'+\'18\')

3、字符串还可以做乘法运算

print(\'hello\'*10)
print(\'=\'*100)

 常用操作+内置方法

#1、按索引取值(正向取+反向取) :只能取
msg=\'hello world\'
print(msg[4])
print(msg[-1])
msg[3]=\'A\'    不支持,报错

#2、切片(顾头不顾尾,步长)
msg=\'hello world\' # 就是从一个大的字符串中切分出一个全新的子字符串
print(msg[0:5])
print(msg) # 没有改变原值

print(msg[0:5:1])  #步长为1
print(msg[0:5])   #hello
print(msg[0:5:2]) #hlo

# 了解:
print(msg[0:5:1])
msg=\'hello world\'
print(msg[5:0:-1])
print(msg[5::-1])
print(msg[-1::-1])
print(msg[::-1])

"""
hello
 olle
 olleh
dlrow olleh
dlrow olleh
"""

#3、长度len
msg=\'hello world\'
print(len(msg))


#4、成员运算in和not in: 判断一个子字符串是否存在于一个大的字符串中
print(\'xxx\' not in \'yb is shuaige\') # 推荐

#5、去掉字符串左右两边的字符strip,不管中间的
user=\'      yb       \'
user=\'   x   yb       \'
user="*******yb********"
user=" **+*  */***yb*  **-*****"
print(user.strip("* +/-"))

#6、切分split:针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值
msg="root:123456:0:0::/root:/bin/bash"
res=msg.split(\':\')
print(res)
print(res[1])

"""
[\'root\', \'123456\', \'0\', \'0\', \'\', \'/root\', \'/bin/bash\']
123456
"""
#根据需要取某个特定的字段
cmd=\'dowload|a.txt|3333333\'
cmd_name,filename,filesize=cmd.split(\'|\')


#7、循环
msg=\'hello\'
for item in msg:
     print(item)
"""
h
e
l
l
o
"""
优先掌握的操作
#1、strip,lstrip,rstrip
print(\'*****yb*****\'.lstrip(\'*\'))
print(\'*****yb*****\'.rstrip(\'*\'))
print(\'*****yb*****\'.strip(\'*\'))

#2、lower,upper
msg=\'aABBBBb\'
res=msg.lower()
print(res)
print(msg)

#3、startswith,endswith
msg=\'yb is shuaige\'
print(msg.startswith(\'yb\'))
print(msg.endswith(\'ge\'))
print(msg.endswith(\'e\'))

#4、format的三种玩法
print(\'my name is %s my age is %s\' %(\'yb\',18))
print(\'my name is {name} my age is {age}\'.format(age=18,name=\'yb\'))

# 了解
print(\'my name is {} my age is {}\'.format(18,\'yb\'))
print(\'my name is {0} my age is {1}{1}\'.format(18,\'yb\'))

#5、split,rsplit
msg=\'get|a.txt|333331231\'
print(msg.split(\'|\',1))
print(msg.rsplit(\'|\',1))
"""
[\'get\', \'a.txt|333331231\']
[\'get|a.txt\', \'333331231\']
"""

#6、join
msg=\'get|a.txt|333331231\'
l=msg.split(\'|\')
print(l)   #[\'get\', \'a.txt\', \'333331231\']

src_msg=\'|\'.join(l)
print(src_msg)    #get|a.txt|333331231

#7、replace
msg=\'zs say i have one dog,zs is zs hahaha\'
print(msg.replace(\'zs\',\'dog\',1))  #数字为替换的个数
print(msg)


#8、isdigit # 判断字符串中包含的是否为纯数字
print(\'10.1\'.isdigit())  #False
age=input(\'>>: \').strip()
if age.isdigit():
    age=int(age) #int(\'asfdsadfsd\')
    if age > 30:
        print(\'too big\')
    elif age < 30:
        print(\'too small\')
    else:
        print(\'you got it\')
else:
    print(\'必须输入数字\')
需要掌握的
#1、find,rfind,index,rindex,count
msg=\'hello yb is shuaige\'
print(msg.find(\'yb\'))  #找到返回第一个字母的索引位置
print(msg.find(\'yb\',0,3))  #找不到返回-1

print(msg.index(\'yb\'))   #6    找到返回索引位置
print(msg.index(\'yb\',0,3))  #找不到直接报错

msg=\'yb aaa yb\'
print(msg.find(\'yb\'))  #0
print(msg.rfind(\'yb\'))  #7

msg=\'yb aaa yb\'
print(msg.count(\'yb\')) # 统计一个子字符串在大字符串中出现的次数,返回2

#2、center,ljust,rjust,zfill
print(\'yb\'.center(20,\'*\'))  #*********yb*********
print(\'yb\'.ljust(20,\'*\'))   #yb******************
print(\'yb\'.rjust(20,\'*\'))   #******************yb
print(\'yb\'.zfill(20))      #000000000000000000yb

#3、expandtabs:把字符串中的 tab 符号(\'\\t\')转为空格,tab 符号(\'\\t\')默认的空格数是 8
print(\'a\\tb\'.expandtabs(1))  #数字代表是几个空格,不写默认空8个


#4、captalize,swapcase,title
print(\'hello\'.capitalize())  #Hello
print(\'hElLo\'.swapcase())   #HeLlO
print(\'yb is nb\'.title())     #Yb Is Nb


#5、is数字系列
#在python3中
num1=b\'4\' #bytes
num2=u\'4\' #unicode,python3中无需加u就是unicode
num3=\'\' #中文数字
num4=\'\' #罗马数字

# \'\'.isnumeric(): unicode,中文数字,罗马数字
print(num2.isnumeric())  #True
print(num3.isnumeric())  #True
print(num4.isnumeric())  #True

# \'\'.isdigit() :bytes,unicode
print(num1.isdigit())  #True
print(num2.isdigit())  #True
print(num3.isdigit())  #False
print(num4.isdigit())  #False


#6、is其他
print(\'abc你\'.isalpha()) # 字符串中包含的是字母或者中文字符    #True

# 字符串中包含的是字母(中文字符)或数字
print(\'ab\'.isalnum())    #True
print(\'123123\'.isalnum())    #True
print(\'ab123\'.isalnum())    #True
print(\'ab123啊\'.isalnum())    #True
需要了解的

 

 

 列表类型:list
  作用:记录/存多个值,可以方便地取出来指定位置的值,比如人的多个爱好,一堆学生姓名
  定义:在[]内用逗号分隔开多个任意类型的值

l1=list(\'hello\') #list就相当于调用了一个for循环依次取出\'hello\'的值放入列表
print(l1)   #[\'h\', \'e\', \'l\', \'l\', \'o\']
l2=list({\'x\':1,\'y\':2,\'z\':3}) 
print(l2)     #[\'x\', \'y\', \'z\']  只打印出key
# list(10000) # 报错

 

l=[10,3.1,\'sanqiansi\',[\'a\',\'b\']] # l=list([10,3.1,\'sanqiansi\',[\'a\',\'b\']])
print(l)
print(l[0])
print(l[2])
print(l[3])
print(l[3][1])

l1=[\'a\',\'b\',[\'c\',[\'d\',]]]
print(l1[2][1][0])
print(type(l))

 

使用:

hobbies="read music sleep eat play"
hobbies=["read","music","sleep","eat","play"]
print(hobbies[2])

students_info=[
[\'yb\',18,[\'play\',]],
[\'zs\',18,[\'play\',\'sleep\']]
]
print(students_info[1][2][0])hobbies="read music sleep eat play"
hobbies=["read","music","sleep","eat","play"]
print(hobbies[2])

students_info=[
[\'yb\',18,[\'play\',]],
[\'zs\',18,[\'play\',\'sleep\']]
]
print(students_info[1][2][0])

 常用操作+内置方法

#1、按索引存取值(正向存取+反向存取):即可存也可以取
l=[\'yb\',\'zs\',\'yxd\']
print(l[0])
l[0]=\'YB\'
print(l)
print(l[-1])
print(l[3]) #报错
# l[0]=\'YB\' # 只能根据已经存在的索引去改值
# l[3]=\'xxxxxxxx\' #如果索引不存在直接报错

#2、切片(顾头不顾尾,步长)
l=[\'yb\',\'zs\',\'yxd\',444,555,66666]
print(l[0:5])  #[\'yb\', \'zs\', \'yxd\', 444, 555]
print(l[0:5:2])  #[\'yb\', \'yxd\', 555]
print(l[::-1])  #[66666, 555, 444, \'yxd\', \'zs\', \'yb\']

#3、长度
l=[\'yb\',\'zs\',\'yxd\',444,555,66666,[1,2,3]]
print(len(l))   #7

#4、成员运算in和not in
l=[\'yb\',\'zs\',\'yxd\',444,555,66666,[1,2,3]]
print(\'zs\' in l)
print(444 in l)


#5、追加
l=[\'yb\',\'zs\',\'yxd\']
l.append(44444)
l.append(55555)
print(l)  #[\'yb\', \'zs\', \'yxd\', 44444, 55555]


#6、往指定索引前插入值
l=[\'yb\',\'zs\',\'yxd\']
l.insert(0,11111)
print(l)  #[11111, \'yb\', \'zs\', \'yxd\']
l.insert(2,2222222)
print(l)  #[11111, \'yb\', 2222222, \'zs\', \'yxd\']

#7、删除
l=[\'yb\',\'zs\',\'yxd\']

# 单纯的删除值:
# 方式1:
del l[1] # 通用的
print(l)  #[\'yb\', \'yxd\']

# 方式2:
res=l.remove(\'zs\') # 指定要删除的值,返回是None
print(l,res)  #[\'yb\', \'yxd\'] None

# 从列表中拿走一个值
res=l.pop(-1) # 按照索引删除值(默认是从末尾删除),返回删除的那个值
print(l,res)  #[\'yb\', \'zs\'] yxd

#8、循环
l=[\'yb\',\'zs\',\'yxd\']
for item in l:
    print(item)
"""
yb
zs
yxd
"""
优先掌握的操作
l=[\'yb\',\'yb\',\'zs\',\'yxd\',444,555,66666]
print(l.count(\'yb\')) #2
print(l.index(\'yb\')) #0
# print(l.index(\'yxd\',0,1))#报错
l.clear() #清空列表

#extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
l=[\'yb\',\'yb\',\'zs\',\'yxd\',444,555,66666]
items=[\'a\',\'b\',\'c\']
# items=\'hello\'
for item in items:
    l.append(item)
l.extend(items)
print(l)
#[\'yb\', \'yb\', \'zs\', \'yxd\', 444, 555, 66666, \'a\', \'b\', \'c\', \'a\', \'b\', \'c\']

l=[\'yb\',\'yb\',\'zs\',\'yxd\',444,555,66666]
l.reverse()
print(l)  #[66666, 555, 444, \'yxd\', \'zs\', \'yb\', \'yb\']


nums=[3,-1,9,8,11]
nums.sort(reverse=True)
print(nums)  #[11, 9, 8, 3, -1]


items=[1,\'a\',\'b\',2]
items.sort()# 报错,整型和字符串不支持一起排序
需要掌握的操作
# 队列:先进先出
l=[]
# 入队
l.append(\'first\')
l.append(\'second\')
l.append(\'third\')
print(l)
# 出队
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))
"""
[\'first\', \'second\', \'third\']
first
second
third
"""

# 堆栈:先进后出
l=[]
# 入栈
l.append(\'first\')
l.append(\'second\')
l.append(\'third\')
print(l)
# 出栈
print(l.pop())
print(l.pop())
print(l.pop())
"""
[\'first\', \'second\', \'third\']
third
second
first
"""
队列与堆栈

 

元组类型:touple

   用途:记录多个值,当多个值没有改的需求,此时用元组更合适

  定义方式:在()内用逗号分隔开多个任意类型的值

常用操作+内置方法