python练习题


#a b 值交换
'''多行注释
a = 3
b = 4
print "a 和 b 的值交换"
print "a is "+ str(a) +" b is " + str(b)
a = a + b
b = a - b
a = a - b
print "a is "+ str(a) +" b is " + str(b)
'''
#乘法表 练习循环使用,print换行输出
'''
print "乘法表"
for i in range(1,10):
    for j in range(1,i+1):
        print str(j) + "*" + str(i) + "=" + str(i*j),
    print " "
'''
#经典小程序练习
#1、请编写application,计算并输出“1+2+……+2008”的结果。
'''
sum = 0
for i in range(1,2008):
    sum = sum + i
print "1+2+3...+2008 = " + str(sum)
'''
#2、请编写application,要求输出1000以内的所有素数。
'''
num = 1000
print "1000以内的素数有:"
for i in range(2,num+1):
    a = 2
    for j in range(3,i/2+1):
        a = i % j
        if a==0:
            break
    if a!=0:
        print str(i) + ","
'''
#3、int a[]={2、4、6、1、3、7、5},要求对其进行非降序排列。
'''
num = [6,8,5,1,2,3,9,4,7]
num.sort()
print (num)
'''
#4、将辗转相除法求了两个整数的最大公因数gcd(a,b)用递归方法实现,设计下列方法:
'''
a = 16
b = 8
yue = 1
bei = 1
if a > b:
    a = a + b
    b = a - b
    a = a - b
for i in range(2,b+1):
    if a % i == 0 and b % i == 0:
        yue = i
print "最大公约数:" + str(yue)
num = range(a,a*b)
num2 = num[::-1]
for i in num2:
    if i % a == 0 and i % b == 0:
        bei = i
print "最小公倍数:" + str(bei)
'''
#5、求满足1+2+……+n<5678 的最大n值(application).
'''
sum = 0
n = 0
while True:
    sum = sum + n
    n = n + 1
    if sum > 5678:
        print "n的值是:" + str(n-2)
        break
'''
#6、请编写程序,由程序的参数给定一个字符串,然后由程序统计并输出在该字符串中每个字符出现的次数。
'''
a = "abaabg"
num = 0
for i in a:
    for j in a:
        if i == j:
            num = num + 1
    print str(i) + "的数量是:" + str(num)
    num = 0
'''
#tuple test
def word_divide(word_tuple):
    res = {}
    for word in word_tuple:
        try:
            print "try1 :" + str(word[0])
            res[word[0]].append(word)
            #print "try2 :" + str(word)
        except:
            res[word[0]] = []
            res[word[0]].append(word)
    return res.values()

word = ('ab','cb','append','clone','cake')
print word[0]
print word_divide(word)

你可能感兴趣的:(python练习题)