1.
#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 += " " * 2for 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 += " " * 2for 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.
n = input("Enter an integer:")
n = n[3::-1]
print(f"The reversed number is {n}")
运行结果如下:
3.
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)