一.数字型
1.整型 int
======================================基本使用======================================
1、用途 用来记录年龄/等级/年等整数相关
2、定义方式
age=18 # age=int(18)
3、常用操作+内置的方法
数学运算符&比较运算
======================================该类型总结====================================
存一个值
无序
不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
2.浮点型 float
======================================基本使用======================================
1、用途 用来记录升高/体重/薪资等小数相关的数字
2、定义方式
salary=3.1 # salary=float(3.1)
3、常用操作+内置的方法
数学运算符&比较运算
======================================该类型总结====================================
存一个值
无序
不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
二.字符串类型
======================================基本使用======================================
1、用途 用来记录姓名/性别/爱好等具有描述性质
2、定义方式
name=\'deng\' hobbies=\'sleep\' #name=str(\'deng\')
3、常用操作+内置的方法
优先掌握的操作:
1、按索引取值(正向取+反向取) :只能取
msg=\'hello world\'
res=msg[2]
res1=msg[0]
print(res)
print(res1)
msg[0]=\'A\'
2、切片(顾头不顾尾,步长)
msg=\'hello world\'
res=msg[0:4:1] # 0 代表起始位置 4 代表终止位置,顾头不顾尾 1 代表步长
res=msg[0:6:2] # 此时步长为2
print(res)
info=\'egon 18\'
print(info[0:4])
了解
msg=\'hello world\'
print(msg[-1:-12:-1]) #-1 -2 -3 -4
print(msg[-1::-1]) #-1 -2 -3 -4
print(msg[::-1]) #-1 -2 -3 -4
3、长度len
print(len(\'hello 我是Enosh Deng\'))
4、成员运算in和not in
msg=\'hello 我是Enosh Deng\'
print(\'Enosh Deng\' in msg)
print(\'no\' in msg)
print(\'大家好\' not in msg)
5、移除空白strip
name=\'*******wang******\'
print(name.strip(\'*\'))
msg=\' 你好 \'
res=msg.strip()
print(res)
msg=\'+-*&^%egon-=)^(#\'
print(msg.strip(\'-+*&^%(#=\'))
name_inp=input(\'please input your name:\').strip()
pwd_inp=input(\'please input your password:\').strip()
if name_inp == \'egon\' and pwd_inp == \'123\':
print(\'登陆成功\')
else:
print(\'user or password error!!\')
6、切分split
msg=\'deng:18:male\'
res=msg.split(\':\')
print(res)
bo_wen_508=\'deng|wang|cui|feng|cui\'
res=bo_wen_508.split(\'|\')
print(res)
msg=\'deng:18:male\'
print(msg.split(\':\',1))
print(msg.rsplit(\':\',1))
7、循环
msg=\'hello world\'
for item in msg:
print(item)
3.2需要掌握的操作 ****
1、strip,lstrip,rstrip
name=\'****deng****\'
print(name.strip(\'*\'))
print(name.rstrip(\'*\'))
print(name.lstrip(\'*\'))
2、lower,upper # lower 把字符串里的所有大写字母转换成小写, upper 把字符串里的所有字母转换成大写
name=\'Enosh Deng\'
print(name.lower())
print(name.upper())
3、startswith,endswith # startswith 是以什么为开头,判断真假! endswith 是以什么结尾,判断真假
msg=\'wang is dsb\'
print(msg.startswith(\'wang\'))
print(msg.endswith(\'sb\'))
4、format的三种玩法
msg=\'my name is %s my age is %s\' %(\'deng\',18)
print(msg)
msg=\'my name is {name} my age is {age}\'.format(age=18,name=\'deng\')
print(msg)
了解
msg=\'my name is {} my age is {}\'.format(\'egon\',18)
print(msg)
5、split,rsplit
info=\'egon:18:male\'
print(info.split(\':\',1))
print(info.rsplit(\':\',1))
6、join
7、replace 替换
msg=\'wang is dsb wang\'
res=msg.replace(\'wang\',\'cui\',2)
print(res)
8、isdigit:当字符串是由纯数字组成时返回True
print(\'123123\'.isdigit())
age_of_db=18
inp_age=input(\'>>: \').strip()
if inp_age.isdigit():
inp_age=int(inp_age)
print(inp_age == age_of_db)
else:
print(\'年龄必须是数字\')
3.3 了解的操作
1、find,rfind,index,rindex,count
find: 查找子字符串在大字符串中的起始位置
msg=\'kevin is kevin xxxx sb\'
print(msg.find(\'kevin\'))
print(msg.index(\'kevin\'))
print(msg.find(\'asdf\'))
print(msg.index(\'asdf\'))
print(msg.rfind(\'kevin\'))
count:统计子字符串在大字符串中出现的次数
print(\'alex kevin alex kevin kevin\'.count(\'kevin\'))
2、center,ljust,rjust,zfill
username=input(\'>>>: \').strip()
print(username.center(50,\'*\'))
print(\'egon\'.ljust(50,\'#\'))
print(\'egon\'.rjust(50,\'#\'))
print(\'egon\'.rjust(50,\'0\'))
print(\'egon\'.zfill(50))
3、captalize,swapcase,title
print(\'abdef adsfasdf\'.capitalize())
print(\'AbC\'.swapcase())
print(\'my name is egon\'.title())
4、is数字系列
print(\'123213\'.isdigit())
print(\'Ⅳ\'.isnumeric())
print(\'贰\'.isnumeric())
5、is其他
name=\'My Nmae\'
print(name.isalnum()) #字符串由字母或数字组成
print(name.isalpha()) #字符串只由字母组成
print(name.isidentifier())
print(name.islower())
print(name.isupper())
print(name.isspace())
print(name.istitle())
======================================该类型总结====================================
存一个值
有序
不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
x=\'a\'
print(id(x))
x+=\'b\'
# print(x)
print(id(x))
相关文章: