python第三天:数据类型转换

#昨日回顾:
格式化输出 % s d r
第一种1、'%s,%s' % (内容1,内容2)
第二种、dic = {'name':'老男孩','age':45}
'%(name)s,%(age)s' % dic
表示单纯的% 用%%
 
while else 语句
如果while循环被break打断,则不走else程序
asicc unicode utf-8 gbk
 
逻辑运算符: not and or
1, () > not > and > or,同一优先级,从左至右执行
2、前后都是数字,x or y,if x True,return x
 
3、基础数据类型
int:计算
 
str:'alix','1233430',存储少量数据。
 
bool: False , True
 
list:['name',True,[]...],各种数据类型的数据,大量的数据。
32位的python的限制是5368710912个元素,64位的python的限制:1152921504606846975个元素,
且列表是是有顺序的。
 
tuple:元组,表现形式:()只读列表。
 
dict:{} {'name':'老男孩',
'name_list':['李华',...]
'alex':{'age':40,
'hobby':'old_woman',}
},
存储大量数据,关系型数据。一个栈对应一个值的存储方式
 
set:集合
set{'wu sir','alex',...}
 
 
 
#int
# i = 7
# print(i.bit_length())
#十进制转换成二进制的最小有效位数
 
'''
1 0000 0001
2 0000 0010
3 0000 0011
100
'''
 
 
'''
int <--> str
str --> int int(str) 条件:字符串必须全部由数字组成
int ---> str str(int)
bool ---> int True --> 1 False --> 0
int ---> bool 非零即为True,零即为False
bool --> str str(True) str(False)
str --> bool 非空即为True ''空字符串--->False
'''
 
 
# age = int(input('>>>'))
# print(age,type(age))
 
s1 = str(123)
s2 = 123
print(s1, s2, type(s1), type(s2))
 
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
123 123 <class 'str'> <class 'int'>
 
Process finished with exit code 0
 
 
举例:
print(int(True))
print(int(False))
 
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
1
0
 
Process finished with exit code 0
 
举例:
print(bool(100))
print(bool(0))
print(bool(-23))
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
True
False
True
 
Process finished with exit code 0
 
bool --> str str(True) str(False)
print(str(True))
print(str(False))
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
True
False
 
Process finished with exit code 0
 
 
str --> bool 非空即为True
print(bool('adnf'))
print(bool(''))
print(bool('0'))
print(bool(0))
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
True
False
True
False
 
Process finished with exit code 0
 
测试验证上面关系
s1 = ' ' #''之间有空格
if s1:
print(666)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
666
 
Process finished with exit code 0
 
再换个形式测试:
s1 = '' #’‘之间没空格
if s1:
print(666)
 
测结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
 
Process finished with exit code 0
 
 
str部分:索引功能
#str 索引 切片和歩长
s = 'Python12期' #字符串的索引从左至右是0开始
s1 = s[0]
print(s1)
 
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
P
 
Process finished with exit code 0
 
索引拿出的字段还是字符串:
#str 索引 切片和歩长
s = 'Python12期sdfasdf' #字符串的索引从左至右是0开始
s1 = s[0]
s2 = s[4]
s3 = s[8]
s4 = s[-1]
 
print(s1,type((s1)))
print(s2)
print(s3)
print(s4)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
P <class 'str'>
o
f
 
Process finished with exit code 0
 
 
str的切片功能
s = 'Python12期sdfasdf' #切片顾头不顾尾
s6 = s[0:6]
s7 = s[:6]
s8 = s[2:5]
s9 = s[1:] #切片取最后一位,需省略写:后数字
s10 = s[:] #切片如果取全部,直接在:前后的数字去掉
print(s6)
print(s7)
print(s8)
print(s9)
print(s10)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
Python
Python
tho
ython12期sdfasdf
Python12期sdfasdf
 
Process finished with exit code 0
 
 
#str的歩长功能
s = 'Python12期' #取P、t、o,格式[首位:末尾:长度]
s11 = s[:5:2]
s12 = s[4::2] #取o、1、期,格式[首位:末尾:长度]
print(s11)
print(s12)
 
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
Pto
o1期
 
Process finished with exit code 0
 
 
反向切片和歩长:
s = 'Python12期' #反向取值:期到n,格式[反向起始位:反向结束位(+1):歩长值]
s13 = s[-1:-5:-1]
print(s13)
 
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
期21n
 
Process finished with exit code 0
 
 
s = 'Python12期' #反向取值:期和1,格式[反向起始位:反向结束位:-2]
s13 = s[-1:-5:-2]
print(s13)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
期1
 
Process finished with exit code 0
 
 
#字符串的常用方法:
#首字母大写:capitalize
s = 'laoNANhai'
s1 = s.capitalize()
print(s1)
#
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
Laonanhai
 
Process finished with exit code 0
 
# * center 格式:center(长度值)
#center
s = 'laoNANhai'
s2 = s.center(20)
s3 = s.center(20, "*")
print(s2)
print(s3)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
laoNANhai
*****laoNANhai******
 
Process finished with exit code 0
 
 
# *** upper 全部大写 lower 全部小写
s = 'laoNANhai'
s12 = s.upper()
s13 = s.lower()
 
print(s12)
print(s13)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
LAONANHAI
laonanhai
 
Process finished with exit code 0
 
 
#实际案例:不区分大小写验证码
code = 'AQdr'.upper()
your_code = input('请输入验证码,不区分大小写:').upper()
if your_code == code:
print('验证成功!')
else:
print('输入错误,请重新输入!')
 
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
请输入验证码,不区分大小写:aqdr
验证成功!
 
Process finished with exit code 0
 
 
#startwith和endswith
s = 'laoNANhai'
# *** startwith:以**为开头 endswith:以**为结尾
#返回bool,可以切片,切片用逗号隔开。
s20 = s.startswith('l')
s21 = s.endswith('i')
s22 = s.endswith('a')
s23 = s.startswith('lao')
s24 = s.endswith('hai')
s25 = s.startswith('N', 3, 6)
s26 = s.endswith('h', 0, -2)
print(s20)
print(s21)
print(s22)
print(s23)
print(s24)
print(s25)
print(s26)
 
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
True
True
False
True
True
True
True
 
Process finished with exit code 0
 
#swap:大小写翻转
s = 'laoNANhai'
s30 = s.swapcase()
print(s30)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
LAOnanHAI
 
Process finished with exit code 0
 
 
# * title 被非字母隔开的每个单词的首字母大写
ss = 'gdex wusri43basde*sdfritian'
s40 = ss.title()
print(s40)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
Gdex Wusri43Basde*Sdfritian
 
Process finished with exit code 0
 
 
#***通过元素找索引:find、index
#index:通过元素找索引,可切片,找不到会报错
#find:通过元素找索引,可切片,找不到返回-1
s = 'laoNANhai'
s50 = s.find('l')
s51 = s.find('a')
s52 = s.find('a', 2, ) #找第二个a
s53 = s.find('A', 2, ) #找A
print(s50)
print(s51)
print(s52)
print(s53)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
0
1
7
4
 
Process finished with exit code 0
 
 
index与find的区别:
find
s = 'sdf ;lskd12*lsd-sd'
s7 = s.find('B')
print(s7)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
-1
 
Process finished with exit code 0
 
index:
s = 'sdf ;lskd12*lsd-sd'
s8 = s.index('B')
print(s8)
测试结果:
File "D:\pythonProject\homework.py", line 124, in <module>
s8 = s.index('B')
ValueError: substring not found
 
Process finished with exit code 1
 
#*** strip:去除前后空格键、换行符,制表符
ss1 = '\talex ' # \t 表示4个空格,或者一个tab键
ss2 = ' alex\n ' # \n 表示换行符
print(ss1)
print(ss2)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
alex
alex
 
 
Process finished with exit code 0
 
strip实际案例:
username = input('请输入用户名:')
if username == '洪嘟嘟':
print('登录成功!')
else:
print('输入错误,请重新输入!')
测试结果1:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
请输入用户名:洪嘟嘟 #洪嘟嘟后无空格
登录成功!
 
Process finished with exit code 0
 
测试结果2:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
请输入用户名:洪嘟嘟 #洪嘟嘟后面加了空格
输入错误,请重新输入!
 
Process finished with exit code 0
 
如何解决输入时前后有空格的情况?通过加strip实现自动去除:
# strip实际案例:
username = input('请输入用户名:').strip()
if username == '洪嘟嘟':
print('登录成功!')
else:
print('输入错误,请重新输入!')
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
请输入用户名: 洪嘟嘟 # 洪嘟嘟 前后都加了空格也没关系
登录成功!
 
Process finished with exit code 0
 
 
# 案例2:strip去除前后的关键字
ss = 'alexssdsd'
s9 = ss.strip('a')
print(s9)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
lexssdsd
 
Process finished with exit code 0
 
# 案例3:strip去除前后的关键字
ss = 'alexssdsd'
s9 = ss.strip('l')
print(s9)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
alexssdsd #此种情况不是字符前后的,所以无法去除!
 
Process finished with exit code 0
 
# 案例4:strip去除前后的关键字
ss = 'dalexssdsd'
s9 = ss.strip('d')
print(s9)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
alexssds
 
Process finished with exit code 0
 
# 案例5:strip去除单词关键字前后的字
ss = 'dalexssdsd'
s9 = ss.strip('d')
s10 = ss.strip('da') #将d和a分别做关键字去除前后的关键字
 
print(s9)
print(s10)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
alexssds
lexssds
 
Process finished with exit code 0
 
# 案例6:lstrip去左边或rstrip去除右边的关键字
ss = 'dalexssdsd'
s9 = ss.lstrip('d')
s10 = ss.rstrip('d')
 
print(s9)
print(s10)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
alexssdsd
dalexssds
 
Process finished with exit code 0
 
#*** split:将字符串分割成列表,
# 默认是用空格隔开,相当于 str ---> list
s = 'wusir alex taibei'
st = 'wusir,alex,taibei'
st1 = 'QwusirQalexQtaibei'
s1 = s.split()
s2 = st.split(',')
s3 = st1.split('Q')
s4 = st1.split('Q',2) #最后的数字,表示分割几次
 
print(s1)
print(s2)
print(s3)
print(s4)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
['wusir', 'alex', 'taibei']
['wusir', 'alex', 'taibei']
['', 'wusir', 'alex', 'taibei']
['', 'wusir', 'alexQtaibei']
 
Process finished with exit code 0
 
 
# ***join参数
# 在某些情况下,list --->str
s = 'alex'
s11 = '+'.join(s)
print(s11)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
a+l+e+x
 
Process finished with exit code 0
 
join功能实现2:
l = ['wusir', 'alex', 'taibai']
st11 = ''.join(l) #''之间没空格
st12 = ' '.join(l) #''之间有空格
print(st11,type(st11))
print(st12,type(st12))
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
wusiralextaibai <class 'str'>
wusir alex taibai <class 'str'>
 
Process finished with exit code 0
 
 
说明:join前提是list中必须都是str
l1 = [1, 'alex', 'taibai'] #l1中第一个改成了数字后测试
st13 = ' '.join(l1)
print(st13,type(st13))
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
Traceback (most recent call last):
File "D:/pycharm/project/day03/数据类型转换.py", line 215, in <module>
st13 = ' '.join(l1)
TypeError: sequence item 0: expected str instance, int found
 
Process finished with exit code 1
 
 
 
# ****replace:替换功能
# replace
s = '小粉嫩小粉嫩sdsdfax小粉嫩'
s12 = s.replace('小粉嫩', '长沙')
s13 = s.replace('小粉嫩', '长沙', 2) #最后的数字表示替换次数
print(s12)
print(s13)
测试结果:
C:\Users\hongd\AppData\Local\Programs\Python\Python38\python.exe D:/pycharm/project/day03/数据类型转换.py
长沙长沙sdsdfax长沙
长沙长沙sdsdfax小粉嫩
 
Process finished with exit code 0
 
 
 
# 公共方法:
# len()
s = 'sdfasdfsaadfs'
# 公共方法:
# len() 总个数
print(len(s))
# count 计算某元素出现次数,可切片
c1 = s.count('a')
c2 = s.count('a', 4, -1)
print(c1)
print(c2)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
13
3
2
 
Process finished with exit code 0
 
 
# format 格式化输出三种方式
# 1,
msg = '我叫{},今年{},爱好{}'.format('太白', '20', 'girl')
print(msg)
 
#2,
msg1 = '我叫{0},今年{1},爱好{2},我依然叫{0}'.format('太白', '20', 'girl')
print(msg1)
 
#3,
msg2 = '我叫{name},今年{ago},爱好{hobby}'.format(name='太白', hobby='girl',ago='20')
print(msg2)
 
#3-2 #\表示不回车的换行,对于一行比较长的内容,可以通过次方式来换行。
msg3 = '我叫{name},今年{ago},爱好{hobby}'.\
format(name='太白', hobby='girl',ago='20')
print(msg3)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
我叫太白,今年20,爱好girl
我叫太白,今年20,爱好girl,我依然叫太白
我叫太白,今年20,爱好girl
我叫太白,今年20,爱好girl
 
Process finished with exit code 0
 
 
# isalnum\ isalpha\ isdigit
name = 'jisan233'
print(name.isalnum()) #字符串有字母或数字组成
print(name.isalpha()) #字符串只由字母组成
print(name.isdigit()) #字符串只由数字组成
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
True
False
False
 
Process finished with exit code 0
 
 
#作业:s = 'sdfasdjowskdm' 使用while分别打印每个字符内容
s = 'asdfghjkl;'
办法1:
count = 0
while count < len(s):
print(s[count])
count += 1
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
a
s
d
f
g
h
j
k
l
;
 
Process finished with exit code 0
 
#办法2: for:有限循环,而while是无限循环
# for:有限循环,而while是无限循环
# 格式:for 变量 in 可迭代对象
pass
#举例1:
s = 'asdfghjkl;'
for i in s:
print(i)
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
a
s
d
f
g
h
j
k
l
;
 
Process finished with exit code 0
 
举例2:
for i in s:
print(i + 'ing')
测试结果:
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/homework.py
aing
sing
ding
fing
ging
hing
jing
king
ling
;ing
 
Process finished with exit code 0
 
 

posted @ 2021-06-20 14:58  hongdudu  阅读(113)  评论(0)    收藏  举报