Python3 实例(三)

Python3 快速入门 实例(三)

21.Python 最小公倍数算法
def lcm(x, y):
    #  获取最大的数
    if x > y:
        greater = x
    else:
        greater = y
    while True:
        if (greater % x == 0) and (greater % y == 0):
            num = greater
            break
        greater += 1

    return num


# 用户输入两个数字
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))

print(num1, "和", num2, "的最小公倍数为", lcm(num1, num2))
22.Python 简单计算器实现(2个数字之间)
# 定义函数
def add(x, y):
    """相加"""
    return x + y

def subtract(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, "=", subtract(num1, num2))

elif choice == '3':
    print(num1, "*", num2, "=", multiply(num1, num2))

elif choice == '4':
    print(num1, "/", num2, "=", divide(num1, num2))
else:
    print("输入不合法")
23.Python 生成日历
# 引入日历模块
import calendar

# 输入指定年月
yy = int(input("输入年份:"))
mm = int(input("输入月份:"))

# 显示日历
print(calendar.month(yy, mm))
24.Python 使用递归斐波那契数列
# 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13,特别指出:第0项是0,第1项是第一个1。从第三项开始,每一项都等于前两项之和。
def cur(m):
    # 递归函数输出斐波那契数列
    if m <= 1:
        return i
    else:
        return cur(m - 1) + cur(m - 2)


# 获取用户输入
n = int(input("您要输出几项? "))

# 检查输入的数字是否正确
if n <= 0:
    print("输入正数")
else:
    print("斐波那契数列:")
    for i in range(n):
        print(cur(i))
25.Python 文件 IO
# 写入文件
with open("test.txt", "wt") as out_file:
    out_file.write("看看我\n宝宝在这儿。")

# 读取文件
with open("test.txt","rt") as in_file:
    txt = in_file.read()
	print(txt)
26.Python 字符串判断
# 测试实例一
print("测试实例一")
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

print("------------------------")

# 测试实例二
print("测试实例二")
str = "runoob"
print(str.isalnum())
print(str.isalpha())
print(str.isdigit())
print(str.islower())
print(str.isupper())
print(str.istitle())
print(str.isspace())
运行结果:
测试实例一
False
False
False
True
False
False
False
------------------------
测试实例二
True
True
False
True
False
False
False
27.Python 字符串大小写转换
str = "www.hao123.COM"
print(str.upper())          # 把所有字符中的小写字母转换成大写字母
print(str.lower())          # 把所有字符中的大写字母转换成小写字母
print(str.capitalize())     # 把第一个字母转化为大写字母,其余小写
print(str.title())          # 把每个单词的第一个字母转化为大写,其余小写

运行结果:
WWW.HAO123.COM
www.hao123.com
Www.hao123.com
Www.Hao123.Com
28.Python 计算每个月天数
# 引入日历模块
import calendar

monthRange = calendar.monthrange(2021, 2)
# 得到的是一个元组,第一个元素是所查月份的第一天对应的是星期几(0-6),第二个元素是这个月的天数。
print(f'第一天是星期{monthRange[0]},该月总共有{monthRange[1]}天。')
29.Python 获取昨天日期
# 引入 datetime 模块
import datetime

def getYesterday():
    today = datetime.date.today()  # 获得今天的日期
    oneday = datetime.timedelta(1)  # 得到一个的日期
    yesterday = today - oneday  # 得到昨天的日期
    return yesterday

# 输出
print(getYesterday())
30.Python 约瑟夫生者死者小游戏
# 30 个人在一条船上,超载,需要 15 人下船。
# 于是人们排成一队,排队的位置即为他们的编号。
# 报数,从 1 开始,数到 9 的人下船。
# 如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?
people = {}
for x in range(1, 31):
    people[x] = 1
check = 0
i = 1
j = 0
while i <= 31:
    if i == 31:
        i = 1
    elif j == 15:
        break
    else:
        if people[i] == 0:
            i += 1
            continue
        else:
            check += 1
            if check == 9:
                people[i] = 0
                check = 0
                print("{}号下船了".format(i))
                j += 1
            else:
                i += 1
                continue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值