Python 练习程序--02

# -*- 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()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值