python循环&判断&断言
1.循环判断:
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2017年9月18日 ''' #________________________for循环算出质数________________________________________ for num in range(10,20): # 迭代 10 到 20 之间的数字 for i in range(2,num): # 根据因子迭代 if num%i == 0: # 确定第一个因子 j=num/i # 计算第二个因子 print ('%d 等于 %d * %d' % (num,i,j)) continue # 跳出当前循环 else: # 循环的 else 部分 print (num, '是一个质数') #_________________循环&判断____________________________________________________ for letter in 'Python': # First Example if letter == 'h': break print ('Current Letter :', letter) var = 10 # Second Example while var > 0: print ('Current variable value :', var) var = var -1 if var == 5: break print ("Good bye!") #换行接着写代码 kkk='fueifjiej' \ 'fije';chen=kkk.split();print (chen) print("\\t") print(r"\t") #.....................break跳转(跳出循环语句)...................... ''' a = 2 b = 2 sum =0 for i in range(1,10):#3表示步值(间隔值) print 'i=',i sum+=i if sum==6: print 'sum=',sum else: print'滚蛋色狼' break print 'hahahhahahahahhah' print '123123123' print '一切将以结束' ''' #.............continue跳转(跳出当前一次循环)................ a = 2 b = 2 sum =0 for i in range(1,10):#3表示步值(间隔值) print('i=',i) sum+=i if sum==6: print ('sum=',sum) else: print('滚蛋色狼') continue print ('hahahhahahahahhah') print (R'一切\t将以结束')#打印原始字符\t #________________for循环参数_______________________ for i in range(10,1,-1): #-1表示倒序开始 print(i) for a in range(1,10,2): #2表示步长值/间隔值 print(a)
2.assert使用
在开发一个程序时候,与其让它运行时崩溃,不如在它出现错误条件时就崩溃(返回错误)。这时候断言assert
就显得非常有用
#!/usr/bin/env python # -*- coding: utf-8 -*- a_str = 'this is a string' b=type(a_str) print('a_str类型为',b) #断言是否等于str(不符合就报异常) assert b== str
3.多条件if判断
a=1;b=2;c=3;d=4 a1=1;b1=2;c1=3;d1=4 #逐个比较的方式来判断 if a == a1 and b == b1 and c == c1 and d == d1: print("a=a1, b=b1, c=c1, d=d1") else: print("a, b, c, d不等于a1, b1, c1, d1") #使用元组比较判断 if (a, b, c, d) == (a1, b1, c1, d1): print("a=a1, b=b1, c=c1, d=d1") else: print("a, b, c, d不等于a1, b1, c1, d1") # 判断chen22是否在a、b、c(满足一个条件即可) if "chen22" in (a, b, c): print("True") else: print("False")
4.通过对象完成变量判断
#!/usr/bin/python # -*- coding: UTF-8 -*- class Student: def __init__(self, student_id, name, age): self.student_id = student_id self.name = name self.age = age def __eq__(self, other): if isinstance(other, Student): return (self.student_id == other.student_id and self.name == other.name and self.age == other.age) return False if __name__ == "__main__": student1 = Student("001", "Alice", 18)#参数可以写成变量 student2 = Student("001", "Alice", 18) student3 = Student("002", "Bob", 20) print(student1 == student2) # 输出 True print(student1 == student3) # 输出 False
5.多个变量通过list判断是否一致
#_______________________判断a==a1,b==b1,c==c1,d==d1_______________________________ a = ["chenwei","wei"] b = "b" c = "c" d = { "name":"点击量", "historyBack":False, "assetTypes":[ { "operation":"DISABLE", "feed_delivery_search":"DISABLED" } ], "flag":"" } a1 = ["chenwei","wei"] b1 = "b" c1 = "c" d1 = { "name":"点击量", "historyBack":False, "assetTypes":[ { "operation":"DISABLE", "feed_delivery_search":"DISABLED" } ], "flag":"" } #把变量分别放到list strings = [a, b, c, d] strings1 = [a1, b1, c1, d1] are_equal = all(x == y for x, y in zip(strings, strings1)) print("判断多个变量相等:",are_equal)
相关连接:
https://blog.csdn.net/humanking7/article/details/45950781 ..............python中assert的使用