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)
相关推荐
Open-AI7 分钟前
Python如何判断一个数是几位数
python
极客代码11 分钟前
【Python TensorFlow】入门到精通
开发语言·人工智能·python·深度学习·tensorflow
义小深13 分钟前
TensorFlow|咖啡豆识别
人工智能·python·tensorflow
疯一样的码农17 分钟前
Python 正则表达式(RegEx)
开发语言·python·正则表达式
进击的六角龙1 小时前
Python中处理Excel的基本概念(如工作簿、工作表等)
开发语言·python·excel
一只爱好编程的程序猿2 小时前
Java后台生成指定路径下创建指定名称的文件
java·python·数据下载
Aniay_ivy2 小时前
深入探索 Java 8 Stream 流:高效操作与应用场景
java·开发语言·python
gonghw4032 小时前
DearPyGui学习
python·gui
向阳12182 小时前
Bert快速入门
人工智能·python·自然语言处理·bert
engchina2 小时前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j