首先废话几句,因为最近学了Python的一些基础的语法,但是发现看书看的不少但是练习做的太少。要学好一门语言最重要的是要多敲代码,在练习中是可以发现很多的问题,这些都是单纯的看书无法得到的。所以鉴于网上关于Python练习题的资源相对较少,而且有的资源只是给出了问题但是没有提供参考的代码。于是我斗胆作为一个小白整理了一系列的Python练习题,希望能给同样的志同道合的Python热爱者提供一些方便。(作为一个刚入道不久的小白,文章中难免有些糟粕,代码也难免有些麻烦或者错误的地方,希望看见的有缘人在下方的评论中可以指出来,在下感激不尽!)
Python练习题:
1.有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多 少?
1 #encoding=utf-8 2 __author__ = 'heng' 3 #利用1,2,3,4可以组成多少个三位数,并且没有重复数字 4 figure = [1,2,3,4] 5 number = 0 6 for x in figure: 7 for y in figure: 8 if x == y: 9 continue 10 else: 11 for z in figure: 12 if y == z or z == x: #注意是or不是and 13 continue 14 else: 15 number += 1 16 print 100*x + 10*y + z 17 print "the number is %s"%number
2.企业发放的奖金根据利润提成。利润(I): 低于或等于 10 万元时,奖金可提 10%; 高于 10 万元,低于 20 万元时,低于 10 万元的部分按 10%提成,高于 10 万元的部分,可提成 7.5%; 20 万到 40 万之间时,高于 20 万元的部分,可提成 5%; 40 万到 60 万之间时,高于 40 万元的部分,可提成 3%; 60 万到 100 万之间时,高于 60 万元的部分,可提成 1.5%, 高于 100 万元时, 超过 100 万元的部分按 1%提成, 从键盘输入当月利润 I,求应发放奖金总数?
1 #encoding=utf-8 2 __author__ = 'heng' 3 #题目不再累述 4 the_profit = float(raw_input("please enter the profit:")) 5 money_award = 0.0 6 if the_profit <= 10: 7 money_award = the_profit * 0.1 8 elif the_profit <= 20: 9 money_award = 10 * 0.1 + (the_profit-10)*0.075 10 elif the_profit <=40: 11 money_award = 10*0.1 + 10*0.075 + (the_profit-20)*0.05 12 elif the_profit <= 60: 13 money_award = 10*0.1 + 10*0.075 + 20 * 0.05 + (the_profit-40)*0.03 14 elif the_profit <= 100: 15 money_award = 10*0.1 + 10*0.075 + 20 * 0.05 + 20*0.03 + (the_profit-60)*0.015 16 elif the_profit > 100: 17 money_award = 10*0.1 + 10*0.075 + 20 * 0.05 + 20*0.03 + 40 * 0.015 + (the_profit - 100) * 0.01 18 print "the money award is:%s"%money_award
貌似感觉这个的代码量确实比较大但是时间复杂度还是相对比较低的
3.一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数, 请问该数是多少?
针对这个问题,我暂时只是想到了最笨最直接的方法,如果还有更加简便的方法希望大神可以在下面评论中告诉我。(感激不尽)
1 #encoding=utf-8 2 __author__ = 'heng' 3 #找出符合题目要求的数字 4 from math import sqrt 5 the_figure = 0 6 while True: 7 if sqrt(the_figure + 100) == int(sqrt(the_figure+100)): 8 if sqrt(the_figure +268) == int(sqrt(the_figure+268)): 9 print "the figure is:%s"%the_figure 10 11 the_figure += 1
4.输入某年某月某日,判断这一天是这一年的第几天?
在这里提供两种方法显示出Python中字典的优越性!(我竟然开始没有想到用字典)
(1)笨方法:
1 #encoding=utf-8 2 __author__ = 'heng' 3 #输入一个时期判断是当年的第几天 4 5 def ifleapyear(the_year): #判断是不是闰年 6 if the_year % 4 == 0 and the_year % 100 != 0 or the_year %400 == 0: 7 return True 8 else: 9 return False 10 11 #主程序 12 year = int(raw_input("please enter the year:")) 13 month = int(raw_input("please enter the month:")) 14 days = int(raw_input("please enter the days")) 15 the_number_day = 0 16 for i in range(1,month): 17 if i ==1: 18 the_number_day +=31 19 if i == 2: 20 if ifleapyear(year): 21 the_number_day += 29 22 else: 23 the_number_day += 28 24 if i ==3: 25 the_number_day += 31 26 if i==4: 27 the_number_day += 30 28 if i ==5: 29 the_number_day += 31 30 if i==6: 31 the_number_day +=30 32 if i ==7: 33 the_number_day +=31 34 if i ==8: 35 the_number_day +=31 36 if i==9: 37 the_number_day += 30 38 if i ==10: 39 the_number_day += 31 40 if i ==11: 41 the_number_day +=30 42 if i==12: 43 the_number_day +=31 44 the_number_day +=days 45 print("the number of the days is %s"%the_number_day)
(2)利用字典(竟然是如此的简单)
1 #encoding=utf-8 2 __author__ = 'heng' 3 #运用字典的优越性来解决 4 #判断是不是闰年 5 def ifleapyear(the_year): 6 if the_year % 4 == 0 and the_year % 100 != 0 or the_year %400 == 0: 7 return True 8 else: 9 return False 10 11 #主程序 12 year = int(raw_input("please enter the year:")) 13 month = int(raw_input("please enter the month:")) 14 days = int(raw_input("please enter the days")) 15 the_number_day = 0 16 the_month = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31} 17 if ifleapyear(year): 18 the_month[2] = 29 19 if month == 1: 20 pass 21 else: 22 for i in the_month.keys(): 23 if i==month: 24 break 25 the_number_day += the_month[i] 26 the_number_day +=days 27 print("the number of day is %s"%the_number_day)
字典的优越性显而易见啊!
4.输入x,y,z三个整数,将这三个整数按从大到小的顺序输出
1 #encoding=utf-8 2 __author__ = 'heng' 3 #输入x,y,z 三个数,然后按照从小到大的顺序排列出来 4 x = float(raw_input("please enter x:")) 5 y = float(raw_input("please enter y:")) 6 z = float(raw_input("please enter z:")) 7 lis = [x,y,z] 8 lis.sort() 9 print lis
5.利用Python输出一个由*组成的C
1 #encoding=utf-8 2 __author__ = 'heng' 3 #利用*输出C的图案 4 print '*'*5 5 print '*' 6 print '*' 7 print '*'*5
6.利用*输出一个矩阵
主要是想通过这个代码熟悉一下方法rjust()
1 #encoding=utf-8 2 __author__ = 'heng' 3 #利用*输出一个(30 X 20)的矩阵 4 l = '*'*30 5 print l 6 for w in range(1,19): 7 print '*' + '*'.rjust(29) 8 print l
7.输出9x9的口诀表
1 #encoding=utf-8 2 __author__ = 'heng' 3 #输出9x9的乘法口诀表 4 for x in range(1,10): 5 for y in range(1,x+1): 6 print '%sx%s=%s'%(y,x,x*y), #如果想让连续输出的print不换行就在后面加上, 7 print ''
8.计算1000的阶乘
这里给出两种经典的方法,也就是利用for循环解决和利用递归解决。
for循环解决:
1 #for循环实现1000的阶乘 2 the_end = 1 3 for i in range(2,1001): 4 the_end = the_end * i 5 print the_end
递归解决:
1 #利用递归实现1000的阶乘 2 def process(i): 3 if i == 1: #这一条一定要加上,要不然会算到负数中造成内存的溢出 4 return 1 5 else: 6 return i * process(i-1) #递归函数一定要记的返回值 7 #主程序 8 print process(1000)
两种解决分析:利用递归似乎程序会更简洁些,但是利用递归要注意防止内存的溢出。所以对与类似的问题个人感觉还是利用for循环解决更好
9.利用Python实现四大排序算法
(1)冒泡排序:
所谓冒泡排序法就是用一个数和它前面的所有数做比较,找出其中自己合适的位置
1 the_list = [8,12,45,1,2,45,3,0,5] 2 for x in range(len(the_list)): 3 for y in range(x): 4 if the_list[x] < the_list[y]: 5 the_buffer = the_list[y] 6 the_list[y] = the_list[x] 7 the_list[x] = the_buffer 8 print the_list
(2)插入排序
插入排序法,就是将相邻的两个数比较,交换之后继续和前面的数比较,满足条件就依次交换,注意不要将插入排序和冒泡排序弄混乱
1 #实现插入排序 2 #定义一个插入排序的函数 3 def the_insert(the_list): 4 for x in range(len(the_list)): 5 i = x 6 while i: 7 if the_list[i] < the_list[i-1]: 8 t = the_list[i] 9 the_list[i] = the_list[i-1] 10 the_list[i-1] = t 11 i -= 1 12 return the_list 13 14 #主函数 15 the_list = [3,2,5,1,0] 16 print the_insert(the_list)
(3)选择排序
1 #encoding=utf-8 2 __author__ = 'heng' 3 #选择排序 4 #选择排序函数 5 def the_choice(the_list): 6 for i in range(len(the_list)-1): 7 x = i 8 while x != len(the_list)-1: 9 x += 1 10 if the_list[i] >= the_list[x]: 11 t = the_list[i] 12 the_list[i] = the_list[x] 13 the_list[x] = t 14 return the_list 15 #主函数 16 the_list = [7,3,6,1,7,2,9,11,10] 17 print the_choice(the_list)
(4)快速排序法
快速排序法用Python的递归会更加的简单明了
1 #encoding=utf-8 2 __author__ = 'heng' 3 #利用递归实现快速排序法 4 #用来排序的递归函数 5 def quick_sort(L,low,high): 6 i = low 7 j = high 8 if i >= j: 9 return L 10 key = L[i] 11 while i < j: 12 while i < j and L[j] >= key: 13 j = j-1 14 L[i] = L[j] 15 while i < j and L[i] <= key: 16 i = i + 1 17 L[j] = L[i] 18 L[i] = key 19 quick_sort(L,low,i-1) 20 quick_sort(L,j+1,high) 21 return L