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)
相关推荐
带娃的IT创业者1 小时前
《Python实战进阶》No39:模型部署——TensorFlow Serving 与 ONNX
pytorch·python·tensorflow·持续部署
Bruce-li__1 小时前
深入理解Python asyncio:从入门到实战,掌握异步编程精髓
网络·数据库·python
九月镇灵将1 小时前
6.git项目实现变更拉取与上传
git·python·scrapy·scrapyd·gitpython·gerapy
小张学Python2 小时前
AI数字人Heygem:口播与唇形同步的福音,无需docker,无需配置环境,一键整合包来了
python·数字人·heygem
跳跳糖炒酸奶2 小时前
第四章、Isaacsim在GUI中构建机器人(2):组装一个简单的机器人
人工智能·python·算法·ubuntu·机器人
步木木2 小时前
Anaconda和Pycharm的区别,以及如何选择两者
ide·python·pycharm
星始流年2 小时前
解决PyInstaller打包PySide6+QML应用的资源文件问题
python·llm·pyspider
南玖yy2 小时前
Python网络爬虫:从入门到实践
爬虫·python
The Future is mine3 小时前
Python计算经纬度两点之间距离
开发语言·python
九月镇灵将3 小时前
GitPython库快速应用入门
git·python·gitpython