for语句格式:for 变量名 in 集合: 语句逻辑:按顺序取“集合”中的每个元素,赋值给“变量”,再去执行语句,如此循环往复,直达取完“集合”中的元素截止 for i in [1, 2, 3, 4, 5]: print(i) range([start,] end [,step]) # 函数 列表生成器start 默认为0 step 默认为1 功能:生成数列 a = range(10)print(a)for x in range(10): print(x)for y in range(2, 20, 3): #3表示步长 print(y) # 同时遍历下标和元素 for index , m in enumerate([1, 2, 3, 4, 5]): #index, m = 下标, 元素 print(index, m) # enumerate 枚举器sum = 0for i in range(1,101): sum = sum +iprint(sum)
二、while循环
while 语句 格式: while 表达式: 语句 逻辑:表达式为真,执行“语句” num = 1while num <= 5: print("num = %d ",%(num)) num += 1 例1# 计算1+2+3.。。。。+100 sum = 1 num = 1 while num <= 100: sum = sum + num num +=1print("sum = %d"% (sum)) 例2# 打印每一个字符 str = " sunck is a good man " index = 0while index < len(str): print("str[%d] = %s" % (index, str[index])) index += 1 例3#打印出所有三位数中的水仙花数 num = 99 while num<999: a = num % 10 b = (int(num /10)) % 10 c = int(num / 100) d = a**3 + b**3 +c**3 if d == num : print ("num = %d" %(num)) num += 1 例4#告诉我五位数中有多少个回文数 num1 = 10000 i = 0 while num1<100000: a = num1//10000 # 求高位数 b = num1 % 10 # 求低位数 if a == b: c = num1 // 1000 % 10 d = num1 //10 %10 if c == d: i += 1 num1 += 1 print ("回文数个数是:%d 个"%(i)) 例6#从控制台输入一个数,判断是否为质素 num = int(input("请输入一个数:")) a = 1 i = 0 while a <= num: b = num /a if b == num // a: i += 1 a += 1 if i > 2: print("%d不是质数%(num)") break if i == 2: print("%d是质数"%(num)) 例7#从控制台输入一个数,分解质因数 num = int(input("输入一个数")) i = 2 while num != 1: if num % i == 0: print(i) num //= i else : i += 1 例8#从控制台输入一个字符串,返回这个字符串中有多少个单词 str = input()#lasdf***aasf aasge age a g easr str1 = str.strip() index = 0 count = 0 while index < len(str1): while str1[indx] != " ": index += 1 if index == len(str1): break count += 1 if index == len(str1): break while str1[index] == " ": index += 1print(count) 例9#从控制台输入一个字符串,打印出这个字符串中所有数字字符的和 str = input("请输入一串字符串") index = 0 sum = 0 while index < len(str): if str[index ] >= "0" and str[index] <= "9": sum += int(str[index]) index += 1print("sum = %d"%(sum)) # 字符串比较大小 # 从第一个字符开始比较,谁的ASCII值大谁就大,如果相等回比较下一个 # 字符的ASCII值大小,那么谁的值大谁就大
三、死循环
# 死循环:表达式永远为真的循环 while 1: print("sunck is a good man !")
四、else语句
while 表达式: 语句1 else: 语句2 # 逻辑:在条件语句(表达式)为False时执行else中的语句 a =1 while a<= 3: print ("sunck is a good man!") a += 1 else : print("very very good")print("you are right")
五、循环中断语句
break语句: 作用:跳出 for 和 while 循环 注意:只能跳出距离他最近的那一层循环 for i in range(10): print(i) if i == 5: #结束循环 break num = 1 while num <= 10: print(num) if num == 3: break num += 1 else: print(num) 注意:循环语句可以有else语句,break导致循环结束,不会执行else语句 continue 语句作用:跳过当前循环中的剩余语句,然后继续下次循环 注意:跳过距离最近的循环,然后进入下次循环 for i in range(10): print(i) if i == 3: continue print("*") print("&") num = 0 while num < 10: print(num) if num == 3: num += 1 continue num += 1 print("*") print("&")
六、实例
#小甲鱼import random secret = random.randint(1,10)print("----------小甲鱼---------") tempt = input ("please input a number :") guess = int (tempt)while guess != secret: tempt = input("从新输入数字:") guess = int(tempt) if guess == secret: print("你是小甲鱼心中的蛔虫吗?") print("哼,猜中了也没有奖励") else : if guess > secret: print("哥,大了,大了") else : print("嘿 ,小了,小了")print("不玩了,不玩了")#小甲鱼print("欢迎进入此游戏") guess = input("please input a number") guess = int(guess)if (guess >= 6): print("大了") guess = input("please input a number once again") guess = int(guess)if (guess <=4): print("小了") guess = input("please input a number once again") guess = int(guess)while True : if guess == 5: print("哇哦,你厉害,猜对了") print("不玩了") break else : print("游戏没意思,不玩了") breakdef MyFirstFunction(): print("欢迎进入游戏") number = input("input a number") number = int (number) while number > 0: print ("恭喜您,中奖了") number= number-1MyFirstFunction() list=[1,2,3,4,5,5]for i in list: print(i)# 判断一个三位数是否为水仙花数num = int (input(" 请输入一个三位数")) a = num % 10 b = num //10 %10 c = num // 100 if num == pow (a,3)+pow(b,3)+pow(c,3): print ("yes") else : print ("no")# 判断是否为回文数num = int(input ("请输入一个五位数")) a = num % 10 #求低位数,用求余数 b = num //10000 #求高位用整除 if a!=b: print("no")#比较三个数中的最大值num1 = int(input()) num2 = int(input()) num3 = int(input()) max =num1 if max < num2: max = num2 else: if max < num3: max = num3 print(max) #判断一个年份是否为闰年year = int (input("请输入一个年份:")) if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0): print("是闰年") else : print ("不是闰年") # 找出一组数中第二大的数listNum = [] num = 0while num < 5: val = int(input()) listNum.append(val) num += 1print(listNum)# 升序排序listNum.sort() count = listNum.count(listNum[len(listNum) - 1]) c = 0while c < count: listNum.pop() c += 1print(listNum[len(listNum) - 1])#打印99乘法表for a in range(10): for b in range(10): c = a * b print("%d * %d = %d"%(a, b,c)) if a == b: break print("\n")#输入两个数,求着两个数的最大公约数a = int(input()) b = int (input()) min = min (a, b)import mathfor i in range(1,min): for i in range(1, min): c = a % i d = b % i if c == 0 and b ==0: d = 1 d = d * iprint(d)
原文地址:http://blog.51cto.com/13711245/2107059