《Python语言程序设计》课件-第二章(中英文课件)_第1页
《Python语言程序设计》课件-第二章(中英文课件)_第2页
《Python语言程序设计》课件-第二章(中英文课件)_第3页
《Python语言程序设计》课件-第二章(中英文课件)_第4页
《Python语言程序设计》课件-第二章(中英文课件)_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Python语言程序设计【while循环与循环的中止】PythonLanguageProgramming[Whileloopandlooptermination]知识点【while循环】while循环基本格式程序流程图代码结构while条件表达式:#表达式为真则执行语句1到语句n语句1语句2……语句n其他语句KnowledgePoints[WhileLoop]BasicformatofwhileloopprogramflowchartscodestructureWhileconditionalexpression:

#Iftheexpressionistrue,executestatements1ton

Statement1

Statement2……

Statementnotherstatements知识点【while循环】【例】计算1到100的总和1.确定循环次数,利用循环变量的方式代码结构sum=0

i=1

whilei<=100:

sum=sum+i

i+=1

print("1到100之和为:%d"%(sum))[Example]Calculatethesumof1to1001.Determinethenumberofcycles,utilizingcyclicvariablescodestructuresum=0

i=1

whilei<=100:

sum=sum+i

i+=1

Print("Thesumof1to100is:%d"%

(sum))KnowledgePoints[WhileLoop]知识点【while循环】【例】根据输入的数字,求出它的所有因子。2.循环次数不定,直至表达式为‘0’或‘Flase’代码结构j=2

i=eval(input("请输入一个整数:"))

answer="它的所有因子为:"

whilei>j:

ifi%j==0:

answer+=str(j)+","

j+=1

print(answer)[Example]Basedontheinputnumber,findallthefactorsofit.2.Thenumberofcyclesisuncertainuntiltheexpressionis'0'or'Flase'codestructureKnowledgePoints[WhileLoop]j=2

i=eval(input("Pleaseenteraninteger:"))

answer="Allitsfactorsare:"

whilei>j:

ifi%j==0:

answer+=str(j)+","

j+=1

print(answer)知识点【while循环】【例】用while语句在屏幕中打印出数字0-92.循环次数不定,直至表达式为‘0’或‘Flase’代码结构i=0

whilei<10:

print(i)

i=i+1#i的值不断递增,从而确保循环能够终止。[Example]Printthenumbers0-9onthescreenwiththewhilestatementcodestructurei=0

whilei<10:

print(i)

i=i+1#iisincreasingtoensurethattheloopcanbeterminated.KnowledgePoints[WhileLoop]2.Thenumberofcyclesisuncertainuntiltheexpressionis'0'or'Flase'知识点【while循环】【例】无限循环3.通过设置条件表达式永远不为false来实现无限循环

代码结构while1==1:#表达式永远成立

num=int(input("输入一个数字:"))

print("你输入的数字是:",num)

print("再见!")[Example]Infiniteloop3.Realizeinfiniteloopbysettingtheconditionalexpressiontoneverbefalsecodestructurewhile1==1:#Theexpressionisalwaystrue

num=int(input("Enteranumber:"))

print("Thenumberyouenteredis:",,num)

print("Goodbye!")KnowledgePoints[WhileLoop]知识点【while循环】【例】while…else条件语句4.while循环使用else语句代码结构count=0

whilecount<5:

print(count,"小于5")

count=count+1

else:

print(count,"大于或等于5")[Example]While.elseconditionalstatement4.WhileloopusingelsestatementcodestructureKnowledgePoints[WhileLoop]count=0

whilecount<5:

print(count,"lessthan5")

count=count+1

else:

print(count,"greaterthanorequalto5")知识点【while循环】【例】简单语句组5.简单语句组代码结构while1==1:print('HelloDerisweng!')如果while循环体中只有一条语句,可以将该语句与while写在同一行中。[Example]Groupofsimplestatements5.Groupsofsimplestatementscodestructurewhile1==1:print('HelloDerisweng!')Ifthereisonlyonestatementinthebodyofthewhileloop,thestatementcanbewrittenonthesamelineaswhile.KnowledgePoints[WhileLoop]知识点【循环的中止】【例】求20以内的所有质数(素数)1.利用break中止循环的流程代码结构i=2

whilei<21:#表示求质数的范围是从2-21

j=2#对于每个i因子,都从2开始计算

whilej<i/2:#如果在2-i/2的范围内有把i整除的数,则i不是质数

ifi%j==0:

break

#若已经整除就没有必要测试其他因子,终止循环

j+=1

ifj>=i/2:

#如果关于j的循环都已经进行完毕,说明在2-i/2的范围内无因子,i是质数

print(i,'是质数')

i+=1Example:Findallprimenumbersupto20.1.Usebreaktoabortthecycleprocesscodestructurei=2

whilei<21:#indicatesthattherangeofprimenumberisfrom2-21

j=2#Foreachifactor,startfrom2

whilej<i/2:#Ifthereisanumberwithintherangeof2-i/2thatdividesi,iisnotaprimenumber

ifi%j==0:

break#Ifithasbeendivided,itisunnecessarytotestotherfactorsandterminatetheloop

j+=1

ifj>=i/2:

#Ifthecycleaboutjhasbeencompleted,itmeansthatthereisnofactorintherangeof2-i/2,andiisaprimenumber

Print(i,'isaprimenumber')

i+=1KnowledgePoints[Abortingthecycle]知识点【循环的中止】1.break语句可以从循环语句的循环体内跳出循环。2.continue语句仅仅是从循环语句的循环体内跳到下一个迭代中。1.Thebreakstatementcanjumpoutoftheloopfromtheloopbodyoftheloopstatement.2.Thecontinuestatementonlyjumpsfromtheloopbodyoftheloopstatementtothenextiteration.KnowledgePoints[Abortingthecycle]Python语言程序设计【常用运算】PythonLanguageProgramming[CommonOperations]Python语言支持的运算符知识点【常用运算】算术运算符、关系运算符逻辑运算符、赋值运算符位运算符、成员运算符、身份运算符OperatorssupportedbyPythonlanguageKnowledgePoints[CommonOperations]arithmeticoperators,relationaloperatorslogicaloperators,assignmentoperatorsbitwiseoperators,membershipoperators,identityoperators知识点【常用运算】1.算术运算符运算符描述+两个对象相加-得到负数或是两个数相减*两个数相乘或是返回一个被重复若干次的字符串/两个数相除%取模:返回除法的余数**幂:x**y,返回x的y次幂//取整:返回商的整数部分123a,b=26,7

c=a//b

print("c=a//b,c=",c)【例】取整//3输出结果:1.thearithmeticoperatorsoperatorsDescription+Twoobjectsaddedtogether-togetanegativenumberortosubtracttwonumbers*Multiplytwonumbersorreturnastringrepeatedseveraltimes/Dividingtwonumbers%modulo:returnstheremainderofthedivision**Power:x**y,returnxtotheypower//rounding:returnstheintegerpartofthequotient123a,b=26,7

c=a//b

print("c=a//b,c=",c)[Example]Rounding//3Outputresult:KnowledgePoints[CommonOperations]知识点【常用运算】2.关系运算符运算符描述==等于!=不等于>

大于<

小于>=大于等于<=小于等于12x,y=35,10

print(x!=y)【例】不等于!=True输出结果:2.RelationaloperatorsoperatorsDescription==Equivalent!=Notequalto>

Greaterthan<

Lessthan>=greaterthanorequalto<=lessthanorequalto12x,y=35,10

print(x!=y)[Example]Notequalto!=TrueKnowledgePoints[CommonOperations]Outputresult:知识点【常用运算】3.逻辑运算符运算符描述and布尔"与"or布尔"或"not布尔"非"1234x,y=35,10

print(xandy)

print(xory)

print(notx)【例】逻辑运算符1035False输出结果:3.LogicaloperatorsoperatorsDescriptionandBoolean"vs."orBoolean"or"notBoolean"non"1234x,y=35,10

print(xandy)

print(xory)

print(notx)[Example]Thelogicaloperator1035FalseKnowledgePoints[CommonOperations]Outputresult:知识点【常用运算】4.赋值运算符运算符描述=x=y将y赋值给x+=x+=y等效于x=x+y-=x-=y等效于x=x-y*=x*=y等效于x=x*y/=x/=y等效于x=x/y%=x%=y等效于x=x%y**=x**=y等效于x=x**y//=x//=y等效于x=x//y123x,y=10,3

x/=y

print(x)【例】赋值运算/=3.3333333333333335输出结果:4.theassignmentoperatoroperatorsDescription=X=yassignsytox+=X+=yisequivalenttox=x+y-=X-=yisequivalenttox=x-y*=X*=yisequivalenttox=x*y/=X/=yisequivalenttox=x/y%=X%=yisequivalenttox=x%y**=X**=yisequivalenttox=x**y//=X//=yisequivalenttox=x//y123x,y=10,3

x/=y

print(x)[Example]Assignmentoperation/=3.3333333333333335KnowledgePoints[CommonOperations]Outputresult:知识点【常用运算】5.位运算符运算符描述&按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0|按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。^按位异或运算符:当两对应的二进位相异时,结果为1~按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1。<<

左移动运算符:运算数的各二进位全部左移若干位,由"<<"右边的数指定移动的位数,高位丢弃,低位补0。>>

右移动运算符:把">>"左边的运算数的各二进位全部右移若干位,">>"右边的数指定移动的位数1234a=9#9=00001001

b=3#3=00000011

c=0

c=a&b#1=00000001【例】按位与5.bitwiseoperatorsoperatorsDescription&Bybitwiththeoperator:thetwovaluesinvolvedintheoperation,ifthetwocorrespondingbitsare1,theresultofthebitis1,otherwise0|Bitwiseoroperator:Wheneveroneofthetwocorrespondingbinarybitsisone,theresultbitisone.^Bitwisedifferentoroperator:whenthetwocorrespondingbinarybitsaredifferent,theresultis1~Bitwiseinverseoperator:invertseachbinarybitofdata,i.e.,changes1to0and0to1.<<

Leftshiftoperator:allbinarybitsoftheoperatorareshiftedleftbyanumberofbits,thenumbertotherightof"<<"specifiesthenumberofbitstobeshifted,thehigherbitsarediscardedandthelowerbitsarezeroed.>>

Rightshiftoperator:shiftsallbinarydigitsoftheoperatortotheleftof">>"totherightbyacertainnumberofdigits,thenumbertotherightof">>"specifiesthenumberofdigitstobeshifted,andthenumbertotherightof">>"specifiesthenumberofdigitstobeshifted1234a=9#9=00001001

b=3#3=00000011

c=0

c=a&b#1=00000001[Example]BybitwithKnowledgePoints[CommonOperations]知识点【常用运算】6.成员运算符运算符描述in如果在指定的序列中找到值返回True,否则返回False。notin如果在指定的序列中没有找到值返回True,否则返回False。FalseTrue输出结果:1234a=6

clist=[1,2,3,4,5]

print(ainclist)

print(anotinclist)【例】成员运算6.MembershipoperatorsoperatorsDescriptioninIfavalueisfoundinthespecifiedsequence,itreturnsTrue;otherwise,itreturnsFalse.notinIfnovalueisfoundinthespecifiedsequence,trueisreturned;otherwise,falseisreturned.FalseTrue1234a=6

clist=[1,2,3,4,5]

print(ainclist)

print(anotinclist)[Example]ThemembershipoperationKnowledgePoints[CommonOperations]Outputresult:知识点【常用运算】7.身份运算符运算符描述isis是判断两个标识符是不是引用自一个对象isnotisnot是判断两个标识符是不是引用自不同对象FalseTrueFalseTrue输出结果:1234567a,b=6,10

print(aisb)

print(aisnotb)

print(id(a)==id(b))

#id方法的返回值就是对象的内存地址

b=6

print(id(a)==id(b))【例】身份运算7.identityoperatorsoperatorsDescriptionisIsistodeterminewhethertwoidentifiersarereferencedfromanobjectisnotIsnottojudgewhethertwoidentifiersarereferencedfromdifferentobjectsFalseTrueFalseTrue1234567a,b=6,10

print(aisb)

print(aisnotb)

print(id(a)==id(b))

#Thereturnv

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论