Python是一種高級編程語言,擁有許多強大的功能,可以用于許多不同的用途。以下是Python能夠實現的一些任務和應用程序:
# 網絡編程 import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('www.google.com', 80)) s.send(b'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n') data = s.recv(1024) s.close() # 數據分析和可視化 import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('data.csv') plt.plot(data['x'], data['y']) plt.show() # 機器學習 from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression diabetes = datasets.load_diabetes() X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target, test_size=0.2) model = LinearRegression() model.fit(X_train, y_train) model.score(X_test, y_test) # 網絡爬蟲 import requests from bs4 import BeautifulSoup url = 'https://www.python.org/' response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') print(soup.title.string) # 自動化 import pyautogui pyautogui.moveTo(100, 100, duration=0.25) pyautogui.click() # 游戲制作 import pygame pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('My Game') running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((255, 255, 255)) pygame.display.flip() pygame.quit()
Python還可以用于許多其他用途,包括網絡服務器、人工智能、數據科學等。其易讀性和簡潔性使其成為一種受歡迎的編程語言,適合初學者和專業程序員。