Python 代码示例:计算矩形、圆形面积和自然数之和
Python 代码示例:计算矩形、圆形面积和自然数之和
以下代码示例分别展示了如何使用 Python 计算矩形的面积、圆形的面积以及 1 到 100 自然数的总和。
1. 计算矩形面积
# This program calculates the area of a rectangle
length = 5
width = 3
area = length * width
print('The area of the rectangle is:', area)
2. 计算圆形面积
# This program calculates the area of a circle
radius = 4
area = 3.14159 * radius ** 2
print('The area of the circle is:', area)
3. 计算 1 到 100 自然数之和
# This program calculates the sum of the first 100 natural numbers
sum = 0
for i in range(1, 101):
sum += i
print('The sum of the first 100 natural numbers is:', sum)
以上代码示例简洁易懂,可以帮助您更好地理解 Python 的基本语法和操作。
原文地址: https://www.cveoy.top/t/topic/lBcn 著作权归作者所有。请勿转载和采集!