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

Python 专栏收录该内容
16 篇文章 0 订阅

num=0
for a in range(1,5):
for b in range(1,5):
for c in range(1,5):
if((a!=b)and(a!=c)and(b!=c)):
print(a,b,c)
num+=1
print (num)


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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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

## print(n+1)

85
1
2
3
4
5
6
7
8

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

for i in range((n+1)2):
if i
0.5int(i**0.5) and (i+168)**0.5int((i+168)**0.5):
print(i-100)
1
2
3
4
5
6
7

def isLeapYear(y):
return (y%4000 or (y%40 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)
1
2
3
4
5
6
7
8
9
10
11
12

‘’’

‘’’
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))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 递归实现

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)
1
2
3
4
5
6
7
8
9
10
11
12

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’]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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

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

‘’’

‘’’
import time

for i in range(4):
print(time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime(time.time())))
time.sleep(1)
1
2
3
4
5
6
7
8
9

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)
1
2
3
4
5
6
7
8
9
10
11
12

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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

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%i0:
print("%d"%i,end=’’)
if target
i:
flag=1
break
print(’*’,end=’’)
target/=i
break
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

‘’’

‘’’
points=int(input(‘输入分数：’))
if points>=90:
elif points<60:
else:
1
2
3
4
5
6
7
8
9
10
11
12

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)
1
2
3
4
5
6
7

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

a=input(‘被加数字：’)
n=int(input(‘加几次？：’))
res=0
for i in range(n):
res+=int(a)
a+=a[0]
print(‘结果是：’,res)
1
2
3
4
5
6
7

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)
1
2
3
4
5
6
7
8
9
10
11
12

‘’’

‘’’
high=200.
total=100
for i in range(10):
high/=2
total+=high
print(high/2)
print(‘总长：’,total)
1
2
3
4
5
6
7
8
9
10
11

peach=1
for i in range(9):
peach=(peach+1)*2
print(peach)
1
2
3
4

a=set([‘x’,‘y’,‘z’])
b=set([‘x’,‘y’,‘z’])
c=set([‘x’,‘y’,‘z’])
c-=set((‘x’,‘z’))
a-=set(‘x’)
for i in a:
for j in b:
for k in c:
if len(set((i,j,k)))==3:
print(‘a:%s,b:%s,c:%s’%(i,j,k))
1
2
3
4
5
6
7
8
9
10

​ *

​ *

def draw(num):
a=""(2*(4-num)+1)
print(a.center(9,’ ‘))
if num!=1:
draw(num-1)
print(a.center(9,’ '))
draw(4)
1
2
3
4
5
6
7

a = 2.0
b = 1.0
s = 0
for n in range(1,21):
s += a / b
a,b = a + b,a
print (s)
1
2
3
4
5
6
7

res=1
for i in range(20,1,-1):
res=i*res+1
print(res)
1
2
3
4

def factorial(n):
return n*factorial(n-1) if n>1 else 1
print(factorial(5))
1
2
3

def rec(string):
if len(string)!=1:
rec(string[1:])
print(string[0],end=’’)

rec(input(‘string here:’))
1
2
3
4
5
6

def age(n):
if n==1:
return 10
return 2+age(n-1)
print(age(5))
1
2
3
4
5

n=int(input(‘输入一个正整数：’))
n=str(n)
print(’%d位数’%len(n))
print(n[::-1])
1
2
3
4

n=input(“随便你输入啥啦：”)
a=0
b=len(n)-1
flag=True
while a<b:
if n[a]!=n[b]:
print(‘不是回文串’)
flag=False
break
a,b=a+1,b-1
if flag:
print(‘是回文串’)
1
2
3
4
5
6
7
8
9
10
11
12

weekT={‘h’:‘thursday’,
‘u’:‘tuesday’}
weekS={‘a’:‘saturday’,
‘u’:‘sunday’}
week={‘t’:weekT,
‘s’:weekS,
‘m’:‘monday’,
‘w’:‘wensday’,
‘f’:‘friday’}
a=week[str(input(‘请输入第一位字母:’)).lower()]
if aweekT or aweekS:
print(a[str(input(‘请输入第二位字母:’)).lower()])
else:
print(a)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

a = [‘one’, ‘two’, ‘three’]
print(a[::-1])
1
2

L = [1,2,3,4,5]
print(’,’.join(str(n) for n in L))
1
2

def hello():
print(‘Hello World!’)
def helloAgain():
for i in range(2):
hello()

if name==‘main’:
helloAgain()
1
2
3
4
5
6
7
8

class bcolors:
OKBLUE = ‘\033[94m’
OKGREEN = ‘\033[92m’
WARNING = ‘\033[93m’
FAIL = ‘\033[91m’
ENDC = ‘\033[0m’
BOLD = ‘\033[1m’
UNDERLINE = ‘\033[4m’
print(bcolors.WARNING + “警告的颜色字体?” + bcolors.ENDC)
1
2
3
4
5
6
7
8
9
10

lo=int(input(‘下限：’))
hi=int(input(‘上限：’))
for i in range(lo,hi+1):
if i > 1:
for j in range(2,i):
if (i % j) == 0:
break
else:
print(i)
1
2
3
4
5
6
7
8
9

‘’’

‘’’
raw=[]
for i in range(10):
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

mat=[[1,2,3],
[3,4,5],
[4,5,6]
]
res=0
for i in range(len(mat)):
res+=mat[i][i]
print(res)
1
2
3
4
5
6
7
8

lis=[1,10,100,1000,10000,100000]
n=int(input('insert a number: '))
lis.append(n)
for i in range(len(lis)-1):
if lis[i]>=n:
for j in range(i,len(lis)):
lis[j],lis[-1]=lis[-1],lis[j]
break
print(lis)
1
2
3
4
5
6
7
8
9

lis=[1,10,100,1000,10000,100000]
for i in range(int(len(lis)/2)):
lis[i],lis[len(lis)-1-i]=lis[len(lis)-1-i],lis[i]
print(‘第一种实现：’)
print(lis)

lis=[1,10,100,1000,10000,100000]
print(‘第二种实现：’)
lis.reverse()
print(lis)
1
2
3
4
5
6
7
8
9
10
11

def dummy():
i=0
print(i)
i+=1

class cls:
i=0
def dummy(self):
print(self.i)
self.i+=1

a=cls()
for i in range(50):
dummy()
a.dummy()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

i=0
n=0
def dummy():
i=0
print(i)
i+=1
def dummy2():
global n
print(n)
n+=1
print(‘函数内部的同名变量’)
for j in range(20):
print(i)
dummy()
i+=1
print(‘global声明同名变量’)
for k in range(20):
print(n)
dummy2()
n+=10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class dummy:
num=1
def Num(self):
print(‘class dummy num:’,self.num)
print('global num: ',num)
self.num+=1

n=dummy()
num=1
for i in range(5):
num*=10
n.Num()
1
2
3
4
5
6
7
8
9
10
11
12

‘’’

‘’’
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[5,8,1],
[6,7,3],
[4,5,9]]

res=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(res)):
for j in range(len(res[0])):
res[i][j]=X[i][j]+Y[i][j]
print(res)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

res=0
for i in range(1,101):
res+=i
print(res)
1
2
3
4

while True:
try:
n=float(input(‘输入一个数字：’))
except:
print(‘输入错误’)
continue
dn=n**2
print(‘其平方为：’,dn)
if dn<50:
print(‘平方小于50，退出’)
break
1
2
3
4
5
6
7
8
9
10
11

def exc(a,b):
return (b,a)
a=0
b=10
a,b=exc(a,b)
print(a,b)
1
2
3
4
5
6

a=int(input(‘a=’))
b=int(input(‘b=’))
if a<b:
print(‘a<b’)
elif a>b:
print(‘a>b’)
else:
print(‘a=b’)
1
2
3
4
5
6
7
8

Max=lambda x,y:x*(x>=y)+y*(y>x)
Min=lambda x,y:x*(x<=y)+y*(y<x)

a=int(input(‘1:’))
b=int(input(‘2:’))

print(Max(a,b))
print(Min(a,b))
1
2
3
4
5
6
7
8

import random
print(random.uniform(10,20))
1
2

a=0o77
print(a)
b=a&3
print(b)
b=b&7
print(b)
1
2
3
4
5
6

a=0o77
print(a|3)
print(a|3|7)
1
2
3

a=0o77
print(a^3)
print(a37)
1
2
3

(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用(0<<4)
(3)将上面二者进行&运算。

a=int(input('输入一个数字: '))
b=0 # 0
b=~b # 1
b=b<<4 # 10000
b=~b # 1111
c=a>>4
d=c&b
print(‘a:’,bin(a))
print(‘b:’,bin(b))
print(‘d:’,bin(d))
1
2
3
4
5
6
7
8
9
10
11

print(~234)
print(~~234)
1
2

from tkinter import *
canvas=Canvas(width=800,height=600,bg=‘yellow’)
canvas.pack(expand=YES,fill=BOTH)
k=1
j=1
for i in range(26):
canvas.create_oval(310-k,250-k,310+k,250+k,width=1)
k+=j
j+=0.3
mainloop()
1
2
3
4
5
6
7
8
9
10

‘’’

‘’’
if name == ‘main’:
from tkinter import *

canvas = Canvas(width=300, height=300, bg='green')
canvas.pack(expand=YES, fill=BOTH)
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_line(x0,y0,x0,y1, width=1, fill='red')
x0 = x0 - 5
y0 = y0 - 5
x1 = x1 + 5
y1 = y1 + 5

x0 = 263
y1 = 275
y0 = 263
for i in range(21):
canvas.create_line(x0,y0,x0,y1,fill = 'red')
x0 += 5
y0 += 5
y1 += 5

mainloop()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

if name == ‘main’:
from tkinter import *
root = Tk()
root.title(‘Canvas’)
canvas = Canvas(root,width = 400,height = 400,bg = ‘yellow’)
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_rectangle(x0,y0,x1,y1)
x0 -= 5
y0 -= 5
x1 += 5
y1 += 5

canvas.pack()
root.mainloop()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

if name == ‘main’:
from tkinter import *
canvas = Canvas(width = 300,height = 300,bg = ‘green’)
canvas.pack(expand = YES,fill = BOTH)
x0 = 150
y0 = 100
canvas.create_oval(x0 - 10,y0 - 10,x0 + 10,y0 + 10)
canvas.create_oval(x0 - 20,y0 - 20,x0 + 20,y0 + 20)
canvas.create_oval(x0 - 50,y0 - 50,x0 + 50,y0 + 50)
import math
B = 0.809
for i in range(16):
a = 2 * math.pi / 16 * i
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = ‘red’)
canvas.create_oval(x0 - 60,y0 - 60,x0 + 60,y0 + 60)

for k in range(501):
for i in range(17):
a = (2 * math.pi / 16) * i + (2 * math.pi / 180) * k
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 + math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
for j in range(51):
a = (2 * math.pi / 16) * i + (2* math.pi / 180) * k - 1
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
mainloop()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

s=‘zhangguang101’
print(len(s))
1
2

def generate(numRows):
r = [[1]]
for i in range(1,numRows):
r.append(list(map(lambda x,y:x+y, [0]+r[-1],r[-1]+[0])))
return r[:numRows]
a=generate(10)
for i in a:
print(i)
1
2
3
4
5
6
7
8

s1=‘aabbxuebixuebi’
s2=‘ab’
s3=‘xue’
print(s1.find(s2))
print(s1.find(s3))
1
2
3
4
5

‘’’

‘’’
if name == ‘main’:
from tkinter import *
x = 360
y = 160
top = y - 30
bottom = y - 30

canvas = Canvas(width = 400,height = 600,bg = 'white')
for i in range(20):
canvas.create_oval(250 - top,250 - bottom,250 + top,250 + bottom)
top -= 5
bottom += 5
canvas.pack()
mainloop()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

if name == ‘main’:
from tkinter import *
canvas = Canvas(width = 400,height = 600,bg = ‘white’)
left = 20
right = 50
top = 50
num = 15
for i in range(num):
canvas.create_oval(250 - right,250 - left,250 + right,250 + left)
canvas.create_oval(250 - 20,250 - top,250 + 20,250 + top)
canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2))
right += 5
left += 5
top += 10

canvas.pack()
mainloop()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

import math
from tkinter import *

class PTS:
def init(self):
self.x = 0
self.y = 0
points = []

def LineToDemo():
screenx = 400
screeny = 400
canvas = Canvas(width = screenx,height = screeny,bg = ‘white’)

AspectRatio = 0.85
MAXPTS = 15
h = screeny
w = screenx
xcenter = w / 2
ycenter = h / 2
radius = (h - 30) / (AspectRatio * 2) - 20
step = 360 / MAXPTS
angle = 0.0
for i in range(MAXPTS):
rads = angle * math.pi / 180.0
p = PTS()
angle += step
points.append(p)
for i in range(MAXPTS):
for j in range(i,MAXPTS):
canvas.create_line(points[i].x,points[i].y,points[j].x,points[j].y)

canvas.pack()
mainloop()


if name == ‘main’:
LineToDemo()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

li=[3,2,5,7,8,1,5]

li[-1],li[li.index(min(li))]=li[li.index(min(li))],li[-1]

m=li[0]
ind=li.index(max(li))
li[0]=li[ind]
li[ind]=m

print(li)
1
2
3
4
5
6
7
8
9
10

from collections import *
li=[1,2,3,4,5,6,7,8,9]
deq=deque(li,maxlen=len(li))
print(li)
deq.rotate(int(input(‘rotate:’)))
print(list(deq))
1
2
3
4
5
6

‘’’

‘’’
if name == ‘main’:
nmax = 50
n = int(input(‘请输入总人数:’))
num = []
for i in range(n):
num.append(i + 1)

i = 0
k = 0
m = 0

while m < n - 1:
if num[i] != 0 : k += 1
if k == 3:
num[i] = 0
k = 0
m += 1
i += 1
if i == n : i = 0

i = 0
while num[i] == 0: i += 1
print(num[i])


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

def lenofstr(s):
return len(s)

print(lenofstr(‘tanxiaofengsheng’))
1
2
3
4

N = 3
#stu

# score[4]: list

student = []
for i in range(5):
student.append([’’,’’,[]])

def input_stu(stu):
for i in range(N):
stu[i][0] = input(‘input student num:\n’)
stu[i][1] = input(‘input student name:\n’)
for j in range(3):
stu[i][2].append(int(input(‘score:\n’)))

def output_stu(stu):
for i in range(N):
print (’%-6s%-10s’ % ( stu[i][0],stu[i][1] ))
for j in range(3):
print (’%-8d’ % stu[i][2][j])

if name == ‘main’:
input_stu(student)
print (student)
output_stu(student)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

‘’’

‘’’
class Node:

def __init__(self, data):
self.data = data
self.next = None

def get_data(self):
return self.data


class List:

def __init__(self, head):

def is_empty(self):
return self.get_len() == 0

def get_len(self):
length = 0
while temp is not None:
length += 1
temp = temp.next
return length

def append(self, node):
while temp.next is not None:
temp = temp.next
temp.next = node

def delete(self, index):
if index < 1 or index > self.get_len():
print("给定位置不合理")
return
if index == 1:
return
cur_pos = 0
while temp is not None:
cur_pos += 1
if cur_pos == index-1:
temp.next = temp.next.next
temp = temp.next

def insert(self, pos, node):
if pos < 1 or pos > self.get_len():
print("插入结点位置不合理")
return
cur_pos = 0
while temp is not Node:
cur_pos += 1
if cur_pos == pos-1:
node.next = temp.next
temp.next =node
break
temp = temp.next

while cur is not None:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre

init_data = []
return init_data


if name==‘main’:
for i in range(10):
node=Node(i)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

class Node:

def __init__(self, data):
self.data = data
self.next = None

def get_data(self):
return self.data


class List:

def __init__(self, head):

def is_empty(self):
return self.get_len() == 0

def get_len(self):
length = 0
while temp is not None:
length += 1
temp = temp.next
return length

def append(self, node):
while temp.next is not None:
temp = temp.next
temp.next = node

def delete(self, index):
if index < 1 or index > self.get_len():
print("给定位置不合理")
return
if index == 1:
return
cur_pos = 0
while temp is not None:
cur_pos += 1
if cur_pos == index-1:
temp.next = temp.next.next
temp = temp.next

def insert(self, pos, node):
if pos < 1 or pos > self.get_len():
print("插入结点位置不合理")
return
cur_pos = 0
while temp is not Node:
cur_pos += 1
if cur_pos == pos-1:
node.next = temp.next
temp.next =node
break
temp = temp.next

while cur is not None:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre

init_data = []
return init_data


if name==‘main’:
for i in range(10):
node=Node(i)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

a=[2,6,8]
b=[7,0,4]
a.extend(b)
a.sort()
print(a)
1
2
3
4
5

if name == ‘main’:
for i in range(5):
n = 0
if i != 1: n += 1
if i == 3: n += 1
if i == 4: n += 1
if i != 4: n += 1
if n == 3: print (64 + i)
1
2
3
4
5
6
7
8

‘’’

‘’’
def peven(n):
i = 0
s = 0.0
for i in range(2,n + 1,2):
s += 1.0 / i
return s

def podd(n):
s = 0.0
for i in range(1, n + 1,2):
s += 1.0 / i
return s

def dcall(fp,n):
s = fp(n)
return s

if name == ‘main’:
n = int(input('input a number: '))
if n % 2 == 0:
sum = dcall(peven,n)
else:
sum = dcall(podd,n)
print (sum)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

l=[‘moyu’,‘niupi’,‘xuecaibichi’,‘shengfaji’,‘42’]
for i in range(len(l)):
print(l[i])
1
2
3

if name == ‘main’:
person = {“li”:18,“wang”:50,“zhang”:20,“sun”:22}
m = ‘li’
for key in person.keys():
if person[m] < person[key]:
m = key

print ('%s,%d' % (m,person[m]))


1
2
3
4
5
6
7
8

l=[‘baaa’,‘aaab’,‘aaba’,‘aaaa’,‘abaa’]
l.sort()
print(l)
1
2
3

if name == ‘main’:
i = 0
j = 1
x = 0
while (i < 5) :
x = 4 * j
for i in range(0,5) :
if(x%4 != 0) :
break
else :
i += 1
x = (x/4) * 5 +1
j += 1
print(x)

for p in range(5):
x=(x-1)/5*4
print(x)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

a = 809
for i in range(10,100):
b = i * a
if b >= 1000 and b <= 10000 and 8 * i < 100 and 9 * i >= 100:
print(b,’ = 800 * ', i, ’ + 9 * ', i)

for i in range(10,100):
if 8i>99 or 9i<100:
continue
if 809i==800i+9*i:
print(i)
break
1
2
3
4
5
6
7
8
9
10
11
12
13

n=eval(‘0o’+str(int(input(‘八进制输入：’))))
print(n)
1
2

if name == ‘main’:
sum = 4
s = 4
for j in range(2,9):
print (sum)
if j <= 2:
s *= 7
else:
s *= 8
sum += s
print(‘sum = %d’ % sum)
1
2
3
4
5
6
7
8
9
10
11

delimiter = ‘,’
mylist = [‘Brazil’, ‘Russia’, ‘India’, ‘China’]
print(delimiter.join(mylist))
1
2
3

if name == ‘main’:
zi = int(input(‘输入一个数字:’))
n1 = 1
c9 = 1
m9 = 9
sum = 9
while n1 != 0:
if sum % zi == 0:
n1 = 0
else:
m9 *= 10
sum += m9
c9 += 1
print (’%d 个 9 可以被 %d 整除 : %d’ % (c9,zi,sum))
r = sum / zi
print (’%d / %d = %d’ % (sum,zi,r))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

a=‘guangtou’
b=‘feipang’
print(b+a)
1
2
3

‘’’

‘’’
if name == ‘main’:
class student:
x = 0
c = 0
def f(stu):
stu.x = 20
stu.c = ‘c’
a= student()
a.x = 3
a.c = ‘a’
f(a)
print(a.x,a.c)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

for i in range(3):
print(’*’*int(input('input a number: ')))
1
2

n=input()
n = str(n)
a=[]
for i in range(4):
a.append((int(n[i])+5)%10)
a[0],a[3]=a[3],a[0]
a[1],a[2]=a[2],a[1]
print ("".join(’%s’ %s for s in a))
1
2
3
4
5
6
7
8

#list
#新建列表
testList=[10086,‘中国移动’,[1,2,4,5]]

#访问列表长度
print (len(testList) )
#到列表结尾
print (testList[1:])
#向列表添加元素
testList.append(‘i’m new here!’)

print (len(testList) )
print (testList[-1] )
#弹出列表的最后一个元素
print (testList.pop(1) )
print (len(testList) )
print (testList )
#list comprehension
#后面有介绍，暂时掠过
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print (matrix )
print (matrix[1] )
col2 = [row[1] for row in matrix]#get a column from a matrix
print (col2 )
col2even = [row[1] for row in matrix if row[1] % 2 == 0]#filter odd item
print (col2even)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

if name == ‘main’:
import time
print (time.ctime(time.time()))
print (time.asctime(time.localtime(time.time())))
print (time.asctime(time.gmtime(time.time())))
1
2
3
4
5

‘’’

‘’’
if name == ‘main’:
import time
start = time.time()
for i in range(3000):
print(i)
end = time.time()

print (end - start)


1
2
3
4
5
6
7
8
9
10
11
12

if name == ‘main’:
import time
start = time.clock()
for i in range(100):
print(i)
end = time.clock()
print(‘different is %6.3f’ % (end - start))
1
2
3
4
5
6
7

if name == ‘main’:
import time
import random

play_it = input('do you want to play it.(\'y\' or \'n\')')
while play_it == 'y':
c = input('input a character:\n')
i = random.randint(0,2**32) % 100
print ('please input number you guess:\n')
start = time.clock()
a = time.time()
while guess != i:
if guess > i:
else:
end = time.clock()
b = time.time()
var = (end - start) / 18.2
print (var)
# print 'It took you %6.3 seconds' % time.difftime(b,a))
if var < 15:
print ('you are very clever!')
elif var < 25:
print ('you are normal!')
else:
print ('you are stupid!')
print ('The number you guess is %d' % i)
play_it = input('do you want to play it.')


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

from dateutil import parser
dt = parser.parse(“Aug 28 2015 12:00AM”)
print (dt)
1
2
3

s1=‘xuebixuebixuebixuebixuebixuebixuebixue’
s2=‘xuebi’
print(s1.count(s2))
1
2
3

if name == ‘main’:
from sys import stdout
filename = input(‘输入文件名:\n’)
fp = open(filename,“w”)
ch = input(‘输入字符串:\n’)
while ch != ‘#’:
fp.write(ch)
stdout.write(ch)
ch = input(’’)
fp.close()
1
2
3
4
5
6
7
8
9
10

if name == ‘main’:
fp = open(‘test.txt’,‘w’)
string = input(‘please input a string:\n’)
string = string.upper()
fp.write(string)
fp = open(‘test.txt’,‘r’)
fp.close()
1
2
3
4
5
6
7
8

if name == ‘main’:
import string
fp = open(‘test1.txt’)
fp.close()

fp = open('test2.txt')
fp.close()

fp = open('test3.txt','w')
l = list(a + b)
l.sort()
s = ''
s = s.join(l)
fp.write(s)
fp.close()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

i = [‘a’, ‘b’]
l = [1, 2]
print (dict(zip(i,l)))

08-04

11-01
09-02
03-24 19万+
03-31 20万+
12-30 5万+
07-20 4万+
07-19 2万+