循环判断条件是编程语言中一个很重要的部分,python也不例外,循环判断条件一般结合continue,return,break关键字来判断,这些关键字用法与java中基本一致
一、if判断语句
判断条件返回的结果为布尔值,在python中,布尔值为True/False,首字母必须大写,否则将出现如下异常

1>>> ls=false 2Traceback (most recent call last): 3 File "<stdin>", line 1, in <module> 4NameError: name 'false' is not defined
View Code
python中,值不为空时,判断条件为True,如

1>>> str='flag' 2>>> if str: 3... print("not empty") 4... else: 5... print("empty") 6... 7not empty 8>>> str='' 9>>> if str: 10... print("not empty") 11... else: 12... print("empty") 13... 14empty
View Code
单个判断条件
1>>> tup=('dog','cat','water','bj') 2>>> for val in tup: 3... if len(val) < 3: 4... print("the length of " + val + " is less than 3") 5... elif len(val) == 3: 6... print("the length of " + val + " is 3") 7... else: 8... print ("the length of " + val + " is more than 3") 9... 10the length of dog is 3 11the length of cat is 3 12the length of water is more than 3 13the length of bj is less than 3
多个判断条件可以使用and或者or
1>>> name="xiao" 2>>> if name != "" and len(name) > 3: 3... print ("long name") 4... else: 5... print ("short name") 6... 7long name 8 9>>> weight=100 10>>> if weight > 150 or weight < 60: 11... print ("no normal weight") 12... else: 13... print ("normal weight") 14... 15normal weight
判断某个值是否在列表中存在
1>>> ls=['car','cat','dog'] 2>>> if 'car' in ls: 3... print("yes, it is car") 4... else: 5... print("no car") 6... 7yes, it is car
while循环
1>>> i=5 2>>> while(i>1): 3... i-=1 4... print(i) 5... 64 73 82 91
for循环
略