第1关:列表操作
# -*- coding: utf-8 -*-
x = [1,2,3,4,5,6,7,8,9,10]
#a.用2种方法输出x中所有奇数
print("result of a:")
###### Begin ######
x = [1,2,3,4,5,6,7,8,9,10]
a=[]
for i in range(0,len(x)):
if x[i]%2!=0:
a.append(x[i])
print(a)
for i in x:
if i%2==0:
x.remove(i)
print(x)
####### End #######
#b.输出大于3,小于7的偶数
print("result of b:")
###### Begin ######
x = [1,2,3,4,5,6,7,8,9,10]
a=[]
for i in range(0,len(x)):
if x[i]>3 and x[i]<7 and x[i]%2==0:
a.append(x[i])
print(a)
####### End #######
#c.用2种方法输出[1,2,3,…10,11,…20]
print("result of c:")
###### Begin ######
x = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(x)):
a=10+x[i]
x.append(a)
print(x)
print(x)
####### End #######
#d.输出x的最大值、最小值。
print("result of d:")
###### Begin ######
x = [1,2,3,4,5,6,7,8,9,10]
b=100
for i in x:
if b>i:
b=i
print('The minimum is:',b)
a=0
for i in x:
if a
第2关:元组操作
# -*- coding: utf-8 -*-
#1-2.针对用元组创建的5*5矩阵,格式化输出并计算其对角线元素之和
a=((1,2,3,4,5),(2,3,4,5,6),(3,4,5,6,7),(4,5,6,7,8),(5,6,7,8,9))
###### Begin ######
print("the 5*5 matrix is:")
for x in a:
for y in x:
print(y,end=" ")
print("")
print("the result is:")
sum=0
for i in range(5):
for j in range(5):
if(i==j):
sum+=a[i][i]
for i in range(5):
for j in range(5):
if(i+j==4 and not(i==j)):
sum+=a[i][j]
print(sum)
####### End #######
第3关:字典操作
# -*- coding: utf-8 -*-
#1-3.利用字典结构求解任意字符串中每个字符的出现次数
str = "utperjg;fjmgv nrivninosamvroewitv mn[cwaocfrpnwco ntsportnv;/dsvmnrpotam nvsarimnwsacorvnimspovaiocasoivcanponvcwtvmn"
###### Begin ######
print("the raw string is :",str)
print("the results of statistics are:")
result={}
for i in str:
if i in result:
result[i]+=1
else:
result[i]=1
print(result)
####### End #######
第4关:集合操作
# -*- coding: utf-8 -*-
#1-4.用集合结构求解任意字符串中未出现的字母
#字符转数字:ord(字符)
#数字转字符:chr(数字)
#‘a’-’z’:97-122
#'A’-’Z’:65-90
str = "sadfsafsfewrwtsafdsfewrewtrewtrgfdsfgafrewfrawefewtrgfvxczvxcvxzcvdsva"
###### Begin ######
print("the raw string is :",str)
print("the sorted result is :")
d=[];
for i in str:
a=ord(i)
d.append(a)
ans=[]
for i in range(65,91):
k=1
for j in d:
if(i==j):
k=0
break
if(k): ans.append(chr(i))
for i in range(97,123):
k=1
for j in d:
if(i==j):
k=0
break
if(k): ans.append(chr(i))
print(ans)
####### End #######
第5关:函数调用
# -*- coding: utf-8 -*-
#1-5 设计递归函数计算斐波那契数列,并打印前30个数据
def fib(n):
#此处填写fib函数代码
###### Begin ######
if n==1 or n==2:
return 1
else:
return fib(n-1)+fib(n-2)
####### End #######
#此处填写调用fib函数,输出结果的代码
###### Begin ######
print("the result is : ")
for i in range(30):
if(i==29):print(fib(i+1),end=" ")
else: print(fib(i+1),end=",")
####### End #######
第6关:递归的缺陷
# -*- coding: utf-8 -*-
#1-6 计算并输出斐波那契数列的前100项
###### Begin ######
def fib(n):
if n==1 or n==2:
return 1
else:
f1=1
f2=0
fn=0
for i in range(n-1):
fn=f2+f1
f2=f1
f1=fn
return fn
print("the result is : ")
for i in range(100):
if(i==99):print(fib(i+1),end=" ")
else: print(fib(i+1),end=",")
####### End #######
第7关:格式化输出
# -*- coding: utf-8 -*-
#1-7 输出如图的杨辉三角
#7行15列
###### Begin ######
l = [[0] * i for i in range(1, 31)]
for i in range(30):
for j in range(i + 1):
if j == 0 or i == j:
l[i][j] = 1
else:
l[i][j] = l[i - 1][j - 1] + l[i - 1][j]
n = 7
print("the result is : ")
for i in range(n):
for j in range(i + 1):
if(j==0):print(" "*3*(n-i+1),end="")
print('{:>6d}'.format(l[i][j]), end='')
print()
####### End #######
第8关:目录与文件操作
# -*- coding: utf-8 -*-
#1-8 目录和文件操作
import os
# 第一步:在代码文件当前目录下创建目录,目录名为:fib1000
#若fib1000目录不存在,创建该目录
myDir = os.getcwd()+r'\fib1000'
###### Begin ######
#os.makedirs(myDir)
####### End #######
# 第二步:将斐波那契数列的前1000个数据以如下格式写入文件fib_data.txt,
# 格式:
# 第1个数是:0
# 第2个数是:1
# ......
print("正在将斐波那契数列的前1000个数据写入文件...")
f1path = myDir+r"\fib_data.txt"
###### Begin ######
def fib(n):
if n==1 or n==2:
return 1
elif n==0:
return 0
else:
f1=1
f2=0
fn=0
for i in range(n-1):
fn=f2+f1
f2=f1
f1=fn
return fn
file=open(f1path, mode='w')
for i in range(1001):
file.write("第 {} 个数是:{}{}{}".format(i+1,fib(i)," ","\n\n"))
file.close()
# file=open(f1path, mode='r')
# print(file.read())
# file.close()
####### End #######
print("完成!")
print("fib_data 已保存")
# 第三步:截取fib.txt文件的第899-920行,并写入新文件fib_part_data.txt
f2path = myDir+r"\fib_part_data.txt"
print("正在截取数据...")
###### Begin ######
file=open(f1path, mode='r')
data=list(file)
data=data[1796:1839]
with open("fib_part_data.txt", 'w+', encoding='utf-8') as f:
f.writelines(data)
file.close()
####### End #######
print("完成!")
print("fib_part_data 已保存")
#显示fib_part_data.txt文件内容
###### Begin ######
with open("fib_part_data.txt", 'r', encoding='utf-8') as f:
data=f.read()
print(data)
####### End #######