Python 初学者练手的小程序代码109


Python 是一种入门友好的编程语言,非常适合初学者练习编写代码。以下是一些简单的 Python 程序,可以帮助您提升编程技能并巩固所学知识。

1. Hello World

最经典的 Python 程序莫过于 "Hello World"。它输出 "Hello World!" 字符串到标准输出。```python
print("Hello World!")
```

2. 计算面积

编写一个程序来计算长方形的面积。用户输入长和宽,程序输出面积。```python
length = float(input("Enter the length: "))
width = float(input("Enter the width: "))
area = length * width
print(f"The area of the rectangle is {area}.")
```

3. 质数检查

编写一个程序来检查一个数字是否是质数。质数是指只能被 1 和自身整除的正整数。```python
number = int(input("Enter a number: "))
if number arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
numbers = list(map(int, input("Enter a list of numbers separated by commas: ").split(",")))
bubble_sort(numbers)
print(numbers)
```

13. 二分搜索

编写一个程序来在给定列表中查找一个数字。程序使用二分搜索算法来查找目标数字。```python
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low

2024-10-16


上一篇:Python 列表转为字符串:全面指南

下一篇:Python 中的 find() 函数:查找字符串中的子字符串