# 快速入门（完整）：Python实例100个（基于最新Python3.7版本）

## Python3 100例

http://www.runoob.com/python/python-100-examples.html
git地址：
https://github.com/RichardFu123/Python100Cases

https://blog.csdn.net/weixin_41084236/article/details/81564963

• 总的来说，并没有什么难度，全部搂了一遍也只花了不到2天时间，其中还有一半以上的时间是用来写文档的。
• 用来练练手还是凑合的。

#### 文章目录

##### 实例001：数字组合

total=0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if ((i!=j)and(j!=k)and(k!=i)):
print(i,j,k)
total+=1
print(total)


import itertools
sum2=0
a=[1,2,3,4]
for i in itertools.permutations(a,3):
print(i)
sum2+=1
print(sum2)


##### 实例002：“个税计算”

profit=int(input('Show me the money: '))
bonus=0
thresholds=[100000,100000,200000,200000,400000]
rates=[0.1,0.075,0.05,0.03,0.015,0.01]
for i in range(len(thresholds)):
if profit<=thresholds[i]:
bonus+=profit*rates[i]
profit=0
break
else:
bonus+=thresholds[i]*rates[i]
profit-=thresholds[i]
bonus+=profit*rates[-1]
print(bonus)

##### 实例003：完全平方数

n=0
while (n+1)**2-n*n<=168:
n+=1

print(n+1)
----------

85



n=0
while (n+1)**2-n*n<=168:
n+=1

for i in range((n+1)**2):
if i**0.5==int(i**0.5) and (i+168)**0.5==int((i+168)**0.5):
print(i-100)

##### 实例004：这天第几天

def isLeapYear(y):
return (y%400==0 or (y%4==0 and y%100!=0))
DofM=[0,31,28,31,30,31,30,31,31,30,31,30]
res=0
year=int(input('Year:'))
month=int(input('Month:'))
day=int(input('day:'))
if isLeapYear(year):
DofM[2]+=1
for i in range(month):
res+=DofM[i]
print(res+day)


##### 实例005：三数排序

raw=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw.append(x)

for i in range(len(raw)):
for j in range(i,len(raw)):
if raw[i]>raw[j]:
raw[i],raw[j]=raw[j],raw[i]
print(raw)

raw2=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw2.append(x)
print(sorted(raw2))


##### 实例006：斐波那契数列

# 递归实现
def Fib(n):
return 1 if n<=2 else Fib(n-1)+Fib(n-2)
print(Fib(int(input())))

# 朴素实现
target=int(input())
res=0
a,b=1,1
for i in range(target-1):
a,b=b,a+b
print(a)


##### 实例007：copy

import copy
a = [1,2,3,4,['a','b']]

b = a					# 赋值
c = a[:]				# 浅拷贝
d = copy.copy(a)		# 浅拷贝
e = copy.deepcopy(a)	# 深拷贝

a.append(5)
a[4].append('c')

print('a=',a)
print('b=',b)
print('c=',c)
print('d=',d)
print('e=',e)

============ RESTART: F:\PyWorkspace\Python100\100examples\007.py ============
a= [1, 2, 3, 4, ['a', 'b', 'c'], 5]
b= [1, 2, 3, 4, ['a', 'b', 'c'], 5]
c= [1, 2, 3, 4, ['a', 'b', 'c']]
d= [1, 2, 3, 4, ['a', 'b', 'c']]
e= [1, 2, 3, 4, ['a', 'b']]

##### 实例008：九九乘法表

for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%2ld '%(i,j,i*j),end='')
print()

##### 实例009：暂停一秒输出

import time
for i in range(4):
print(str(int(time.time()))[-2:])
time.sleep(1)

##### 实例010：给人看的时间

import time

for i in range(4):
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
time.sleep(1)

##### 实例011：养兔子

month=int(input('繁殖几个月？： '))
month_1=1
month_2=0
month_3=0
month_elder=0
for i in range(month):
month_1,month_2,month_3,month_elder=month_elder+month_3,month_1,month_2,month_elder+month_3
print('第%d个月共'%(i+1),month_1+month_2+month_3+month_elder,'对兔子')
print('其中1月兔：',month_1)
print('其中2月兔：',month_2)
print('其中3月兔：',month_3)
print('其中成年兔：',month_elder)

##### 实例012：100到200的素数

import math
for i in range(100,200):
flag=0
for j in range(2,round(math.sqrt(i))+1):
if i%j==0:
flag=1
break
if flag:
continue
print(i)

print('\nSimplify the code with "else"\n')

for i in range(100,200):
for j in range(2,round(math.sqrt(i))+1):
if i%j==0:
break
else:
print(i)

##### 实例013：所有水仙花数

for i in range(100,1000):
s=str(i)
one=int(s[-1])
ten=int(s[-2])
hun=int(s[-3])
if i == one**3+ten**3+hun**3:
print(i)

##### 实例014：分解质因数

target=int(input('输入一个整数：'))
print(target,'= ',end='')

if target<0:
target=abs(target)
print('-1*',end='')

flag=0
if target<=1:
print(target)
flag=1

while True:
if flag:
break
for i in range(2,int(target+1)):
if target%i==0:
print("%d"%i,end='')
if target==i:
flag=1
break
print('*',end='')
target/=i
break


##### 实例015：分数归档

points=int(input('输入分数：'))
if points>=90:
elif points<60:
else:

##### 实例016：输出日期

import datetime
print(datetime.date.today())
print(datetime.date(2333,2,3))
print(datetime.date.today().strftime('%d/%m/%Y'))
day=datetime.date(1111,2,3)
day=day.replace(year=day.year+22)
print(day)

##### 实例017：字符串构成

string=input("输入字符串：")
alp=0
num=0
spa=0
oth=0
for i in range(len(string)):
if string[i].isspace():
spa+=1
elif string[i].isdigit():
num+=1
elif string[i].isalpha():
alp+=1
else:
oth+=1
print('space: ',spa
• 1059
点赞
• 7540
收藏
觉得还不错? 一键收藏
• 83
评论
11-13 7088
07-05
06-07 643
08-04
12-20 4845
02-08 906
10-22 9496
07-11 1930
09-16
03-09
09-19
07-02 1971
10-03 3930

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。