python作业一

1.

python 复制代码
#A.方法一
num = int(input("请输入要打印的层数:"))
for n in range(1, num+1):
    s = ""
    for i in range(1, n+1):
        s += f"{i}" + " "

    print(s)
#方法二:
i=int(input("i:"))
for i in range(1,i+1):
    s=""
    for j in range(1,i+1):
        s+=f"{j}\t"
    print(s)

#B.方法一:
num = int(input("请输入要打印的层数:"))
for i in range(num+1, 0, -1):
    s=" "
    for j in range(1, i, 1):
        s+=f"{j}" + " "
    print(s) 

#方法二:
n = int(input("n:"))
for i in range(n):
    for j in range(1,n-i+1):
        print(f"{j}\t",end='')
    print()

#C.方法一:
num = int(input("请输入要打印的层数:"))
for i in range(1, num + 1):
    s = ""
    for k in range(num - i):
        s += " " * 2
    for j in range(0, i):
        s += f"{(i - j)}" + " "
    print(s)
#方法二:
n = int(input("n:"))
for i in range(1,n+1):

    for _ in range(n-i):
        print("\t",end='')

    for j in range(i,0,-1):
        print(f"{j}\t",end='')
    print()

#D.方法一:
num = int(input("请输入要打印的层数:"))
for i in range(num + 1, 0, -1):
    s = ""
    for k in range(num + 1 - i):
        s += " " * 2
    for j in range(i - 1, 0, -1):
        s += f"{(i - j)}" + " "
    print(s)
#方法二:
n = int(input("n:"))
for i in range(1,n+1):

    for _ in range(i-1):
        print("\t",end='')

    for j in range(1,n+2-i):
        print(f"{j}\t",end='')
    print()

2.

python 复制代码
n = input("Enter an integer:")
n = n[3::-1]
print(f"The reversed number is {n}")

运行结果如下:

3.

python 复制代码
n = int(input("Enter an integer:"))
m = n % 5 == 0 and n % 6 == 0
print(f"Is {n} divisible by 5 and 6?", m)
m = n % 5 == 0 or n % 6 == 0
print(f"Is {n} divisible by 5 or 6?", m)
m = not (n % 5 == 0 and n % 6 == 0)
print(f"Is {n} divisible by 5 and 6,but not both", m)
相关推荐
古希腊掌管学习的神1 小时前
[搜广推]王树森推荐系统——矩阵补充&最近邻查找
python·算法·机器学习·矩阵
LucianaiB2 小时前
探索CSDN博客数据:使用Python爬虫技术
开发语言·爬虫·python
PieroPc4 小时前
Python 写的 智慧记 进销存 辅助 程序 导入导出 excel 可打印
开发语言·python·excel
梧桐树04298 小时前
python常用内建模块:collections
python
Dream_Snowar8 小时前
速通Python 第三节
开发语言·python
蓝天星空9 小时前
Python调用open ai接口
人工智能·python
jasmine s10 小时前
Pandas
开发语言·python
郭wes代码10 小时前
Cmd命令大全(万字详细版)
python·算法·小程序
leaf_leaves_leaf10 小时前
win11用一条命令给anaconda环境安装GPU版本pytorch,并检查是否为GPU版本
人工智能·pytorch·python
夜雨飘零110 小时前
基于Pytorch实现的说话人日志(说话人分离)
人工智能·pytorch·python·声纹识别·说话人分离·说话人日志