Python简单小程序练习

时间:2022-11-28 20:55:59

1.九九乘法表

#!/usr/bin/python
for i in range(1,10):
for j in range(i):
j += 1
print ("%d * %d = %-2d " %(i, j, i*j)),
print ""

2.水仙花

#!/usr/bin/python
l = []
for a in range (100,1000):
if (a/100%10)**3 + (a/10%10)**3 + (a%10)**3 == a:
l.append(a) print l

3.质数

#!/usr/bin/python
l = []
for i in range (2,101):
flag = 0
for j in range (2,i-1):
if i % j == 0:
flag = 1
break
if flag == 0:
l.append(i)
print l

4.倒叙排列和冒泡排序

#!/usr/bin/python
#!coding=utf-8
list = [5,8,6,9,3,4,8,9,5,1,4]
list_len = len(list)
#倒叙排列
l_len = list_len/2
for i in range(l_len):
list[i],list[list_len-1-i] = list[list_len-1-i], list[i]
print list
#冒泡排序
for i in range(list_len - 1):
for j in range(i,list_len):
if list[i] > list[j]:
list[i],list[j] = list[j],list[i]
print list

5.完数

#!/usr/bin/python
l = []
for i in range (1,1000):
sum = 0
for j in range (1,i):
if i % j == 0:
sum += j
if sum == i:
l.append(i)
print l

 6.斐波那契数列

#!/usr/bin/python 

l = [1,1]
for i in range(9):
l.append (l[i] + l[i+1]) print l