本文目錄一覽:
python能做什麼有趣的東西
python能做什麼有趣的東西?下面給大家介紹35個Python實例:
1. Python3 實現圖片識別
2. Python3 圖片隱寫術
3. 200 行 Python 代碼實現 2048
4. Python實現3D建模工具
5. 使用 Python 定製詞雲
相關推薦:《Python教程》
6. Python3 智能裁切圖片
7.微信變為聊天機器人
8. 使用 Python 解數學方程
9. 使用 Python 創建照片馬賽克
10. Python 基於共現提取《釜山行》人物關係
11. Python 氣象數據分析:《Python 數據分析實戰》
12. NBA常規賽結果預測:利用Python進行比賽數據分析
13. Python 的循環語句和隱含波動率的計算
14. K-近鄰演算法實現手寫數字識別系統
15. 數獨遊戲的 Python 實現與破解
16. 基於 Flask 與 MySQL 實現番劇推薦系
17. Python 實現英文新聞摘要自動提取
18. Python 解決哲學家就餐問題
19. Ebay 在線拍賣數據分析
20. 神經網路實現人臉識別任務
21. 使用 Python 解數學方程
22. Python3 實現火車票查詢工具
23. Python 實現埠掃描器
24. Python3 實現可控制肉雞的反向Shell
25. Python 實現 FTP 弱口令掃描器
26. 基於PyQt5 實現地圖中定位相片拍攝位置
27. Python實現網站模擬登陸
28.Python實現簡易區域網視頻聊天工具
29. 基於 TCP 的 python 聊天程序
30. Python3基於Scapy實現DDos
31. 高德API + Python 解決租房問題
32. 基於 Flask 與 RethinkDB 實現TODO List
33. Python3 實現簡單的 Web 伺服器
34. Python 實現 Redis 非同步客戶端
35. 仿 StackOverflow 開發在線問答系統
python有趣的編程代碼
class Point:
row=0
col=0
def __init__(self, row, col):
self.row=row
self.col=col
def copy(self):
return Point(row=self.row, col=self.col)
#初始框架
import pygame
import random
#初始化
pygame.init()
W=800
H=600
ROW=30
COL=40
size=(W,H)
window=pygame.display.set_mode(size)
pygame.display.set_caption(‘貪吃蛇’)
bg_color=(255,255,255)
snake_color=(200,200,200)
head=Point(row=int(ROW/2), col=int(COL/2))
head_color=(0,128,128)
snakes=[
Point(row=head.row, col=head.col+1),
Point(row=head.row, col=head.col+2),
Point(row=head.row, col=head.col+3)
]
#生成食物
def gen_food():
while 1:
pos=Point(row=random.randint(0,ROW-1), col=random.randint(0,COL-1))
#
is_coll=False
#是否跟蛇碰上了
if head.row==pos.row and head.col==pos.col:
is_coll=True
#蛇身子
for snake in snakes:
if snake.row==pos.row and snake.col==pos.col:
is_coll=True
break
if not is_coll:
break
return pos
#定義坐標
food=gen_food()
food_color=(255,255,0)
direct=’left’ #left,right,up,down
#
def rect(point, color):
cell_width=W/COL
cell_height=H/ROW
left=point.col*cell_width
top=point.row*cell_height
pygame.draw.rect(
window, color,
(left, top, cell_width, cell_height)
)
pass
#遊戲循環
quit=True
clock=pygame.time.Clock()
while quit:
#處理事件
for event in pygame.event.get():
if event.type==pygame.QUIT:
quit=False
elif event.type==pygame.KEYDOWN:
if event.key==273 or event.key==119:
if direct==’left’ or direct==’right’:
direct=’up’
elif event.key==274 or event.key==115:
if direct == ‘left’ or direct == ‘right’:
direct=’down’
elif event.key==276 or event.key==97:
if direct == ‘up’ or direct == ‘down’:
direct=’left’
elif event.key==275 or event.key==100:
if direct == ‘up’ or direct == ‘down’:
direct=’right’
#吃東西
eat=(head.row==food.row and head.col==food.col)
#重新產生食物
if eat:
food = gen_food()
#處理身子
#1.把原來的頭,插入到snakes的頭上
snakes.insert(0, head.copy())
#2.把snakes的最後一個刪掉
if not eat:
snakes.pop()
#移動
if direct==’left’:
head.col-=1
elif direct==’right’:
head.col+=1
elif direct==’up’:
head.row-=1
elif direct==’down’:
head.row+=1
#檢測
dead=False
#1.撞牆
if head.col0 or head.row0 or head.col=COL or head.row=ROW:
dead=True
#2.撞自己
for snake in snakes:
if head.col==snake.col and head.row==snake.row:
dead=True
break
if dead:
print(‘死了’)
quit=False
#渲染——畫出來
#背景
pygame.draw.rect(window, bg_color, (0,0,W,H))
#蛇頭
for snake in snakes:
rect(snake, snake_color)
rect(head, head_color)
rect(food, food_color)
#
pygame.display.flip()
#設置幀頻(速度)
clock.tick(8)
#收尾工作
這是一個簡易版貪吃蛇的代碼,雖然結構簡單,但是該有的功能都是完整的,可玩性也不錯
python必背入門代碼
python必背入門代碼如下:
defnot_empty(s):
returnsandlen(s。strip())0
#returnsands。strip()
#如果直接單寫s。strip()那麼s如果是None,會報錯,因為None沒有strip方法。
#如果s是None,那麼Noneand任何值都是False,直接返回false
#如果s非None,那麼判定s。trip()是否為空。
這樣子filter能過濾到None,””,””這樣的值。
分成兩部分看。第一部分是對長度進行序列。相當於就是range(5)他的結果就是。01234。第二部分就是具體的排序規則。排序規則是用nums的值進行排序,reverse沒申明就是默認升序。就是用nums(0到4)的值進行排序,根據這個結果返回的一個range(5)的數組。
python必背內容:
1、變數。指在程序執行過程中,可變的量。定義一個變數,就會伴隨有3個特徵,分別是內存ID,數據類型和變數值。常量,指在程序執行過程中,不可變的量。一般都用大寫字母定義常量。
2、與程序交互。古時候,我們去銀行取錢,需要有一個銀行業務員等著我們把自己的賬號密碼輸入給他,然後他去進行驗證等成功後,我們再將取款金額輸入,告訴他。
驕傲的現代人,會為客戶提供一台ATM機,讓ATM機跟用戶交互,從而取代人力。然而機器是死的,我們必須為其編寫程序來運行,這就要求我們的編程語言中能夠有一種能與用戶交互,接收用戶輸入數據的機制。
好玩的python代碼示例
import random
while True:
# 出拳
punches = [‘石頭’,’剪刀’,’布’]
computer_choice = random.choice(punches)
user_choice = ”
user_choice = input(‘請出拳:(石頭、剪刀、布)’) # 請用戶輸入選擇
while user_choice not in punches: # 當用戶輸入錯誤,提示錯誤,重新輸入
print(‘輸入有誤,請重新出拳’)
user_choice = input()
# 亮拳
print(‘————戰鬥過程————’)
print(‘電腦出了:%s’ % computer_choice)
print(‘你出了:%s’ % user_choice)
# 勝負
print(‘—————結果—————’)
if user_choice == computer_choice: # 使用if進行條件判斷
print(‘平局!’)
# 電腦的選擇有3種,索引位置分別是:0石頭、1剪刀、2布。
# 假設在電腦索引位置上減1,對應:-1布,0石頭,1剪刀,皆勝。
elif user_choice == punches[punches.index(computer_choice)-1]:
print(‘你贏了!’)
else:
print(‘你輸了!’)
a1 = input(‘要繼續遊戲嗎,請輸入n退出,輸入其他繼續:’) # 在 while True 循環中設置跳出條件。
if a1 == ‘n’:
break
else:
print(‘———next game——- are you ready???’)
原創文章,作者:ERKI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/131045.html