1. while

  终止while循环:
    (1) 改变条件,使其不成立
    (2) break

  应用实例1:计算1+2+3+...+100

#1.使用两个变量
count = 1
sum = 0
while count <= 100:
sum = sum + count
count = count + 1
print(sum) #
#2.使用三个变量
count = 1
sum = 0
flag = True
while flag:
sum = sum + count
count = count + 1
if count > 100:
flag = False
print(sum)

  应用实例2:打印1-100

#1.使用两个变量
count = 1
flag = True
while flag:
print(count)
count = count + 1
if count > 100:
flag = False #2.使用一个变量
count = 0
while count <= 100:
print(count)
count = count + 1

2. continue

  结束本次循环,继续下一次循环

#一直循环输出1
count = 1
while count < 20:
print(count)
continue
count = count + 1  

3. break

print('')
while True:
print('')
print(333)
break
print(444)
print('abc') 

  应用实例:打印1-100

count = 1
while True:
print(count)
count = count + 1
if count > 100:break

4. while-else

  (1)while循环被break打断,则不执行else
  (2)while循环未被break打断,则执行else

count = 0
while count <= 5:
count += 1
if count == 3:
break #(1)不执行else,如下左图
#pass #(2)执行else,如下右图
print('loop',count)
else:
print('循环正常执行结束')

5. 逻辑运算符or_and_not

(1)x or y

  若x为真,返回x(0为假,其余为真);否则返回y

print(1 or 2)   #
print(1 or 5) #
print(0 or 1) #
print(0 or 2) #

(2)x and y(结果与or相反)

   若x为真,返回y(0为假,其余为真);否则返回x

print(1 and 2)   #
print(1 and 5) #
print(0 and 1) #
print(0 and 2) #

(3)not x

  若x为真,返回True;否则返回False

print(not 0)    #True
print(not 1) #False
print(not 56) #False

Python——while、continue、break、while-else、or、and、not的更多相关文章

  1. python学习:continue及break使用

    continue及break使用 #continue 作用:结束本次循环,继续下次循环#break 作用:跳出整个当次循环 for i in range(10): if i < 5: conti ...

  2. 【Python 学习】continue ,break 的使用

    # continue 跳出本轮循环并进入下一次循环# break 终止当前循环,跳出循环体 1. continue 使用案例 : for i in range(5): if i < 3: pri ...

  3. js--基础(对象、数组、函数、if语句、while语句、do while语句、continue语句、break语句)

    三.流程控制:1.单行语句var age =20;//单行语句 2.复合语句花括号包含起来的与聚集和叫做复合语句,一对花括号表示一个复合语句 ,处理时可以当成一个单行语句来看待,一般复合句与叫做代码块 ...

  4. 自学Linux Shell12.7-控制循环break、continue命令

    点击返回 自学Linux命令行与Shell脚本之路 12.7-控制循环break.continue命令 break命令.break命令用于跳出循环,使用break可以跳出任何类型的循环:for.whi ...

  5. break、continue、exit、return的区别和对比

    break.continue.exit.return的区别和对比 一:说明 break.continue在条件循环语句及循环语句(for.while.if等)中用于控制程序的走向:而exit则用于种植 ...

  6. if-elif-else分支判断语句(附加continue和break)---举例说明

    一.分支循环语句: a=input("请输入一个五位数字") if(len(a)!=5): print("输入的数字不合格"); elif(a[0::]==a[ ...

  7. A Byte of Python 笔记(4)控制流:if、for、while、break、continue

    第6章  控制流 3种控制流语句-- if  for  while 默认pyhon使用ASCII码来解释程序的,默认不支持中文,需要在程序的第一行或者第二行声明编码.官方参考具体参考以下三种方式:1. ...

  8. python 语句:条件、循环、break、continue...

    1. 条件语句 执行条件:判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围. [Python程序语言指定任何非0和非空(null)值为true,0 或 ...

  9. python(3)-- 语句:条件、循环、break、continue...

    1. 条件语句 执行条件:判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围. [Python程序语言指定任何非0和非空(null)值为true,0 或 ...

  10. python中break、continue 、exit() 、pass终止循环的区别

    python中break.continue .exit() .pass区分 1.break:跳出循环,不再执行 Python break语句,就像在C语言中,打破了最小封闭for或while循环. b ...

随机推荐

  1. 为什么需要超出48K的音频采样率,以及PCM到DSD的演进

    网上很多观点说,根据采样定理,48K的音频采样率即可无损的表示音频模拟信号(人耳最多可以听到20K的音频),为何还需要96K, 192K等更高的采样率呢?最先我也有这样的疑问,毕竟采样定理是经过数学家 ...

  2. windows下使用selenium报错selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH

    问题 :执行程序代码报错: WebDriverException:Message:'geckodriver'executable needs to be in Path 或者 selenium.com ...

  3. linux杀毒软件clamav安装与使用

    #clamav安装与使用 ###第一步:Clamav下载http://www.clamav.net/downloads wget http://www.clamav.net/downloads/pro ...

  4. echarts 折线图配置

    html内容: <div id="user_num_chart" style="width: 582px;height:250px;"></d ...

  5. 010PHP基础知识——运算符(三)

    <?php /** * 位运算符: * 1:&按位与:左右两边的数,同位都为1,返回是1,否则返回是0 */ /*$a = 5; $b = 6; $a = decbin($a);//10 ...

  6. zoj 2966 Build The Electric System(最小生成树)

    Build The Electric System Time Limit: 2 Seconds      Memory Limit: 65536 KB In last winter, there wa ...

  7. RabbitMQ(1) 核心概念

    消息中间价 消息中间价,也称消息队列,是分布式式系统中常用的中间价. 通过消息中间价传递消息,使得各个子系统解耦,异步通信. 目前业界有许多消息队列的实现,如RabbitMQ.Kafka.Active ...

  8. An error report file with more information is saved as hs_err_pid2756.log

    An error report file with more information is saved as hs_err_pid2756.log weblogic启服务时遇到的问题,重新部署都不行, ...

  9. Eclipse上安装springsource-tool-suite

    spring tool suite 是一个基于eclipseIDE开发环境中的用于开发spring应用程序的工具.提供了开箱即用的环境用于实现,调试和部署你的spring应用,包括为关键的的服务器和云 ...

  10. spring framework各个版本下载网址

    spring framework各个版本下载网址 http://repo.spring.io/simple/libs-release-local/org/springframework/spring/