数据类型的常用方法

一、可变类型与不可变类型
  1. 可变类型:值改变,但是id不变,证明就是在改变原值,是可变类型
  2. 不可变类型:值改变,但是id也跟着变,证明是产生了新的值,是不可变类型
二、数字类型常用方法

1.整型int
基本使用
1.1用途:记录年龄、等级、数量
1.2定义方式
age = 10 # age=int(10)
数据类型转换: 只能将纯数字的字符串转成int

2.浮点型float
基本使用
2.1用途:记录身高、薪资、体重
2.2定义方式

salary = 10.1 # salary=float(10.1)

数据类型转换: 只能将包含小数的字符串转成float

x=float('3.1')
print(x,type(x))

该类型总结
存一个值且不可变

三、字符串常用方法

基本使用
1、用途:记录描述性质的状态
2、定义方式:在单引号、双引号、三引号内包含一串字符串

msg='hello' # msg=str('hello')

数据类型转换:所有类型都可以被str转成字符串类型

res=str([1,2,3])
print(res,type(res))

3、常用操作+内置的方法
优先掌握的操作:(*****)
3.1、按索引取值(正向取+反向取) :只能取

msg='hello'
print(msg[0],type(msg[0]))
print(msg[-1])
print(msg[-2])

3.2、切片(顾头不顾尾,步长)

msg='hello world'
res=msg[0:3:1] # 0 1 2
print(res)
print(msg)
res=msg[:]
res=msg[::2] # 0 2 4 6 8 10
print(res)

msg='hello world'
res=msg[-1:-12:-1]
res=msg[-1::-1]
res=msg[::-1]
print(res)

3.3、长度len

msg='hello world'
print(len(msg))

3.4、成员运算in和not in:判断一个子字符串是否存在于大字符串中

msg='ccc is fff'
print('ccc' in msg)
print('dsb' in msg)
print('aaa' not in msg)
print(not 'aaa' in msg)

3.5、移除 空白strip: 用来去除字符串左右两边的字符,如果不指定字符默认去除的是空格

msg='      he llo     '
res=msg.strip()
print(res,id(res))
print(msg,id(msg))
print('******eg**on*****'.strip('*'))
print('***+-\/***peter#@$*****'.strip('*+-\/#@$'))
name=input('username>>>: ').strip() # name='peter      '
pwd=input('password>>>: ').strip()
if name == 'peter' and pwd == '123':
    print('login successful')
else:
    print('输错了。。。')

3.6、切分split:针对有规律的字符串,按照某种分隔符切成列表

info='peter:18:male'
res=info.split(':')
print(res,type(res))
print(res[0],res[1])

cmd='get|a.txt|33333'
print(cmd.split('|',1))

用join方法把特殊的符号作为连接符号将纯字符串的列表拼接成一个字符串

l=['peter', '18', 'male']  # 'peter:18:male'
res=l[0]+':'+l[1]+':'+l[2]
res=':'.join(l)
print(res)

3.7、循环

for item in 'hello':
    print(item)

需要掌握的操作
1、strip,lstrip,rstrip

print('******peter***********'.strip('*'))取两边字符
print('******peter***********'.lstrip('*'))去除左边的字符
print('******peter***********'.rstrip('*'))去除右边的字符

2、lower,upper

print('Abc123'.lower())将字符串内的大写转换为小写
print('Abc123'.upper())将字符串内的小写转换为大写

3、startswith,endswith

msg='alex is dsb'
print(msg.startswith('alex'))判断是不是某个字符串开始
print(msg.endswith('b'))判断是不是某个字符串结尾

4、format的三种玩法

res='my name is %s my age is %s' %('peter',18)
print(res)

res='my name is {name} my age is {age}'.format(age=18,name='peter')
print(res)

了解

res='my name is {} my age is {}'.format('peter',18)
res='my name is {0}{1} my age is {1}{1}{1}{1}'.format('peter',18)
print(res)

5、split,rsplit

msg='a:b:c:d'
print(msg.split(':',1))从左往右进行切分
print(msg.rsplit(':',1))从右往左进行切分

6、replace代替,把字符串内的字符进行替换

msg='kevin is kevin is hahahah'
res=msg.replace('kevin','sb',1)
print(res)

7、isdigit

print('123123'.isdigit()) # 如果字符串是由纯数字组成的,则返回True
print('123123   '.isdigit())
print('123123asdf'.isdigit())
print('12312.3'.isdigit())

了解的操作
1、find,rfind,index,rindex,count

print('123 ke123ke'.find('ke'))
print('123 ke123ke'.rfind('ke'))
print('123 ke123ke'.index('ke'))
print('123 ke123ke'.rindex('ke'))

print('123 ke123ke'.find('xxxx'))
print('123 ke123ke'.index('xxxx'))
print('123 ke123ke'.count('ke',0,6))

2、center,ljust,rjust,zfill

print('peter'.center(50,'*'))
print('peter'.ljust(50,'*'))
print('peter'.rjust(50,'*'))

print('peter'.rjust(50,'0'))
print('peter'.zfill(50))

3、captalize,swapcase,title

print('abcdef dddddd'.capitalize())
print('abcAef dddddd'.swapcase())
print('abcAef dddddd'.title())

4、is数字系列

num1=b'4' #bytes
num2='4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字

bytes与阿拉伯数字组成的字符串

print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

阿拉伯数字组成的字符串

print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal()

阿拉伯数字\中文\罗马组成的字符串

print(num2.isnumeric())
print(num3.isnumeric())
print(num4.isnumeric())

该类型总结
字符串类型只存一个值而且是有序,字符串的值是不可以改变的

四、列表的常用方法

基本使用
1、用途:按照位置记录多个值,索引对应值

2、定义方式:在[]内用逗号分隔开多个任意类型的值

l=['a',11,11.3,] # l=list(['a',11,11.3,])

数据类型转换:但凡能够被for循环遍历的数据类型都可以传给list,被其转换成列表

res=list('hello')
res=list(123)
print(res)

res=list({'a':1,'b':2,'c':3}) # []
print(res)

3、常用操作+内置的方法
优先掌握的操作:
3.1、按索引存取值(正向存取+反向存取):即可存也可以取

l=['a','b','c','d','e']
print(l[0])
print(l[-1])
print(id(l))
l[0]='A'
print(id(l))

强调强调强调!!!:对于不存在的索引会报错

3.2、切片(顾头不顾尾,步长)

**l=['a','b','c','d','e']
print(l[1:4])
print(l[::-1])**

3.3、长度

l=['a','b','c','d','e']
print(len(l))

3.4、成员运算in和not in

l=['a','b','c','d','e']
print('a' in l)

3.5、追加与insert;append是往末尾追加元素,insert是往制定位置增加元素

l=['a','b','c','d','e']
l.append('xxx')
l.append('yyy')
print(l)

l.insert(0,'xxxx')
print(l)

3.6、删除有del、remove和pop

l=['a','bbb','c','d','e']   # del是一种通用的删除操作,没有返回值
del l[0]
print(l)

dic={'k1':1}
del dic['k1']
print(dic)

l.remove(指定要删除的那个元素),没有返回值

res=l.remove('bbb')
print(l)
print(res)

l.pop(指定要删除的那个元素的索引),返回刚刚删掉的那个元素

l=['a','bbb','c','d','e']
l.pop(-1)
res=l.pop(1)
print(l)
print(res)

3.7、循环

l=['a','b','c','d','e']
for item in l:
    print(item)

练习:
队列:先进先出
l=[]
入队

l.append('first')
l.append('second')
l.append('third')
print(l

)
出队

print(l.pop(0))
print(l.pop(0))
print(l.pop(0))

堆栈:后进先出
只有在类中所有元素都是同种类型的情况下才能用sort排序

l=[1,3,2]
l.sort(reverse=True)

l=['z','d','a']
l.sort()
print(l)

该类型总结
列表可以存多个值而且这些值是有序的,列表内的元素是可以进行改变的

猜你喜欢

转载自blog.csdn.net/linwow/article/details/88779832