Python 5行代码的神奇操作(1):
1、古代兔子问题
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
def count(n): if (1 == n or 2 == n): return 1 elif (n >= 2): return count(n - 2) + count(n - 1) print(count(36) * 2)
2、加法计算器
num1 = input("第一个数:") num2 = input("第二个数:") new_num1 = int(num1) new_num2 = int(num2) print(new_num1 + new_num2)
3、循环问答
while(True): question = input() answer = question.replace('吗', '呢') answer = answer.replace('?', '!') print(answer)
输出:
在吗 在呢 吃饭了吗 吃饭了呢 要下班了吗 要下班了呢 最近好吗 最近好呢
4、实现一个简单的服务器
from http import server from http.server import SimpleHTTPRequestHandler server_address = ('127.0.0.1', 8888) httpd = server.HTTPServer(server_address, SimpleHTTPRequestHandler) httpd.serve_forever()
5、九九乘法表1
for i in range(1, 10): for j in range(1, i+1): print('{}x{}={}\t'.format(j, i, i*j), end='') print()
输出:
1x1=1 1x2=22x2=4 1x3=32x3=63x3=9 1x4=42x4=83x4=124x4=16 1x5=52x5=103x5=154x5=205x5=25 1x6=62x6=123x6=184x6=245x6=306x6=36 1x7=72x7=143x7=214x7=285x7=356x7=427x7=49 1x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=64 1x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=81
想了解更多python知识,请观看Python入门与进阶教学视频(极客学院)视频!