Python30个基础题(三)

21、斐波那契数列

斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13;特别指出:第0项是0,第1项是第一个1。从第三项开始,每一项都等于前两项之和。

# 获取用户输入数据
nterms = int(input("你需要几项?"))

# 第一和第二项
n1 = 0
n2 = 1
count = 2

# 判断输出的值是否合法
if nterms <= 0:
    print("请输入一个正整数!")
elif nterms == 1:
    print("菲波那切数列:")
    print(n1)
else:
    print("菲波那切数列:")
    print(n1, ",", n2, end=",")
    while count < nterms:
        nth = n1 + n2
        print(n1+n2, end=",")
        # 更新值
        n1 = n2
        n2 = nth
        count += 1

输出结果:
你需要几项?33
菲波那切数列:
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309

22、十进制转二进制、八进制、十六进制

dec = int(input("输入数字:"))
print("十进制数为:", dec)
print("转换为二进制为:", bin(dec))
print("转换为八进制为:", oct(dec))
print("转换为十六进制为:", hex(dec))

输出结果:
输入数字:22
十进制数为: 22
转换为二进制为: 0b10110
转换为八进制为: 0o26
转换为十六进制为: 0x16

23、最大公约数

def hcf(x,y):
    '''该函数返回两个数的最大公约数'''
    # 获取最小值
    if x > y:
        smaller = y
    else:
        smaller = x

    for i in range(1, smaller + 1):
        if ((x % i == 0) and (y % i == 0)):
            hcf = i
    return hcf
# 用户输入两个数字
num1 = int(input("输入第一个数字:"))
num2 = int(input("输入第二个数字:"))
print(num1, "", num2, "的最大公约数为", hcf(num1, num2))

输入结果:
输入第一个数字:33
输入第二个数字:55
33 和 55 的最大公约数为 11

23、最小公倍数

def lcm(x,y):
    # 获取最大的数
    if x > y:
        greater = x
    else:
        greater = y

    while(True):
        if ((greater % x == 0) and (greater % y == 0)):
            lcm = greater
            break
        greater += 1
    return lcm
# 用户输入两个数字
num1 = int(input("输入第一个数字:"))
num2 = int(input("输入第二个数字:"))
print(num1, "", num2, "的最小公倍数为", lcm(num1, num2))

输出结果:
输入第一个数字:33
输入第二个数字:55
33 和 55 的最小公倍数为 165

24、简单计算器

# 定义函数
def add(x, y):
    '''相加'''
    return x + y
def subract(x, y):
    '''相减'''
    return x - y
def multiply(x, y):
    '''相乘'''
    return x * y
def divide(x, y):
    '''相除'''
    return x / y
# 用户输入
print("选择运算:")
print("1、相加")
print("2、相减")
print("3、相乘")
print("4、相除")
choice = input("输入你的选择(1/2/3/4):")
num1 = int(input("输入第一个数字:"))
num2 = int(input("输入第二个数字:"))
if choice == '1':
    print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
    print(num1, "-", num2, "=", subract(num1, num2))
elif choice == '3':
    print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
    if num2 != 0:
        print(num1, "/", num2, "=", divide(num1, num2))
    else:
        print("分母不能为0")
else:
    print("非法输入")

输出结果:
选择运算:
1、相加
2、相减
3、相乘
4、相除
输入你的选择(1/2/3/4):4
输入第一个数字:3
输入第二个数字:0
分母不能为0

25、生成日历

import calendar          # 引入日历模块
# 输入指点年月日
yy = int(input("输入年份:"))
mm = int(input("输入月份:"))
# 显示日历
print(calendar.month(yy, mm))

输出结果:
输入年份:2019
输入月份:2
   February 2019
Mo Tu We Th Fr Sa Su
             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

26、文件IO

# 写入文件
with open("test.txt", "wt") as out_file:
    out_file.write("该文本会写入到文件中\n看到我了吧!")
# 读取文件
with open("test.txt", "rt") as in_file:
    text = in_file.read()
print(text)

输出结果:
该文本会写入到文件中
看到我了吧!

27、字符串判断

str = "runoob.com"
print(str.isalnum())    # 判断所有字符都是数字或者字母
print(str.isalpha())    # 判断所有字符都是字母
print(str.isdigit())    # 判断所有字符都是数字
print(str.islower())    # 判断所有字符都是小写
print(str.isupper())    # 判断所有字符都是大写
print(str.istitle())    # 判断所有单词都是首字母大写,像标题
print(str.isspace())    # 判断所有字符都是空白字符,\t,\n,\r

输出结果:
False
False
False
True
False
False
False

28、字符串大小写转换

str = "https HTTP abc AbC"
print(str.upper())          # 把所有支付中的小写字母转换成大写字母
print(str.lower())          # 把所有支付中的大写字母转换成小写字母
print(str.capitalize())     # 把第一个字母转化为大写字母,其余小写
print(str.title())          # 把每个单词的第一个字母转化为大写,其余小写

输出结果:
HTTPS HTTP ABC ABC
https http abc abc
Https http abc abc
Https Http Abc Abc

29、计算每个月天数

import calendar
monthRange = calendar.monthrange(2019, 2)
print(monthRange)

输出结果:
(4, 28)

30、获取昨天的日期

import datetime
def getYesterday():
    today = datetime.date.today()
    oneday = datetime.timedelta(days=1)
    yesterday = today - oneday
    return yesterday
print(getYesterday())

输出结果:
2019-01-30

猜你喜欢

转载自www.cnblogs.com/yitao326/p/10342583.html