Day 05 可变不可变、数据类型内置方法

1.可变类型:值改变,但是id不变,证明就是改变原值,是可变类型

2.不可变类型:值改变,但是id也跟着改变,证明是产生新的值,是不可变类型

数字类型

一、整型int

1、用途:记录年龄、等级、数量

2、定义方式

age = 18

数据类型转换:只能将纯数字的字符串转成int

二、浮点型float

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

2、定义方式

salary = 10.1

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

x =float('3.1')

x=3.1

print(id(x))

x=4.3

print(id(x))

三、字符串类型

1、用途:记录描述性质的状态

2、定义方式:在单引号、双引号、三引号内包含一串字符串
msg='hello'

数据类型转换

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

print(res,type(res))

3、常用操作+内置方法

优先掌握操作:

3.1按索引取值(正向取+反向取):只能取

msg='hello'
print(msg[0],type(msg[0]))   正取
print(msg[-1])  反取

msg[0]='H' #只能取

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

msg='hello world'

res=mes[0:3:1]

print(res)==>hel   正切

msg='hello world'

res=msg[-1:-12:-1]

res=msg[-1::-1]

res=msg[::-1]

print(res) ==>dlrow  反取

3.3长度len

msg=‘hello world’

print(len(msg))==>11

3.4成员运算in和not in

判断一个字符串是否存在于大字符串中

msg='kevin is dsb'

print('kevin' in msg)

print('dab' in msg)

print('aaa' not in msg)

print(not 'aaa' in msg)

3.5移除空白

用来去除字符串左右两边的字符,不指定默认去除的是空格

msg='    hello '

res=msg.strip()

pirint(res,id(res))

print(msg.id(msg))

print('************eg**on***********'.strip('*'))

用户名密码认证去除空格

name = input('username==>:').strip()

pwd = input('password==>:'),strip()

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

info = ‘egon:18:male’

res= info.split(':')

print(res,type(res))

print(res[0],res[1])

cmd ='get|a.txt|33333'

print(cmd.split('|',1))

用:号作连接符号将纯字符串的列表拼接成一个字符串

l=['egon','18','male']

res = ':'.join(1)

print(res)==>egon:18:male

3.7循环

for item in 'hello'

  print(item)

需要掌握的操作

1、strip,lstrip,rstrip

print('***********egon**********'.strip('*'))==>egon

print('***********egon**********'.lstrip('*'))==>egon**********

 print('***********egon**********'.rstrip('*'))==>***********egon

2、lower,upper

print('Abc123'.lower())==>abc123

print('Abc123'.upper())==>ABC123

3、startswitch,endswitch

startswitch()方法用于检查字符串是否是以指定子字符串开头

endswith() 方法用于判断字符串是否以指定后缀结尾

msg='alex is dsb'

print(msg.startswith('alex'))==>True

print(msg.endswith('b'))==>True

4、format的三种玩法

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

print(res)==>my name is egon my age is 18

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

print(res)==>my name is egon my age is 18

5、split,rsplit

msg='a:b:c:d'

print(msg.split(':',1))==>['a', 'b:c:d']

print(msg.rsplit(':',-1))==>['a:b:c', 'd']

6、replace

replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

msg='kevin is kevin is hahahah'

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

print(res)==>sb is kevin is hahahah

7、isdigit

如果字符串是由纯数字组成的,则返回True

print('12345'.isdigit())==>True

成绩验证必须输入数字

score=input('>>>>:').strip()

if score.isdigit():

总结:字符串只能存一个值

   字符串是有序的

   字符串是不可变类型

四、列表类型list

1.。用途:按照位置记录多个值,索引对应值

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

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

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

print(res)==>['a', 'b', 'c']

3、常用操作+内置方法

有先掌握的操作:

3.1按照索引取值(正向反向存取):即可存也可取

l=['a','b','c','d','e']

print(l[0])

print(l[-1])

print(id(l))

1[0]='A'

print(id(1))

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

l[5]='AAAA'

dic={"k1":111}

dic['k2']=2222

print(dic)==>{'k1': 111, 'k2': 2222}

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

l=['a','b','c','d','e']

print(l[1:4])==>['b', 'c', 'd']

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

3.3长度

l=['a','b','c','d','e']

print(len(l))==>5

3.4成员运算in和not in

l=['a','b','c','d','e']

print('a' in l)

3.5追加与insert

insert() 函数用于将指定对象插入列表的指定位置

l=['a','b','c','d','e']

l.append('xxx')

l.append('yyy')

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

l.insert(0,'xxxx')

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

3.6删除

l=['a','bbb','c','d','e']

del是一种通用的删除操作,没有返回值

del l[0]

print()==>['bbb', 'c', 'd', 'e']

dic={'k1':1}

del dic['k1']

print(dic)==>{}

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

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

l=['a','bbb','c','d','e']

l.pop(-1)

res=l.pop(1)

print(1)==>['a', 'c', 'd']

print(res)==>bbb

3.7循环

l=['a','b','c','d','e']

for item in l:

  print(item)

==>

a
b
c
d
e

l =['aaa','bb',345]

l.clear()

l,append([1,2,3])

for i in [1,2,3]:

  l.append(i)

l.extend([1,2,3])

l.extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。

l.reverse() 函数用于反向列表中元素。

l.sort() 用于对原列表进行排序,只有在类中所有元素都是同种类型的情况下才能用sort排序

总结:列表可以存多个值

   列表是有序的

   列表是可变类型

原文地址:https://www.cnblogs.com/zhengyuli/p/10579634.html