admin 管理员组

文章数量: 1184232


2023年12月19日发(作者:简述jsp的特点)

python的经典计算例题

下面是一些 Python 中的经典计算例题:

1. 计算两个数的和

```python

num1 = 3

num2 = 5

result = num1 + num2

print("The sum is:", result)

```

2. 计算两个数的乘积

```python

num1 = 4

num2 = 2

result = num1 * num2

print("The product is:", result)

```

3. 计算一个数的平方

```python

num = 5

result = num ** 2

print("The square is:", result)

```

4. 计算一个列表中所有数的和

```python

numbers = [1, 2, 3, 4, 5]

result = sum(numbers)

print("The sum is:", result)

```

5. 计算一个列表中所有数的平均值

```python

numbers = [1, 2, 3, 4, 5]

result = sum(numbers) / len(numbers)

print("The average is:", result)

```

6. 计算一个字符串中字符的个数

```python

string = "Hello, world!"

result = len(string)

print("The number of characters is:", result)

```

这些例题展示了 Python 在基本计算和列表操作上的使用。您可以根据需要进行适当的修改和扩展。


本文标签: 计算 简述 修改