python怎么循环输入
python中的循环语句,可使用for循环,或者while循环。两者的区别是,for循环要求提前知道要循环的次数,而while循环只要满足条件就会循环,直到条件不符合,跳出循环。下面是while循环的详解
其基本形式为:
while判断条件:
执行语句……
执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假false时,循环结束。
while语句时还有另外两个重要的命令continue,break来跳过循环,continue用于跳过该次循环,break则是用于退出循环。
此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
#continue和break用法
i=1
whilei<10:
i+=1
ifi%2>0:#非双数时跳过输出
continue
printi#输出双数2、4、6、8、10
i=1
while1:#循环条件为1必定成立
printi#输出1~10
i+=1
ifi>10:#当i大于10时跳出循环
break
以上内容为大家介绍了Python培训之怎么循环输入,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
pythonpython对大小写敏感吗?
python对大小写是敏感的,也就是说,python中是区分大小写的。Python的变量名是区分大小写的,例如:name和Name就是两个变量名,而非相同变量。...详情>>
2023-11-09 23:57:19
python单引号怎么打
实际上在Python中'...'和"..."是完全一样的,但不能出现'..."和"...'这种情况。而将其混合使用会有很多意想不到的效果:具体规...详情>>
2023-11-09 22:41:18
pythonpop什么意思
python中的pop:pop()函数的作用就是用来移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。语法pop()方法语法:list.pop(obj=list...详情>>
2023-11-09 20:54:42
python怎么循环输入
python中的循环语句,可使用for循环,或者while循环。两者的区别是,for循环要求提前知道要循环的次数,而while循环只要满足条件就会循环,直到...详情>>
2023-11-09 16:48:15