以下是一些Python示例:

  1. 打印“Hello World”:
print("Hello World")
  1. 计算两个数的和:
num1 = 10
num2 = 20

sum = num1 + num2

print("The sum of {0} and {1} is {2}".format(num1, num2, sum))
  1. 判断一个数是奇数还是偶数:
num = 7

if num % 2 == 0:
    print("{0} is even".format(num))
else:
    print("{0} is odd".format(num))
  1. 计算一个数的阶乘:
num = 5

factorial = 1

if num < 0:
    print("Sorry, factorial does not exist for negative numbers.")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1, num + 1):
        factorial = factorial * i
    print("The factorial of {0} is {1}".format(num, factorial))
  1. 使用函数实现斐波那契数列:
def fibonacci(n):
    if n <= 0:
        print("Incorrect input")
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))
python示例

原文地址: https://www.cveoy.top/t/topic/yEu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录