python中的条件判断及循环
来源:互联网 发布:php访问共享文件夹 编辑:程序博客网 时间:2024/05/18 13:44
python中的判断用的是if-elif-else结构,当然也可以使用if-else结构,具体语法结构如下:
if <条件判断1>: <执行1>elif <条件判断2>: <执行2>elif <条件判断3>: <执行3>else: <执行4>python是用缩进方式区分代码块的,‘:’表示后面的缩进是一个代码块,因此‘:’一定不能缺失。
下面看一个示例:
age = 12if age <6:print "your age is ",ageprint "child"elif age <18:print "your age is",ageprint "teenager"else:print "your age is ",ageprint "adult"if语句还可以写成
if x: print 'True'x不为0,非空时,结果为True。
python提供两种循环,一种是for……in,另一种是while循环。
for……in循环中in前面是用于暂时存储遍历元素的变量,in后面是需要遍历的集合,
python提供了range()函数,这个函数用于生成一个整数序列,例如:
下面看几个示例:
1、循环打印集合中的元素
list = ['a','b','c']for va in list:print va2、计算1到100的累加和
sum = 0for i in range(101):sum += i print sumwhile循环直接在while后面接入循环条件,每次判断条件是否满足,是否执行循环体,直到循环结束,例如:计算1到100之间奇数的和。
sum = 0n=99while n>0:sum+=nn-=2;print sum
0 0
- python中的条件判断及循环
- 【python】python中的条件判断和循环
- Python中的条件判断和循环
- Python:循环、条件判断
- python条件判断、循环
- Python学习-3:条件判断及循环结构
- python 条件判断和循环
- python 条件判断与循环
- Python(条件判断和循环)
- python条件判断与循环
- Python-条件判断和循环
- Python条件判断和循环
- python条件判断和循环
- Python中的条件判断
- python中的条件判断
- python条件及循环语句
- python中的条件和循环
- python中的条件判断语句
- 学习笔记--C++
- Codeforces Round #278 (Div. 2) A. Giga Tower(水题)
- 第13周 项目4-3 选择排序法
- 成绩处理
- SQL对时间的操作,比如在当前时间上增加减少一天,在当前的时间上增加减少一个月
- python中的条件判断及循环
- linkin大话数据结构--Google commons工具类
- 福昕pdf阅读器块选
- SSIS OLE DB 命令 使用方法
- 合并多个数组,不用array_merge()
- 链路层的网卡聚合-基于Linux bonding
- BING: Binarized Normed Gradients for Objectness Estimation at 300fps
- 七讲:什么是 APK 文件?它和 android 手机是什么关系?
- java包名前面有个红色的大叹号(A cycle was detected in the build path of project )