17-Python3 循环语句
2018-11-21 18:23:56
print('pass语句')
for letter in 'Runoob':
if letter=='o':
pass
else:
print(letter)
score = int(input('请输入您的分数:'))
if score >=90:
print('A')
if score >=80 and score<90:
print('B')
if score<80:
print('C')
'以上两端代码的效果相等'
if score>=90:
print('A')
elif score>80:
print('B')
else:
print('C')
print('计算1到100的和***************************************************************')
sum = 0
n = 1
while n <= 100:
sum = sum + n
n = n + 1
print('1到100的和为:',sum)
print('while无线循环***************************************************************')
var = 1
while var == 1:
num = int(input('请输入一个整数:'))
print('您输入的数字为:',num)
print('while循环使用else语句***************************************************************')
flag = 1
while (flag):print('欢迎访问菜鸟教程!')
print('while循环使用else语句***************************************************************')
num =0
while num<5:
print('您输入的数小于5',num)
num = num +1
else:
print('您输入的数大于5',num)
print('使用for遍历列表中的数据***************************************************************')
sites = ['Google','Baidu','Taobao','Runoob']
for mysite in sites:
print('在呢:',mysite)
print('在列表中找到循环对象后停止***************************************************************')
'如果未找到循环对象提示未找到循环对象,如果已找到循环对象提示已找到循环对象'
sites = ['Google','Baidu','Runoob','Taobao']
for i in sites:
if i =='Runoob':
print('菜鸟教程:',i)
break
else:
print('不是菜鸟教程哦:',i)
print('用for遍历range()生成的一个数列*******************************************************')
for i in range(5):
print(i)
print('用for遍历range()生成的一个指定区间的数列**************************************************')
for ii in range(5,10):
print(ii)
print('用for遍历range()生成的一个指定区间、步长的数列*********************************************')
for iii in range(5,10,2):
print(iii)
print('用for结合range()和len()遍历一个序列的索引***************************************************')
sites2 = ['Google','Baidu','Runoob','Taobao']
i = 0
for i in range(len(sites2)):
print(i,sites2[i])
i = i+1
else:
print('遍历结束')
print('使用range()创建一个列表*********************************************************************')
newlist = list(range(5))
print(newlist)
print('break语句跳出for循环体***********************************************************************')
'找到一个字符中的某个字母时跳出循环体'
for letter in 'Runoob':
if letter == 'b':
print('找到了;',letter)
break
else:
print('这不是目标:',letter)
print('break语句跳出while循环体*********************************************************************')
'从10开始倒序输出,遇到7时跳出循环体'
i =10
while i <=10:
if i ==7:
print('找到了:',i)
break
else:
print('这不是我要的:',i)
i = i-1
print('continue跳过for循环块中的剩余语句************************************************************')
'遇到Runoob中的o时跳过不输出'
for letter in 'Runoob':
if letter == 'o':
continue
print('我是被忽略的:',letter)
else:
print('我不是被忽略的:',letter)
print('continue跳过while循环块中的剩余语句********************************************************888')
'*遇到10、9、、、0中的7时跳过不输出'
i = 10
while i > 0:
i = i - 1
if i ==7:
continue
print('我是被忽略的:',i)
else:
print('我不是被忽略的:',i)
'''
循环语句可以有 else 子句,它在穷尽列表(以for循环)或条件变为 false (以while循环)导致循环终止时被执行,但循环被break终止时不执行。
如下实例用于查询质数的循环例子:
'''
print('pass是空语句,一般用作占位语句************************************************************')
while True:
pass
print('end的应用及使用效果对比*******************************************************************')
for i in range(5):
print(i)
for i in range(5):
print(i,end='')
for i in range(5):
print(i,end=' ')
for i in range(5):
print(i,end=',')
17-Python3 循环语句的更多相关文章
- Python3 循环语句
Python3 循环语句 转来的 很适合小白 感谢作者 Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中wh ...
- 【python】Python3 循环语句
[python]几种常见的循环 注意:如果涉及到程序中print语句中含有%d,%s,那么要在脚本最开始写语句:#coding=utf-8,才能够正常输出想要的数字或者字符串. Python3 循环语 ...
- Python3循环语句
Python3 循环语句 Python中的循环语句有for和while. 循环语句控制结构图如下: 一.while循环 ①循环结构 while 判断条件: 执行语句 实例: n = int(input ...
- python013 Python3 循环语句
Python3 循环语句本章节将为大家介绍Python循环语句的使用.Python中的循环语句有 for 和 while.Python循环语句的控制结构图如下所示: while 循环Python中wh ...
- python3循环语句while
Python的循环语句有for和while语句,这里讲while语句. Python中while语句的一般形式: while 条件判断 : 语句 需要注意冒号和缩进.另外,注意Python中没有do. ...
- Python3 循环语句(十)
Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中while语句的一般形式: while 判断条件: 语句 同样需要注意 ...
- (四)Python3 循环语句——for
for循环的一般格式如下: for <variable> in <sequence>: <statements> else: <statements> ...
- (三)Python3 循环语句——while
while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进.另外,在 Python 中没有 do..while 循环. 以下实例使用了 while 来计算 1 到 100 的总和 ...
- 17.for循环语句
for循环: 语法: for(表达式1;表达式2;表达式3){ java语句; } 表达式1是最初始化表达式:最先执行,只执行一次 表达式2必须是boolean 类型的表达式.结果为ture或者fal ...
- Python系列:三、流程控制循环语句--技术流ken
Python条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非 ...
随机推荐
- nginx 根据端口不同实现负载均衡
upstream www.abc.com { server www.mynginx.com:91; server www.mynginx.com:92; }server { listen 80; se ...
- python 写入CSV出现空白行问题及拓展
最近在学习python,要求让出表格.期间在不懂得情况下,写了些代码,运行后发现存在输入写入猴行之间存在空白行.猴发现原来问题在打开文件的方式不对. 现将我的学习交流经验分享如下: 1,自己的起初代码 ...
- Could not write file: C:\......\.classpath
最近因为换操作系统,把项目从Mac系统copy到了win10下,出现了不少项目部署启动上的问题.最开始的一个问题是:Could not write file: C:\......\.classpath ...
- LeetCode 112 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- SQL Server 2012 安装过程详解(包含每一步设置的含义)
转http://www.cnblogs.com/EastLiRoar/p/4051969.html 一.启动安装程序,点击“安装”选项卡,选择“全新SQL Server独立安装或向现有安装添加功能”. ...
- CH 1602 - The XOR Largest Pair - [字典树变形]
题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...
- nginx内置变量 大全
nginx内置变量 内置变量存放在 ngx_http_core_module 模块中,变量的命名方式和apache 服务器变量是一致的.总而言之,这些变量代表着客户端请求头的内容,例如$http_u ...
- wpf(Application 如何创建一个事件,及其Application相关的属性)
1.如何关闭wpf程序.应用程序的关闭只有调用其shutdown方法才可以.shutdown有三种属性.OnLastWindowClose,OnMainWindowClose,OnExplicitSh ...
- 加载properties文件的三种方法
源代码: package a.one; import java.io.FileInputStream; import java.io.InputStream; import java.util.Pro ...
- oracle执行计划相关
执行计划相关 根据Operation缩进来判断,缩进最多的最先执行:(缩进相同时,最上面的最先执行) 同一级如果某个动作没有子ID就最先执行 同一级的动作执行时遵循最上最右先执行的原则 TABLE A ...