【课程12】循环嵌套和算法

【循环镶嵌】

# '''相亲约会'''
# boys = ['boys1','boys2','boys3']
# girls = ['girls1','girls2','girls3']
# for boy in boys:
# for girl in girls:
# print('%s shakes %s'% (boy,girl))
# '''九九乘法表'''
# for i in range(1,10):
# for j in range(1,i+1):
# print('{}X{}={} '.format(j,i,i*j), end=' ')
# print()

【列表生成式】

beforetax = [10000,15000,8000,4000,5000]
'''method-1'''
# aftertax = [int(one*0.9) for one in beforetax]
'''method-2'''
# aftertax = []
# for one in beforetax:
# aftertax.append(int(one*0.9))
'''加判断符号'''
aftertax = [int(one*0.9) for one in beforetax if one>=10000]
print(aftertax)

【排序算法】

'''升序降序'''#sort
alist = [8,2,6,0]
# alist.sort()#升序
# alist.sort(reverse=True)#降序
# print(alist)

'''冒泡'''
def mysort(inlist):
for i in range(len(inlist)-1):
for j in range(0,len(inlist)-1-i):
if inlist[j]>inlist[j+1]:
inlist[j],inlist[j+1] = inlist[j+1],inlist[j]
return inlist
print(mysort(alist))

 

posted @ 2019-01-17 13:22  万骨枯  阅读(287)  评论(0编辑  收藏  举报