学习python,学习方法挺重要的。自己也看了很多书和网课,对比之下,推荐mooc上的北大陈斌老师的《python语言基础与应用》这门课。下面是这门课里的几个好用的小程序:
1、归并排序(代码贴上去行首缩进就自动取消了)
'''
import random
def merge_sort(data_list):
if len(data_list) <= 1:
return data_list
middle = int(len(data_list) / 2)
left = merge_sort(data_list[:middle])
right = merge_sort(data_list[middle:])
merged = []
while left and right:
merged.append(left.pop(0) if left[0] <=right[0] else right.pop(0))
merged.extend(right if right else left)
return merged
data_list = [random.randint(1,100) for _ in range(50)]
print(merge_sort(data_list))
'''
2、打印三角形
'''
n = int(input())
for i in range(n):
line = " "(n-1-i) + "@"(2*i +1)
print(line)
'''
3、输入某年某月某日,判断这一天是这一年的第多少天
'''
import datetime
dtstr = input('Enter the datetime:(20201028):')
dt = datetime.datetime.strptime(dtstr,"%Y%m%d")
another_dtstr = dtstr[:4] + '0101'
another_dt = datetime.datetime.strptime(another_dtstr, "%Y%m%d")
print(int((dt - another_dt).days)+1)
'''