Python 学习笔记8 循环语句 while
While循环是哟中利用条件语句,不断的执行某一段代码块,达到批量操作输出等一系列的操作,直到条件不满足或者被强制退出为止。
其工作流程如下: (图片来源菜鸟教程:http://www.runoob.com/python/python-while-loop.html )

我们来看一个例子:
current_number = 10
while current_number <= 20:
print("Current number is : " + str(current_number))
current_number += 1 print("Final number: " + str(current_number)) '''
输出:
Current number is : 10
Current number is : 11
Current number is : 12
Current number is : 13
Current number is : 14
Current number is : 15
Current number is : 16
Current number is : 17
Current number is : 18
Current number is : 19
Current number is : 20
Final number: 21
'''
我们可以看到 变量current_number 的初始值为10, 在小于等于20的情况下,不断的被打印,被增长,直至增长到21的时候,跳出了循环。
我们也可以使用标志,比如True,False 等布尔值来进行循环:
flag = True
while flag:
number = int(input("Input a number less than 10:\n"))
if number < 10:
flag = True
else:
flag = False print("The game is end...") '''
输出:
Input a number less than 10:
2
Input a number less than 10:
11
The game is end...
Final number: 21
'''
使用 break 退出循环。在while循环中,你可以通过特定的条件语句,选择终止循环,不再继续运行余下的代码。
flag = True
while flag:
city = input("Input a city which you like: \n" + "Input 'Q' to quit...\n")
if city == 'Q':
break
else:
print("You like " + city) print("Thanks for your information.") '''
输出:
Input a city which you like:
Input 'Q' to quit...
ShangHai
You like ShangHai
Input a city which you like:
Input 'Q' to quit...
HangZhou
You like HangZhou
Input a city which you like:
Input 'Q' to quit...
Q
Thanks for your information.
'''
使用 continue 不再继续执行余下的代码块,返回到循环开头,继续下一轮循环。
number = 0
while number <= 10:
if number % 2 == 0:
print("The number is even: " + str(number))
number += 1
continue
print("Next...")
else:
number += 1 '''
输出:
The number is even: 0
The number is even: 2
The number is even: 4
The number is even: 6
The number is even: 8
The number is even: 10
'''
循环也可以和else 一起使用:
number = 0
while number <= 10:
if number % 2 == 0:
print("The number is even: " + str(number))
number += 1
continue
print("Next...")
else:
number += 1
else:
print("The number is equal or more than 10, stop loop.") '''
输出:
The number is even: 0
The number is even: 2
The number is even: 4
The number is even: 6
The number is even: 8
The number is even: 10
The number is equal or more than 10, stop loop.
'''
使用while 操作列表 List
peoples = ['Ralf', 'Clark', 'Leon']
while peoples:
people = peoples.pop()
print(people) '''
输出:
Leon
Clark
Ralf
'''
peoples = ['Ralf', 'Clark', 'Leon', 'Terry']
while 'Terry' in peoples:
peoples.remove('Terry')
print(peoples) '''
输出:
['Ralf', 'Clark', 'Leon']
'''
while循环语句可以解决程序中需要重复执行的操作。其循环执行的次数由循环条件确定,当循环条件满足时,重复执行某程序段,直到循环条件不成立为止。反复执行的程序段称为循环体,循环条件必须要在循环体中改变,否则可能会出现无限循环的结果
Python 学习笔记8 循环语句 while的更多相关文章
- python学习笔记:循环语句——while、for
python中有两种循环,while和for,两种循环的区别是,while循环之前,先判断一次,如果满足条件的话,再循环,for循环的时候必须有一个可迭代的对象,才能循环,比如说得有一个数组.循环里面 ...
- Python 学习笔记9 循环语句 For in
For in 循环主要适用于遍历一个对象中的所有元素.我们可以使用它遍历列表,元组和字典等等. 其主要的流程如下:(图片来源于: https://www.yiibai.com/python/pytho ...
- 【Python学习笔记】循环和迭代
for和while基本语法 break和continue else的使用 enumerate和zip在循环中的应用 for和while基本语法 Python中的的循环使用for和while语句来实现, ...
- python学习笔记-基础、语句、编码、迭代器
#python的优缺点优点:Python简单优雅,尽量写容易看明白的代码,尽量写少的代码.缺点:第一个缺点就是运行速度慢,和C程序相比非常慢,因为Python是解释型语言,你的代码在执行时会一行一行地 ...
- python学习:利用循环语句完善输入设置
利用循环语句完善输入设置 使用for循环: 代码1:_user = "alex"_password = "abc123" for i in range(3): ...
- python学习笔记--for循环
推荐一个学习语言的网站:http://www.codecademy.com 有教程,可以边学边写,蛮不错的. for循环: 1.for loops allow us to iterate throug ...
- python学习笔记四——循环及冒泡排序
3.3.3 break 和 continue语句 break:跳出整个循环 continue:跳出当前循环继续后面的循环 例: x=int(input("please input the ' ...
- python 学习笔记(循环,print的几种写法,操作符)
一.循环( for, while) while循环是指在给定的条件成立时(true),执行循环体,否则退出循环.for循环是指重复执行语句. break 在需要时终止for /while循环 cont ...
- 【Python学习笔记】with语句与上下文管理器
with语句 上下文管理器 contextlib模块 参考引用 with语句 with语句时在Python2.6中出现的新语句.在Python2.6以前,要正确的处理涉及到异常的资源管理时,需要使用t ...
随机推荐
- PYthon3:简单几步实现冒泡排序
1.概念理解: 冒泡排序:可以简单的理解为是列表中相近的元素,两两比较,小的在前面.最多需要len()-1次排序. 2.例子:a=[11,7,4,56,35,0] 3.代码实现: 4.输出结果: 第1 ...
- openwrt MT7628 编译前更改为DHCP,root 密码、ssid、时区、主机名
一.设置为DHCP动态获取ip地址 在:/home/OpenWrt/openwrt_CC_mt76xx_zhuotk_source/ 目录下,新建文件名/files/etc/config. 将配置好的 ...
- C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈
一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...
- Gradle: Download 下载慢的解决办法
应该是gradle被墙了,在网上查得:使用阿里云的国内镜像仓库地址,就可以快速的下载需要的文件: 修改项目根目录下的文件 build.gradle : buildscript { repositori ...
- ionic 3 常见报错及解决办法
用ionic 3开发也有一段时间了,现在总结下开发中遇到的报错,以及解决办法: ERROR DOMException: Failed to execute 'setAttribute' on 'Ele ...
- Yii2中mongodb使用ActiveRecord的数据操作
概况 Yii2 一个高效安全的高性能PHP框架.mongodb 一个高性能分布式文档存储NOSQL数据库. 关于mongodb与mysql的优缺点,应该都了解过. mysql传统关系数据库,安全稳定 ...
- LNMP一键安装包添加虚拟主机、删除虚拟主机及如何使用伪静态
本文主要介绍LNMP一键安装包添加虚拟主机.删除虚拟主机及如何使用伪静态. 一.添加虚拟主机通俗点就是在VPS/服务商上添加一个网站(域名). 需要执行如下命令:/root/vhost.sh 执行后会 ...
- 初识Python,简单初学代码
第一个自己手写的代码~ If 与 Elif #!/usr/bin/env python # - * - coding:uft8 - * - Inp = input ( '请输入你的会员级别' ) if ...
- Class--2019-04-14
获取class对象,有三种方法: 1.通过类名.class直接访问 Class c = Integer.class; 2.通过Class.forName(类名)函数获取 Class c = Class ...
- Yarn集群的搭建、Yarn的架构和WordCount程序在集群提交方式
一.Yarn集群概述及搭建 1.Mapreduce程序运行在多台机器的集群上,而且在运行是要使用很多maptask和reducertask,这个过程中需要一个自动化任务调度平台来调度任务,分配资源,这 ...