以下是一些示例代码:

  1. 计算两个数字的和
num1 = 10
num2 = 20
sum = num1 + num2
print("The sum of", num1, "and", num2, "is", sum)

输出:

The sum of 10 and 20 is 30
  1. 判断一个数字是否为偶数
num = 6
if num % 2 == 0:
    print(num, "is even")
else:
    print(num, "is odd")

输出:

6 is even
  1. 遍历一个列表并打印每个元素
fruits = ["apple", "banana", "orange", "grape"]
for fruit in fruits:
    print(fruit)

输出:

apple
banana
orange
grape
  1. 定义一个函数来计算一个数的平方
def square(num):
    return num ** 2

print(square(5))

输出:

25
写几个代码来看看

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

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