python入门10 循环语句
两种循环:
1 for in
2 while
#coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 循环语句 """ '''for in''' #打印0-9 for i in range(10): print(i) #取出元组中不能被2整除的数据 turple = (1,13,33,54,8329) for i in turple: if(i%2 == 1): print(i) '''while''' #打印0-9 i = 0 while i < 10: print(i) i+=1 '''break跳出循环''' while True: str = input('输入字符串') print(str) goon = input('输入y继续') if goon != 'y': break '''continue跳出本次循环''' for i in range(10): if i%2 == 0: continue print(i) '''enumerate() 同时取出索引和值''' print(enumerate(turple)) for i in enumerate(turple): print(i) for i,j in enumerate(turple): print(i,j) '''else 循环正常执行完执行else语句块''' x = 1 while x < 10: print(x); x +=2 #break #未正常执行完不执行else else: print('循环正常执行完毕') x=1 #恢复初始值 print('x:',x) '''pass占位语句''' while x < 10: if x == 2: pass print(x) x += 1 '''/r 在行开头输出''' # while True: # for char in ['-','/','|','\\']: # print(char+'\r',end='')
'''n!''' n = 10 result = 1 i = 1 while i <= n: result = result * i i += 1 print(result)
'''n!之和 1!+2!+3!''' n = 10 sum = 0 for i in range(1,n+1): mul =1 #存储阶乘 for j in range(1,i+1): mul = mul * j print('%d 的阶乘: %d'%(i,mul)) sum += mul #阶乘之和 print(sum)
'''100以内最大平方数''' from math import sqrt for i in range(99,1,-1): #倒着取 #print(sqrt(i)) if sqrt(i) == int(sqrt(i)): print(i) break
'''回文数判断''' i =12393466439321 str = str(i) #转换成字符串 len = len(str) for index in range(0,len//2): #判断前半段即可 if str[index] != str[-index-1]: print(i,'不是回文数') break else: print(i,'是回文数')
'''100-999之间 各数字3次方之和等于数字本身的数字''' for i in range(100,999): if (i%10)**3 + (i//10%10)**3 + (i//100)**3 == i: print(i)
#转化为字符串,再取每个字符 for i in range(100,999): str1 = str(i) sum = 0 for char in str1: sum += int(char)**3 if sum == i : print(i)
'''100以内的素数''' for i in range(1,100): sign = 1#标志位 1是素数,0不是素数 for j in range(2,i//2+1): if i % j == 0: sign =0 break if sign == 1 : print(i,'是素数')
for i in range(2,100): for j in range(2,i//2+1): if i % j == 0: break else: print(i,'是素数') #正常结束所有循环,说明是素数
'''冒泡排序''' list = [13,3,65,2,655,54,12] for index in range(0,len(list)): for index2 in range(index+1,len(list)): if list[index] > list[index2]: list[index],list[index2] = list[index2],list[index] #print(list) print(list)
'''选择排序 每次选出剩下的里面最大或最小的放到前面''' list = [33,12,22,129,10,35] for i in range(len(list)): maxindex = i for j in range(i+1,len(list)): if(list[j] > list[maxnum]): maxindex = j list[i],list[maxindex] = list[maxindex],list[i] print(list)
'''一个数= 所有因子之和''' for i in range(1,1000): sum = 0 for j in range(1,i//2+1): if i%j == 0 : sum += j if (i == sum): print(i)
'''出租车 3公里内10元 3公里以后每0,5公里1元 等待2.5分钟加1元 超过15公里加原价50%''' length_km = 4.6 waittime_sec = 60 waittime_fee = waittime_sec//150 if waittime_sec%150==0 else waittime_sec//150+1 #等待时间按计费单位 money = waittime_fee * 1 if length_km <= 3: money += 10 elif length_km > 3: km = (length_km-3)/0.5 if (length_km-3)/0.5%1 ==0 else (length_km-3)/0.5//1+1 #超出0.5里程个数数 money += 10 + km * 1 if length_km >= 15: money *= 1.5 print(money)
'''打印空心矩形''' h = 4 w = 5 for i in range(0,h): for j in range(0,w): if i == 0 or i == h - 1 or j==0 or j==w-1: print('*', end='') else: print(' ',end='') print('\r')
当情绪低落,烦躁的时候,学技术并整理成博客能忘却所有烦恼。技术改变心境。