python笔记汇总14
基本概念
编译型:一次性将全部的代码编译成二进制文件(C++)
优点:运行效率高
缺点:开发速度慢,不能跨平台
解释型:当程序运行时,从上至下一行一行的解释成二进制
优点:开发速度快,效率高,可以跨平台
缺点:运行效率低
1.用户交换input
name=input(请输入你的名字)age=input(请输入你的年龄)print(你的名字+name,你的年龄+age)
2.流程控制语句if
If条件:(冒号作用是把条件与结果分开)
(tape或四个空格)+结果
第一种
If54:#(相当于ture)Print()
练习:多选
num=input(请输入你猜的数字:)ifnum==1(问题是字符串,条件也都要是字符串):print(一起抽烟)elif(或者)num==2:print(一起喝酒)elifnum==3:print(新开了一家,走看看)else(其他):print(你猜错了)字符串转换成数字:int(str)[前提字符串(str)必须是数字]数字转化成字符串:str(int)
注意从上到下只走最上面可通过的结果,一旦通过,结束流程
练习:
if43:print(我请你喝酒)else:print(喝什么酒)
练习:
num=input(请输入你猜的数字:)ifnum==1:print(一起抽烟)elifnum==2:print(一起喝酒)elifnum==3:print(新开了一家,走看看)else:print(你猜错了)
练习:
score=int(input(请输入分数:))ifscore:print(我擦,最高分才...)elifscore=90:print(牛逼,A)elifscore=80:print(真不错,B)elifscore=60:print(继续努力,C)elifscore=40:print(唉...D)else:print(太笨了...E)
练习:
name=input(请输入名字:)age=input(喜不喜欢喝茶:)ifname==老张哥:ifage==是:print(恭喜你,你是茶王、茶神、茶仙)else:print(不可能)else:print(还不快向老张哥学习)
3.标志位,循环语句while
While条件:
循环体
无限循环
终止循环:1.改变条件使其不成立
2.break结束流程pass,只是结束小流程
3.continue结束流程往上走(while)
练习:输入数字1到
count=1flag=Truewhileflag:print(count)count=count+1ifcount:flag=False
练习:输入数字1到
count=1whilecount=:print(count)count=count+1
练习:输入数字1+2+3......+
count=1sum=0whilecount=:sum=sum+countcount=count+1print(sum)
3.1break跳出循环
练习:break跳出while循环,不在打印while后面的语句()
print(11)whileTrue:print()print()breakprint()print(abc)
练习:输入数字1到
count=1whileTrue:print(count)count=count+1ifcount:break
练习:输入(空格)
count=0whilecount10:count+=1#count=count+1ifcount==7:print()else:print(count)
对比:输入
count=0whilecount10:count+=1#count=count+1ifcount==7:breakelse:print(count)
对比:输入7
count=0whilecount10:count+=1#count=count+1ifcount==7:breakprint(count)
3.2continue,跳出循环往下走
练习:输入
count=0whilecount10:count+=1#count=count+1ifcount==7:continue#继续执行else,如果是break,则跳出else循环else:print(count)
3.3pass
练习:输入
count=0whilecount10:count+=1#count=count+1ifcount==7:passelse:print(count)
练习:输入1-内所有奇数
#方法一
count=1whilecount:print(count)count+=2
#方法2
count=1whilecount:ifcount%2==1:#对2取余数print(count)count+=1
练习:求1-2+3-4...99所有的和
sum=0count=1whilecount:ifcount%2==0:sum=sum-countelse:sum=sum+countcount+=1print(sum)
练习:用户登录(三次机会重试)
提示:input有账户密码,while
i=0whilei3:username=input(请输入账户:)password=int(input(请输入密码:))ifusername==老张哥andpassword==:print(登录成功)else:print(登录失败,请重新登录)i+=1
提示:while循环没有被break打断,就走else,断了则不走
练习
count=0whilecount=5:count+=1ifcount==3:breakprint(Loop,count)else:print(循环正常执行完啦)print(------outofwhileloop------)
4.格式化输出
%sd,%%想在格式化的语句中打印%
练习
name=input(请输入名字)age=input(请输入年龄)height=input(请输入身高)msg=我叫%s,今年%s身高%s%(name,age,height)print(msg)
练习
name=input(请输入名字)age=input(请输入年龄)job=input(请输入工作)hobbie=input(请输入爱好)msg=---------infoof%s----------Name:%sAge:%sjob:%sHobbie:%s---------------end---------------%(name,name,age,job,hobbie)print(msg)
5.初始编码
网络传输
电脑的传输,储存实际上都是001
美国:ascii码
()8位bit==1个字节(byte)(可能)
byte==1KB
KB==1MB
mb==1GB
中文:9万多字,为了解决全球文字:万国码,unicode
1个字节可表示所有英文,特殊字符,数字等等(种可能)
期初:2个字节,16位表示一个中文
unicode一个中文用4个字节,32位
你
升级版:utf-8:一个中文用三个字节表示(24位)
gbk国内使用,一个中文用2个字节表示
1.各个编码之间的二进制,是不能互相识别的,会产生乱码。
2.文件的储存,传输,不能是unicode(只能是utf-8utf-16gbkgbasciid等)
py3:str在内存中是用unicode储存的
5.1bytes类型
s=alexs1=balexprint(s,type(s))print(s1,type(s1))
s1=alexencode编码,如何将str---bytes,()s11=s1.encode(utf-8)print(s11)
6.基本运算符
%取余数==比较相等
**幂!=比较两个是否不相等
//取整数c+=a(c=c+a)
7.数据类型:
int:用于计算
bool:truefalse,用户判断
str:liu存储少量数据,便于计算操作
list:[老张哥[1,2,3]]可储存大量的数据
元祖:(1,2,3等大量数据),但是只读,不能修改
dict:字典{name,age}关键性大量数据
集合:{1,2,3}用的少
8.逻辑运算(andornot)
#优先级()notandor
练习
print(21and14)print(21and14or23and96or24and32)
#TorTorF
#TorF
#T
#XorY,X为非零(True),则返回X(and与or规则相反)
print(1or2)#1print(0or1)#1print(0or4and3or2)#3
#int--bool非零转换成boolTrue,0转换成bool是false
print(bool(2))#trueprint(bool(-2))#trueprint(bool(0))#false
print(int(True))#1print(int(False))#0
练习:用户登录(三次输错机会)且每次输错时显示剩余错误次数(提示:字符串格式化)
username="liu"password=i=3whilei0:name=input(请输入你的账户:)i-=1ifname==username:passwd=input(请输入你的密码:)ifpasswd==password:print(登录成功)print(恭喜你登录成功欢迎用户进入username:%spassword:%s%(username,password))breakelse:ifi==0:print(您的机会已经用完,结束本次操作)print(你的密码错误,请重新输入)print(你还有+str(i)+次机会)else:print(请输入正确的用户名)ifi==0:print(您的机会已经用完,结束本次操作)print(你还有%s次机会%(i))
9.字符串操作
9.1capitalize()#首字母大写
s=alexWUsirs1=s.capitalize()print(s1)
9.2upper()#全部大写
lower()#全部小写
swapcase()#大小写调换
s2=s.upper()s21=s.lower()s3=s.print(s2,s21,s3)
练习
s_str=acEQyou_input=input(请输入验证码,不区分大小写:)ifs_str.upper()==you_input.upper():print(输入成功)else:print(请重新输入)
9.3title()#不同名字,首字母大写(空格,特殊字符或数字隔开)
s=alexexgonwusirs4=s.title()print(s4)
9.4center()#填充物
s=alexWUsirs5=s.center(20,#)print(s5)
9.5len()#计算一系列字符串多少个字符
s=ahsfjaksaWjdjsaoofl=len(s)print(l)
9.6startswith()#以什么字母开头(反馈结果为TrueorFalse)
endswith()#以什么字母结尾
s=alexWUsirs7=s.startswith(alex)print(s7)s71=s.startswith(e,2,5)#从0开始数,后面的数字是位数区间print(s71)
9.7find#通过元素找索引(所在位置),找不到返回-1
index#通过元素索引,找不到报错
s=alexWUsirs8=s.find(WU)#第一个字母所在位数后整体体现,可以切片print(s8,type(s8))
9.8strip#默认删除前后空格
rstrip从右往左删,lstrip从左往右删
s=alexWUsirs9=s.strip()print(s9)
练习
username=input(请输入名字:).strip()ifusername==老张哥:print(恭喜老张哥发财)
练习
s=*a%lexWUsir%s91=s.strip(a%*)print(s91)
#顺序无所谓,从前往后,前后一致删除,默认第一和最后一位,若连带字母删除,必须与前面一致(空格也算元素)
9.9count#数某个字母在字符串中的个数,可以数整体(al)
s=alexaawusirls10=s.count(a)print(s10)
9.10split#将字符串分割转换成列表,str---list。
如果是s.slip(+),则字符串中+前后会被拆分
s=alexwusirltaibail=s.split()print(l)
9.11format#格式化输出(练习)
s=我叫{},今年{},爱好{},再说一下我叫{}.format(太白,36,girl,太白)print(s)s=我叫{0},今年{1},爱好{2},再说一下我叫{0}.format(太白,36,girl)print(s)s=我叫{name},今年{age},爱好{hobby},再说一下我叫{name}.format(name=太白,age=36,hobby=girl)print(s)name=input(请输入你的名字)s=我叫{0},今年{1},爱好{2},再说一下我叫{0}.format(name,36,girl)#加引号是字符串,不加引号是变量print(s)name=input(请输入你的名字)s=我叫{name},今年{age},爱好{hobby},再说一下我叫{name}.format(name=name,age=36,hobby=girl)print(s)
9.12replace()#替换
s=睡觉觉安徽大睡觉市场s11=s.replace(睡觉,市场,1)#只替换第一个睡觉(old)print(s11)
9.13is系列
name=jcgh
print(name.isalnum())#字符串由字母或数字组成
print(name.isalpha())#字符串只由字母组成
print(name.isdigit())#字符串只由数字组成
9.14for有限循环
s=gjdjajfsforiins:print(i)
练习:
s=fasf苍井空sjssif苍井空ins:print(您的评论有敏感词...)鹰击长空LEO
转载请注明:http://www.sonphie.com/jbzl/14176.html