python基础代码示例

Hello World:

python
print("Hello, World!")

计算两个数字的和:

python
num1 = 5 num2 = 3 sum = num1 + num2 print("The sum is:", sum)

判断奇偶数:

python
num = 7 if num % 2 == 0: print(num, "is even.") else: print(num, "is odd.")

使用循环输出列表中的元素:

python
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)

定义函数并调用:

python
def greet(name): print("Hello,", name, "!") greet("Alice")

这些示例涵盖了Python的一些基本概念,包括变量赋值、条件语句、循环、列表和函数。希望对你有所帮助!