1、int、bool
bool (True False)
bit_length() #数字类型转化成二进制最小位数
i = 100 print(i.bit_length()) #7
2、int、bool、str之间的转换
#int ---> str
i = 10
s = str(i)
#str ---> int
s = '123' i = int(s)
#int ---> bool 只要是0 --》False 非0 --》Ture
i = 3 b = bool(i) print(b) #True
#bool ---> int #True 1 #False 0
#ps: while True: pass while 1: #效率高 pass
#str --> bool #非空字符串都是True #空字符串都是False
3、字符串str
(1)字符串的索引与切片
s = 'ABCDLSESRF'
s = 'ABCDLSESRF' s1 = s[0] print(s1) s2 = s[3] print(s2) s3 = s[-1] print(s3) s4 = s[-2] print(s4)
#ABCD 切片:顾头不顾尾 s5 = s[0:4] print(s5) s6 = s[0:-1] print(s6) s7 = s[0:] s8 = s[:] print(s7,s8) s9 = s[0:0] print(s9) s = 'ABCDLSESRF' # s[首:尾:步长] s10 = s[0:5:2] print(s10) s11 = s[4:0:-1] print(s11) s12 = s[3::-1] print(s12) s13 = s[-1::-1] print(s13) s14 = s[::-1] print(s14)
(2)字符串的的操作
【1】 capitalize() #首字母大写
s = 'alasdanWAFsa' s1 = s.capitalize() #首字母大写 print(s1)
title() #每个隔开(特殊字符或数字)的单词首字母大写
s = 'alxs +eafa -gkgf'
s4 = s.title() #每个隔开(特殊字符或数字)的单词首字母大写
print(s4)
【2】 upper() lower()
s = 'alasdanWAFsa'
s2 = s.upper() #全大写 s3 = s.lower() #全小写 print(s2,s3)
#ps: s_str = 'acEQ1' you_input = input('请输入验证码,不区分大小写') if s_str.upper() == you_input.upper(): print('输入成功') else: print('请重新输入')
【3】 swapcase() #大小写翻转
s3 = s.swapcase() #大小写翻转 print(s3)
【4】 find 通过元素找索引,找不到返回-1
index 通过元素找索引,找不到报错
s = 'alasWAFsa' s8 = s.find('a') s81 = s.index('a') print(s81,type(s8))
【5】 center(width(位宽),‘填充的字符‘’(不填默认空格))
s = 'alasdanWAFsa' s5 = s.center(20,'-') #居中,空白填充 print(s5)
【6】 expandtabs(tabsize(不填默认8个字符)) #返回 s 的副本,其中所有制表符都使用空格展开。 如果没有给出 tabsize,则假定选项卡大小为8个字符。
s = 'alasdanWA\tFsa' s6 = s.expandtabs() print(s6) #alasdanWA Fsa
【7】 len() #测长度 (公共方法)
s = 'alaa.a/.,爱神的箭' l = len(s) print(l) #13
【8】 startswith() endwith() #判断是否以什么开头/结尾
s = 'alasdanWAFsa' s7 = s.startswith('al') s71 = s.startswith('a',2,5) print(s71)
【9】strip() #默认去除字符串前后空格
rstrip() #删右侧 lstrip() #删左侧
s = ' alasWAFsa ' s9 = s.strip() print(s9) s = ' a*dalasWAFsa% ' s91 = s.strip(' %*') #从两端同时删除,若第一个不包含在删除的字符中,就跳过 print(s91) #a*dalasWAFsa
username = input('请输入名字:').strip() if username == '春哥': print('恭喜发财')
【10】count() #记数
s = ' alasWAFsa ' s10 =s.count('al') print(s10) #1
【11】split() #分割 , str ---> list
s = 'alas WAF sa ' l = s.split('W') print(l) #['alas ', 'AF sa ']
【12】fomat() #格式化输出
s = '我叫{},今年{},爱好{},再说一下我叫{}'.format('太白',21,'girl','太白') print(s) s = '我叫{0},今年{1},爱好{2},再说一下我叫{0}'.format('太白',21,'girl') print(s) name = input('请输入名字:') s = '我叫{name},今年{age},爱好{hobby},再说一下我叫{name}'.format(age=21,name=name,hobby='girl') print(s)
【13】replace() #替换
s = '卡拉积分凯撒奖发卡机了福克斯剪发卡了' s11 = s.replace('卡拉积分','加速度',1) #replaace(‘要换的内容’,‘换成的内容’,替换的次数) print(s11)
ps:字符串的查询,直接用for循环遍历
s = 'afasfaf' for i in s: print(i) s = 'salfka黄色askdas' if '黄色' in s: print('您的评论有敏感词...')
4、作业总结
(1)逻辑运算
#1、判断T,F print(1>1 or 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #T print(not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #F print(1>2 and 3<4 or 4>5 and 2>1 or 9<8 and 4>6 or 3<2) #F #2、求下列逻辑语句的值 print(8 or 3 and 4 or 2 and 0 or 9 and 7) #8 print(0 or 2 and 3 and 4 or 6 and 0 or 3) #4 print(5 and 9 or 10 and 2 or 3 and 5 or 4 or 5) #9 #3、求下列结果 print(6 or 2>1) #6 print(3 or 2>1) #3 print(0 or 5<4) #0 ? F print(5<4 or 3) #T ? 3 print(2>1 or 6) #T print(3 and 2>1) #1 ? T print(0 and 3>1) #0 print(2>1 and 3) #T ? 3 print(3>1 and 0) #F 0 print(3>1 and 2 or 2<3 and 3 and 4 or 3>2) #T 2
总结:
or: 首先看x,x为真返回x,x是数值就返回该数值,x是逻辑运算则返回T;x为假看y,y为数值则返回该数值,y为逻辑运算,则判断该逻辑为真或假,返回相应的T,F;
and:首先看x,x为真看y,y为数值则返回该数值,y为逻辑运算,则判断该逻辑为真或假,返回相应的T,F;x为假则返回x,x是数值就返回该数值,x是逻辑运算则返回F;
(2)计算
count = 1 sum = 0 while count < 100: if count == 88: count += 1 continue elif count % 2 == 1: sum = sum + count else: sum = sum - count count += 1 print(sum)
i = 0 sum = 0 j = -1 while i < 100: i += 1 if i == 88: continue else: j = -j sum = sum + i*j print(sum)
(3)用户登录(三次)输错机会,且每次输入错误是显示错误次数(提示:使用字符串格式化)
i = 0 j = 0 name = 'xiaowang' word = '123' while i < 3 : i += 1 j = 3 - i username = input('请输入用户名:') password = input('请输入密码:') if username == name and password == word: print('登陆成功') break else: print('登录失败') if j !=0: msg = '剩余错误次数:%d' %(j) print(msg) else: print("账户已锁定")
i = 0 j = 0 username = "xiaoyang" password = "123456" while i < 3: name = input("请输入你的用户名:") word = input("请输入你的密码:") i += 1 j = 3 - i if name == username and word == password: print("登录成功,请稍后......") msg = ''' username = %s password = %s ''' %(username, str(password)) print(msg) break else: if name == username: if word == password: print("登录成功,请稍后......") msg =''' username = %s password = %s '''%(username,str(password)) print(msg) break else: if j != 0: print("登录失败,你的密码错误!请重新输入") print('你还剩余%s次机会' %(str(j))) else: print('你的账户已锁定') break continue else: if word == password: if j != 0: print("登录失败,你的用户名错误!请重新输入") print('你还剩余%s次机会' %(str(j))) else: print('你的账户已锁定') break continue else: if j != 0: print("登录失败,你的用户名和密码错误!请重新输入") print('你还剩余%s次机会' %(str(j))) else: print('你的账户已锁定') break