# 循环中break 语句, break用于结束真个循环,可用于while语句 while True: a = input("please input a string(input q or Q to jump out):") if a == "q" or a =="Q" : print("循环结束,退出") break else: print(a) # continue 语句用于结束本次循环 employSal = 0 employNum = 0 sal_detail = [] while True: s = input("please type the employees salary:(to quit please input Q or q)") if s.upper() == "Q" : print("FINISHED, PLZ QUIT") break if float(s) < 0: continue employNum +=1 employSal += float(a) employSal.append(float) print("员工数量{0}".format(employNum)) print("录入薪资:", sal_detail) print("平均公司席子{1})".format(employSal/employNum))
# continue 语句用于结束本次循环 sal_sum = 0 employNum = 0 sal_detail = [] while True: s = input("please type the employees salary:(to quit please input Q or q)") if s.upper() == "Q": print("FINISHED, PLZ QUIT") break if float(s) < 0: continue employNum += 1 sal_detail.append(float(s)) sal_sum += float(s) print("员工数量{0}".format(employNum)) print("录入薪资:", sal_detail) print("平均工资{0}".format(sal_sum / employNum)) # else while for 循环附带一个else语句; 如果for while没有break语句结束,则会执行else子句。 salarys = [] salarySum = 0 for i in range(4): s = input("请输入{0}个员工薪资:(按Q退出)".format(i + 1)) if s.upper() == "Q": print("已经退出") break if float(s) < 0: continue salarys.append(float(s)) salarySum += float(s) else: print("已完成4名人员工资录入") print("您已经全部录入{0}名员工薪资,分别是{1},平均工资为{2}".format(i, salarys, float(salarySum / 4))) # 循环代码的优化 1 尽量减少循环内部不必要的运算 2 尽量减少内部循环嵌套 3 尽量使用局部变量 # zip() 并行迭代 多个序列 for i in [1, 2, 3]: print(i) a = ("asd", "adsasd", "123sad", "teset") b = (1, 2, 3, 4) c = ("iio", "qwe", "qqewwe") for a, b, c in zip(a, b, c): print("{0}--{1}--{2}".format(a, b, c)) for n in range(3): print("{0}--{1}--{2}".format(a[n], b[n], c[n]))
# 列表推导式[] # 表达式 [for item in 可迭代对象] 或者 [表达式 for item in 可迭代对象 if 条件判断] '''y = [x*2 for x in range(1,5)] print(y) y = [x*3 for x in range(1,5) if x%2==0] print(y) cells = [(row,col) for row in range(1,10) for col in range(11,20)] for cell in cells: print(cell,end="\t") ''' # 字典推导式 {} # [key_expression : value_expression for 表达式 in 可迭代对象] my_text = "I think the PMBOK is very good to use and guid for PM" char_count = {x: my_text.count(x) for x in my_text} #找到字符的数量 并循环输出 print(char_count) # 集合推导式 {} 没有value b = {x for x in range(1,100) if x%9==0} print(b) # 生成器推导式(),生成器是可迭代的元素 gnt = (x for x in range(1,100) if x%9==1) # print(tuple(gnt)) for x in gnt: print(x, end="\t") print("") print(tuple(gnt))
# 画圆 """ import turtle t = turtle.Pen() t.pensize(4) t.speed(1) my_colors = ("red", "blue", "yellow", "black") for i in range(10): t.penup() t.goto(0, -i * 10) # 0 -100 -200 -300 t.pendown() t.color(my_colors[i % len(my_colors)]) t.circle(10 + i * 20) turtle.done() """ # 画棋盘 import turtle x=1 t = turtle.Pen() t.pensize(1) t.speed(5) # 横线 for i in range(18): t.penup() t.goto(0, -10 * i) t.pendown() t.goto(170, -10 * i) t.penup() t.goto(180,-10 * i) t.pendown() t.write(x+i,True) t.penup() # 竖线 for i in range(18): t.penup() t.goto(i * 10, 0) t.pendown() t.goto(i * 10, -170) # t.goto(0,0) turtle.done() # 函数用法和底层分析 # 函数是可反复调用的代码块 """ def function([canshu]): "文档字符串" 结构体/若干语句 #形参和实参 def printMax(a,b): """比较大小""" if a>b: print(a,"较大值") else: print(b,"较大值") printMax(12,32) help(printMax.__doc__) #文档字符串 def test01(): print("*" * 10) print("#" * 10) print(id(test01)) print(type(test01)) test01() for i in range(1, 10): test01() # 测试返回值的用法 def add(a, b): print("计算两个数{0},{1}的和{2}".format(a, b, (a + b))) return a + b def test02(): print("12312") print("qweqwe") return c = add(23,30) test02()