Python-for循环(个人笔记)

一般形式

for  in :
    
else:
    

循环list

languages = ["C", "C++", "Perl", "Python"] 
for x in languages:
    print (x)

range()形式

>>>for i in range(5):
...     print(i)

若指定步长和区间

>>>for i in range(0, 10, 3) :
    print(i)

range()与len()结合循环list(类似c循环情况)

>>>a = ['Google', 'Baidu', 'Runoob', 'Taobao', 'QQ']
>>> for i in range(len(a)):
...     print(i, a[i])

冒泡排序

# python 冒泡排序

def paixu(li) :
    max = 0
    for ad in range(len(li) - 1):
        for x in range(len(li) - 1 - ad):
            if li[x] > li[x + 1]:
                max = li[x]
                li[x] = li[x + 1]
                li[x + 1] = max
            else:
                max = li[x + 1]
    print(li)

你可能感兴趣的:(Python-for循环(个人笔记))