python实例小项目

0 去试试创业网

1. 简单的猜数字游戏:随机生成一个1到100之间的数字,让用户猜测,如果猜大了或猜小了,给出提示。

```python import random

number_to_guess = random.randint(1, 100) guess = None

while guess!= number_to_guess: guess = int(input("Guess a number between 1 and 100: ")) if guess < number_to_guess: print("Too low!") elif guess > number_to_guess: print("Too high!")

print(f"Congratulations! You guessed the number {number_to_guess}.")

```

2. BMI计算器:用户输入身高和体重,程序计算并输出BMI指数。

```python height = float(input("Enter your height in meters: ")) weight = float(input("Enter your weight in kilograms: "))

bmi = weight / (height 2)

print(f"Your BMI is {bmi:.2f}.") ```

3. 简单的日记应用:用户可以输入他们的日记条目,这些条目将被保存在文本文件中。

```python def write_diary(): diary_entry = input("Enter your diary entry: ") with open('diary.txt', 'a') as file: file.write(diary_entry + '\n')

write_diary() ```

4. 简单的货币转换器:用户输入货币类型和金额,程序将进行货币转换。

```python def currency_converter(): currency Converter from USD to EUR usd = float(input("Enter amount in USD: ")) rate = 0.91 # current exchange rate eur = usd * rate print(f"{usd} USD is equal to {eur:.2f} EUR.")

currency_converter() ```

5. 简单的数学练习:程序随机生成加减乘除的数学题目,用户输入答案,程序判断答案是否正确。

```python import random

def math_practice(): num1 = random.randint(1, 10) num2 = random.randint(1, 10) operator = random.choice(["+", "-", "*", "/"])

if operator == "+": correct_answer = num1 + num2 elif operator == "-": correct_answer = num1 - num2 elif operator == "*": correct_answer = num1 * num2 else: correct_answer = round(num1 / num2, 2)

user_answer = float(input(f"What is {num1} {operator} {num2}? "))

if user_answer == correct_answer: print("Correct!") else: print(f"Wrong. The correct answer is {correct_answer}.")

math_practice() ```

这些项目都是基础的,随着Python知识的深入,你可以尝试更复杂的项目,比如Web应用开发,数据分析,机器学习等。

5分钟就能完成的5个Python小项目,赶紧拿去练习吧

顾名思义,通知生成器会生成有关通知,提醒你需要的任何内容的通知(消息)。在今天的构建中,我们将在Windows设备上生成一个弹出通知。还可以设置你喜欢的音乐,或者,你可以设置在电池电量低于35%时发出警报或提醒

df = pd.DataFrame({'location':location, 'temperature':temperature})print('温度列')print(df['temperature'])

python实例小项目

from collections import Counter# 检查两个字符串是否 相同字母异序词,简称:互为变位词def anagram(str1, str2): return Counter(str1) == Counter(str2)anagram('eleven+two', 'twelve+one') # True 这是一对神器的变位词anagram('eleven', 'twelve') # False

def head(lst): return lst[0] if len(lst) > 0 else Nonehead([]) # Nonehead([3, 4, 1]) # 3

from math import ceildef divide(lst, size): if size <= 0: return [lst] return [lst[i * size:(i+1)*size] for i in range(0, ceil(len(lst) / size))]r = divide([1, 3, 5, 7, 9], 2) # [[1, 3], [5, 7], [9]]

1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

for i in range(1,10): for j in range(1,i+1): print('{0}*{1}={2}'.format(j,i,j*i),end="\t") print()

x,y = mgrid[0:5,0:5]list(map(lambda xe,ye: [(ex,ey) for ex, ey in zip(xe, ye)], x,y))[[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)], [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)], [(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)], [(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)], [(4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]]

告别枯燥,60秒学会一个Python小例子。奔着此出发点,我在过去1个月,将平时经常使用的代码段换为小例子,分享出来后受到大家的喜欢。

10个非常适合菜鸟练手的Python项目环保小生意都有哪些活动,墙裂建议收藏

在Python中进行随机数值的选择,可以利用Python的内置库random,上图函数中,choice函数是从列表中随机选择一个数值,choices函数又放回的选择k个数值,sample则是无放回的选择k个数值。

专题: 到农村创业   农村创业贷   来农村创业