# -*- coding: utf-8 -*-
"""
Calcualte 5!
"""
def tang(i):
sum_value = 0
if i == 0:
sum_value = 1
else:
sum_value = i * tang(i-1)
return sum_value
for j in range(10):
print('%d!=%d'%(j,tang(j)))
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/untitled6.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
0!=1
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
# -*- coding: utf-8 -*-
"""
利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来
"""
def output(s,l):
if l == 0:
return
print(s[l-1])
output(s,l-1)
s = input("Please input a string")
l = len(s)
output(s,l)
# -*- coding: utf-8 -*-
"""
按逗号分割列表
"""
L = ['tang','lilie','hangmeimei',123]
s = ','.join(str(n) for n in L)
print(s)
"""
将一个数组逆序输出
"""
a = [5,7,8,5,9,1]
N = len(a)
print(a)
for i in range(int(len(a)/2)):
a[i],a[N-1-i]= a[N-1-i],a[i]
print(a)
"""
两个3行3列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵
"""
X = [[1,2,3],[4,5,6],[7,8,9]]
Y = [[10,2,3],[4,50,6],[7,8,90]]
Z = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
for j in range(3):
Z[i][j] = X[i][j] + Y[i][j]
for z in Z:
print(z)
"""
匿名函数求和
"""
sum_value = lambda x,y:x+y
print(sum_value(1,2))
"""
查找字符串位置
"""
s1 = 'asdgffgghhyjugfgd'
s2 = 'ffg'
print(s1.find(s2))
"""
在字典中找出年龄最大的,并输出
"""
people = {'laowang':40,'Me':30,'ZS':35}
m = 'Me'
for key in people.keys():
if people[key] > people[m]:
m = key
print(m,people[m])
"""
将列表转为字典结构
"""
key = ['a','b']
value = [123,456]
print(dict([key,value]))
"""
从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件中
"""
f = open('test.txt','w')
s = input("Please input a string")
s = s.upper()
f.write(s)
f.close()
f = open('test.txt','r')
print(f.read())
f.close()