【强推】8个实用的Python程序

1. 引言

本文所提到的所有代码都曾经帮助我激发了解决问题的一些思考。不言而喻,如果您想学习编码和提升解决问题的能力,我们可以尝试自己来解决以下问题。

闲话少说,我们直接开始吧。 :)

2. 处理句子中的脏话

编写一个Python程序,用以实现从句子中删除脏话

我们经常会遇到开发某些线上应用程序时需要关注类似评论留言部分。假如我们需要监视某些脏话来将其进行屏蔽,此时下面这个Python包将会派上用场。将一个带有脏话的句子传递给profanity中的方法,它将返回一个星号来代替脏话。
这个包的安装如下:

pip install better_profanity

举例如下:

from better_profanity import profanity
censored_text = profanity.censor("Just shut up and piss off")               
print(censored_text) 

输出如下:
在这里插入图片描述

3.调换字符次序创建新单词

编写一个Python程序,用以打乱单词字符次序以创建新单词。

为了完成这个任务,我们需要打乱单词并与单词字典库进行比较。如果字典库中存在该单词,那么我们可以在控制台上打印该单词。我们可以借助于流行的第三方包 nltk(自然语言工具包)来确认生成的词是非为正常的单词。

样例代码如下:

from itertools import permutations
from nltk.corpus import words

wrd = input("Enter any word")
new = [''.join(data) for data in permutations(wrd)]                                
for i in new:
    if i in words.words():
        print(i)

运行结果如下:
在这里插入图片描述

4.创建1000个目录

编写一个Python程序,用以实现创建1000个目录

上述任务要求使用 Python 在个人计算机上创建一千个文件夹。下面这段代码就像魔术一样工作,很容易被用作朋友的恶作剧。样例如下:

import os
i = 1
j = 1000
while i <= j:
    os.mkdir(str(i))
    i += 1                                                                     

5.打乱段落中单词字符次序

编写一个 Python 代码来打乱每个单词并将其连接成段落。

下述代码将一个句子作为输入并输出一个加扰动的文本。将每个单词中的字母洗牌并重新组成句子。附加的打乱的术语会产生打乱的文本输出。有时即使字母被打乱了,我们仍然可以阅读,这很有趣。

import random
def scramble(sentence):
    words = []
    for word in  sentence.split():
        if len(word) > 1:
            words.append(word[0]
                       + ''.join(random.sample([char for char in word[1:-1]], len(word) - 2))+ word[-1])
        else:
            words.append(word)
    return ' '.join(words)
text= '''Shuffle words in this sentence.
 Python has a limitless number of packages used in everyday programming. 
'''
new = scramble(text)
print(new)

输出如下:
在这里插入图片描述

6. 控制台上绘制图形

使用cowsay包在控制台上用Python程序绘制动物图

著名的 cowsay API现在可用于 Python。这个包可以方便的绘制各种动物字符图案。

安装如下:

pip install cowsay

样例代码如下:

import cowsay  
cowsay.cow('HI, I AM A COW')                                                 

运行结果如下:

在这里插入图片描述

7. 使用Python打开记事本

写一个 Python 程序在电脑上打开记事本

subprocess包由几个有价值的子模块组成,用于资源管理。该软件包包含许多模块,可通过创建新进程来运行应用程序。

样例代码如下:

import subprocess
path = r'path/to/your/file.txt'
subprocess.Popen(['notepad.exe', path])                                 

上述代码运行后,就可以用记事本软件打开指定路径下的txt文件。

8. 获取电脑的IP地址

编写一个 Python 程序来获取自己PC 的 IP 地址

这个功能可以使用Python内置包socket来轻松完成。样例代码如下:

import socket
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(f"Hostname: {hostname}")
print(f"IP_Address: {ip_address}")                                                 

运行结果如下:
在这里插入图片描述

9. 使用Python实现关机

编写一个 Python 程序来关闭你的计算机

我们可以使用Python内置的os 包来实现关闭计算机的功能。请记住在执行此程序之前关闭并保存文件。

import os
shutdown = input("Do you want to shutdown your computer? enter(yes/no): ")
if shutdown == 'no':
    exit()
else:
    os.system("shutdown /s /t 1")

10. 总结

本文重点介绍了8个日常中实用的Python小程序,可以帮助我们提升日常工作效率,所有功能都给出了相应的代码实现。

您学废了吗?

在这里插入图片描述
关注公众号《AI算法之道》,获取更多AI算法资讯。

参考

  • 16
    点赞
  • 140
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
当然,我可以帮您写一个推箱子的Python程序。以下是一个基本的推箱子游戏示例: ```python # 定义地图 MAP = [ "#####", "# #", "# #", "#o #", "#####" ] # 将地图转换为二维列表 map_list = [list(row) for row in MAP] # 定义玩家的初始位置 player_x, player_y = 3, 1 # 定义箱子的初始位置 box_x, box_y = 3, 2 # 游戏循环 while True: # 打印地图 for row in map_list: print("".join(row)) # 判断是否通关 if map_list[1][3] == "o" and map_list[2][3] == "o": print("你通关了!") break # 获取玩家输入 move = input("移动方向(上:w,下:s,左:a,右:d):") # 移动箱子 if move == "w" and box_x == player_x - 1 and map_list[box_y - 1][box_x] == " ": map_list[box_y][box_x], map_list[box_y - 1][box_x] = " ", "o" box_y -= 1 elif move == "s" and box_x == player_x + 1 and map_list[box_y + 1][box_x] == " ": map_list[box_y][box_x], map_list[box_y + 1][box_x] = " ", "o" box_y += 1 elif move == "a" and box_y == player_y - 1 and map_list[box_y][box_x - 1] == " ": map_list[box_y][box_x], map_list[box_y][box_x - 1] = " ", "o" box_x -= 1 elif move == "d" and box_y == player_y + 1 and map_list[box_y][box_x + 1] == " ": map_list[box_y][box_x], map_list[box_y][box_x + 1] = " ", "o" box_x += 1 # 移动玩家 elif move == "w" and map_list[player_y - 1][player_x] == " ": map_list[player_y][player_x], map_list[player_y - 1][player_x] = " ", "@" player_y -= 1 elif move == "s" and map_list[player_y + 1][player_x] == " ": map_list[player_y][player_x], map_list[player_y + 1][player_x] = " ", "@" player_y += 1 elif move == "a" and map_list[player_y][player_x - 1] == " ": map_list[player_y][player_x], map_list[player_y][player_x - 1] = " ", "@" player_x -= 1 elif move == "d" and map_list[player_y][player_x + 1] == " ": map_list[player_y][player_x], map_list[player_y][player_x + 1] = " ", "@" player_x +=
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵卓不凡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值