python 经典例题

python 复制代码
num = int(input("Enter an integer: "))
reversed_num = 0
while num!=0:
    remainder = num % 10
    reversed_num = reversed_num * 10 + remainder
    num //=10
    print("The reversed number is",reversed_num)
python 复制代码
num = int(input("Enter an integer: "))
divisible_by_5_and_6 = (num % 5 == 0) and (num % 6 ==0)
divisible_by_5_or_6 = (num % 5 == 0) or (num % 6 ==0)
divisible_by_5_or_6_but_not_both= ((num % 5 == 0) and (num % 6!=0)) or ((num % 5!= 0) and (num % 6 == 0))
print(f"Is {num} divisible by 5 and 6? {divisible_by_5_and_6}")
print(f"Is {num} divisible by 5 or 6? {divisible_by_5_or_6}")
print(f"Is {num} divisible by 5 or 6, but not both? {divisible_by_5_or_6_but_not_both}")
python 复制代码
# 左下三角形
print("模式A")
for i in range(6):
    for j in range(6):
        if j < i+1:
            print(j+1, end=" ")
        else:
            print("", end=" ")
            print()
print()
# 左上三角形
print("模式B")
for i in range(6):
    for j in range(6):
        if j < (6-i):
            if j != (6-i-1):
                print(j+1, end=" ")
        else:
            print(j+1, end=" ")
    else:
        print(" ", end=" ")
    print()
print()
# 右下三角形
print("模式C")
for i in range(6):
    for j in range(6):
        if (i+j) >=5:
            print(j+1-5+i, end=" ")
        else:
            print(" ", end=" ")
            print()
print()
# 右上三角形
print("模式D")
for i in range(6):
    for j in range(6):
        if j >= i:
            print(j+1-i, end=" ")
        else:
            print(" ", end=" ")
            print()
print()
相关推荐
Swift社区1 小时前
在 Swift 中实现字符串分割问题:以字典中的单词构造句子
开发语言·ios·swift
没头脑的ht1 小时前
Swift内存访问冲突
开发语言·ios·swift
没头脑的ht2 小时前
Swift闭包的本质
开发语言·ios·swift
wjs20242 小时前
Swift 数组
开发语言
stm 学习ing3 小时前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
湫ccc3 小时前
《Python基础》之字符串格式化输出
开发语言·python
mqiqe4 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin4 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python