一:数字
int:
x=10 本质是 x=int(10)
x=int('111')
字符串转行成int类型,‘‘ ’’里面只能存一串数字(字符、浮点值都不行)
总结:
1.int是不可变类型
2.只能存一个值
3.没有有序和无序之分
可变类型:是指内存空间里id不变的前提下,变量值可以改变,就叫可变类型
不可变类型:是值内存空间里,变量值发生改变,id也跟着改变,就叫不可变类型
float:
salary=3.1 本质是salary=float(3.1)
salary='3.1' 或者 salary='3'
字符串转行成float类型,‘‘ ’’里面只能存一串数字或浮点
总结:
1.int是不可变类型
2.只能存一个值
3.没有有序和无序之分
二:字符串
str:根据索引来去
x=str(1)
x=str(【1,2,3】)
str可以把任意类型转换成字符串类型
name=("Hello World")
print(name[0]) #取第一个值
print(name[-1]) #取最后一个值
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
H
d
切片:顾头不顾尾
name="Hello World"
# print(name[0])
# print(name[-1])
print(name[0:6])
print(name[0:6:2]) (name[0:6:2]) 0-6代表:只5个字符 2:表示取值步数 默认是1
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
Hello
Hlo
print(name[::-1]) 倒着迈步,输出反转(取值范围 *:* 默认不写是从头到尾)
与str索引去值相比,切片取值可以定义范围,可以取多个值
注意:正向取同时步数也要是正
长度:len
输出字符串总的个数,如何:
name="Hello World"
print(len(name))
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
11
成员运算:in 与 no in
name="Hello World"
print("H" in "Hello World" )
print("H" not in "Hello World" )
print("H"in name )
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
True
False
True
Strip移除字符:
name='***yangzhizong** '
print(name.strip('*'))
print(name.lstrip())
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
yangzhizong**
***yangzhizong**
定义取多个值:需定义在strip(‘值1值2’)
name='***yangzhizong**'
print(name.strip('*g'))
应用环境:
password=input("输入密码:").strip()
password=int(password)
if password == "123":
print('成功')
split:切分 将字符串切成列表
split(':',1) 理解:从左到右找:切掉第一个‘:,以被切得为左右分开,分成多个字符串
name='yangzhizong:18:male:180'
l=name.split(':')
print(l)
l=name.split('y')
print(l)
l=name.split(':',1) #1个:切一次,分左右2部分,切成列表,左右2部分依然保持原列表元素
print(l)
l=name.split(':',2) #2个:切一次,分左右2部分
print(l)
被切到:之后,被:切得部分以三个字符串分开组成一个列表
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
['yangzhizong', '18', 'male', '180']
['', 'angzhizong:18:male:180']
['yangzhizong', '18:male:180']
['yangzhizong', '18', 'male:180']
name='a:1:2:ds1dsd'
l=name.split('1')
print(l)
l=name.split('1',1) #从左到右切除第一个1
print(l)
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
['a:', ':2:ds', 'dsd']
['a:', ':2:ds1dsd']
循环:for循环
isdigit:
可以判断byteshes和unicode类型,是做常用的用于判断字符是否为“数字”的方法
age=input('>>: ')
print(age.isdigit())
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
>>: aa
False
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
>>: 22
True
join:将list中单个字符串,用定义的字符链接成一个大的字符串
name=['egon','say','hello','world']
l='/'.join(name)
print(l)
print(type(l)) #输出格式是字符串
print(type(name)) #原格式是列表
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
egon/say/hello/world
<class 'str'>
<class 'list'>
name=['egon','say','hello','world']
print(''.join(name)) #默认是空格
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
egonsayhelloworld
replace: 取代 替换值 replace(原值,要替换为的值,要替换几个))
name='yangzhizong:18:male:180 yangzhizong love study'
print(name.replace('yangzhizong','mawenjie',1)) #要替换几个
print(name.replace('yangzhizong','mawenjie',2))
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
mawenjie:18:male:180 yangzhizong love study
mawenjie:18:male:180 mawenjie love study
三:列表
作用:多个装备、爱好..
按索引取值:
l=[1,2,3,4,5,6]
print(l[0:3:1])
print(l[2::-1])
print(l[::-1])
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
[1, 2, 3]
[3, 2, 1]
[6, 5, 4, 3, 2, 1]
切片:
username=['zhansan','lisi','wangwu','liming']
print(username[0:3])
print(username[0:3:2])
username[0]='ZHANGSAN' 也可以更改
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
['zhansan', 'lisi', 'wangwu']
['zhansan', 'wangwu']
['ZHANGSAN', 'lisi', 'wangwu', 'liming']
追加:
username=['zhansan','lisi','wangwu','liming']
username.append('qita')
print(username)
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
['zhansan', 'lisi', 'wangwu', 'liming', 'qita']
往制定位置添加:insert
username=['zhansan','lisi','wangwu','liming']
username.insert(0,'qita')
print(username)
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
['qita', 'zhansan', 'lisi', 'wangwu', 'liming']
删除1:del
安装索引删除
username=['zhansan','lisi','wangwu','liming']
del username[0]
print(username)
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
['lisi', 'wangwu', 'liming']
删除2:remove
安装元素的值去删除
username=['zhansan','lisi','wangwu','liming']
username.remove('zhansan')
print(username)
username是单纯的删除,不会看到你删除的值,没有返回值
username=['zhansan','lisi','wangwu','liming']
shiji=username.remove('zhansan')
print(shiji)
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
None
删除3:pop
拿走一个值是按照索引去删除的,有返回值
如果pop不指定参数默认从最后一个删
username=['zhansan','lisi','wangwu','liming']
info=username.pop(0)
print(username)
print(info)
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
['lisi', 'wangwu', 'liming']
zhansan
循环:
username=['zhansan','lisi','wangwu','liming']
for name in username:
print(name)
E:PycharmProjectsuntitledvenvScriptspython.exe E:/PycharmProjects/untitled/day5/day5课堂笔记.py
zhansan
lisi
wangwu
liming