# Python 100道基础入门练习题（附答案）

370 篇文章 114 订阅

### 实例001：数字组合

1 num=0
2 for a in range(1,5):
3     for b in range(1,5):
4         for c in range(1,5):
5             if((a!=b)and(a!=c)and(b!=c)):
6               print(a,b,c)
7               num+=1
8 print (num)


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


1 profit=int(input('Show me the money: '))
2 bonus=0
3 thresholds=[100000,100000,200000,200000,400000]
4 rates=[0.1,0.075,0.05,0.03,0.015,0.01]
5 for i in range(len(thresholds)):
6     if profit<=thresholds[i]:
7         bonus+=profit*rates[i]
8         profit=0
9         break
10     else:
11         bonus+=thresholds[i]*rates[i]
12         profit-=thresholds[i]
13 bonus+=profit*rates[-1]
14 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)
print('digit: ',num)
print('alpha: ',alp)
print('other: ',oth)


### 实例018：复读机相加

a=input('被加数字：')
n=int(input('加几次？：'))
res=0
for i in range(n):
res+=int(a)
a+=a[0]
print('结果是：',res)


### 实例019：完数

def factor(num):
target=int(num)
res=set()
for i in range(1,num):
if num%i==0:
return res

for i in range(2,1001):
if i==sum(factor(i))-i:
print(i)


### 实例020：高空抛物

high=200.
total=100
for i in range(10):
high/=2
total+=high
print(high/2)
print('总长：',total)


• 163
点赞
• 1088
收藏
觉得还不错? 一键收藏
• 28
评论
06-08
01-30 792
05-10 5456
08-03 5164
10-20 4万+
10-29
03-05
07-06

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

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

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