Pythton3实例

计算1-100之和

#add.py
n = 0
sum = 0
for n in range(0,101):
    sum += n
print(sum)

实现99乘法法则

#mul.py
i = 1
while i <= 9:
    j = 1
    while j <= i:
        mut = j*i
        print("%d * %d = %d"%(j,i,mut),end=" ")
        j += 1
    print(" ")
    i += 1

运算结果:

robot@ubuntu:~/wangqinghe/python/20190827$ python3 mul.py

1 * 1 = 1 

1 * 2 = 2 2 * 2 = 4 

1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 

1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 

1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 

1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 

1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 

1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 

1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81

 

冒泡排序ptyhon版:

#bubble.py
def bubble(li):
    temp = 0
    for i in range(len(li) - 1):
        for j in range(len(li) - 1 - i):
            if li[j] > li[j + 1]:
                temp = li[j]
                li[j] = li[j + 1]
                li[j + 1] = temp
            else:    
                temp = li[j+1]
    print(li)

bubble([123,345,12,27,48,846,3458,239,9342])

 

运行结果:

robot@ubuntu:~/wangqinghe/python$ python3.5 bubble.py

[12, 27, 48, 123, 239, 345, 846, 3458, 9342]

 

选择排序:

#select.py
a=[1,5,4,2,2,31,12,7,4]
b = list(set(a))
c = []
for j in b:
    for i in a:
        if i == j:
            c.append(i)
print(c)

a = [1,23,457,234,68,234,47,925,345]
print("primary a : ",a)
a.sort()
print(a)

运行结果:

robot@ubuntu:~/wangqinghe/python/20190827$ python3.5 select.py

[1, 2, 2, 4, 4, 5, 7, 12, 31]

primary a :  [1, 23, 457, 234, 68, 234, 47, 925, 345]

[1, 23, 47, 68, 234, 234, 345, 457, 925]

posted @ 2019-08-27 18:09  王清河  阅读(198)  评论(0编辑  收藏  举报