Python是一种高级编程语言,以其简洁明了的语法和强大的功能受到广大程序员的喜爱,Python的设计哲学强调代码的可读性和简洁的语法(尤其是使用空格缩进划分代码块,而非使用大括号或者关键词),Python的标准库非常庞大,可以支持很多任务,如网页爬取、数据分析、机器学习等。

一、Python基础语法

1、变量和数据类型

Python中有多种数据类型,包括整数(int)、浮点数(float)、字符串(str)、列表(list)、元组(tuple)、字典(dict)等,变量是存储数据的标识符,可以通过赋值语句来给变量赋值。

a = 10  # 整数
b = 3.14  # 浮点数
c = "Hello, Python"  # 字符串
d = [1, 2, 3]  # 列表
e = (1, 2, 3)  # 元组
f = {"name": "Tom", "age": 18}  # 字典

2、控制结构

Python中的控制结构包括条件语句(if...elif...else)和循环语句(for和while)。

# 条件语句
if a > b:
    print("a大于b")
elif a < b:
    print("a小于b")
else:
    print("a等于b")

# 循环语句
for i in range(5):
    print(i)

while a > 0:
    print(a)
    a -= 1

3、函数

函数是一段具有特定功能的代码块,可以接受输入参数并返回结果,可以使用def关键字来定义函数。

def add(a, b):
    return a + b

result = add(1, 2)
print(result)

二、Python应用实例

Python基础语法及应用

1、文件操作

Python提供了丰富的文件操作功能,如打开文件、读取文件内容、写入文件等。

# 打开文件
file = open("test.txt", "r")
content = file.read()
print(content)
file.close()

# 写入文件
file = open("test.txt", "w")
file.write("Hello, Python!")
file.close()

2、网络编程

Python提供了socket模块来实现网络编程,如创建TCP客户端和服务器。

import socket

# 创建TCP服务器
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 8888))
server.listen(5)
while True:
    client, address = server.accept()
    print("连接地址:", address)
    client.send("Hello, Python!".encode())
    client.close()

3、数据分析与可视化

Python提供了NumPy、Pandas等库来进行数据分析,Matplotlib、Seaborn等库来进行数据可视化。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets, linear_model, metrics, model_selection, preprocessing, tree, ensemble, naive_bayes, neighbors, svm, neural_network, discriminant_analysis, statistics, utility, impute, pipeline, externals, inspection, io, decomposition, cluster, dimensionality, manifold, optimization, kernel_minding, models, graph, bioinformatics, image, text, nlp, quantum, deeplearning, integration, bayesian_optimization, resampler, grid_search, metrics as metrics_module, base as base_module, algorithms as algorithms_module, ensemble as ensemble_module, discriminant_analysis as discriminant_analysis_module, externals as externals_module, inspection as inspection_module, io as io_module, kernel_minding as kernel_minding_module, libraries as libraries_module, math as math_module, models as models_module, neural_network as neural_network_module, optimization as optimization_module, preprocessing as preprocessing_module, quantum as quantum_module, regression as regression_module, scikit_learn as scikit_learn_module, statsmodels as statsmodels_module, timeseries as timeseries_module, tree as tree_module, visualization as visualization_module from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score from sklearn.tree import DecisionTreeClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.naive_bayes import GaussianNB from sklearn.ensemble import VotingClassifier from sklearn.preprocessing import StandardScaler from sklearn.pipeline import make_pipeline from sklearn.externals import joblib from sklearn.inspection import permutation_importance from sklearn.impute import SimpleImputer from sklearn.cluster import KMeans from sklearn.decomposition import PCA from sklearn.manifold import TSNE from sklearn.metrics import confusion_matrix from sklearn.metrics import classification_report from sklearn.metrics import roc_curve from sklearn.metrics import auc from matplotlib import style style.use('ggplot') data = load_iris() X = data["data"] y = data["target"] X_train, X_test, y_train, y_test = train_test_split(X, y) clf1 = LogisticRegression() clf2 = DecisionTreeClassifier() clf3 = KNeighborsClassifier() clf4 = SVC() clf5 = GaussianNB() clf6 = VotingClassifier([('lr', clf1), ('dt', clf2), ('knn', clf3), ('svc', clf4), ('gnb', clf5)], voting='soft') pipe1 = make_pipeline(StandardScaler(), clf6) pipe2 = make_pipeline(SimpleImputer(), pipe1) pipe3 = make_pipeline(PCA(), pipe2) pipe4 = make_pipeline(TSNE(), pipe3) pipe5 = make_pipeline(KMeans(), pipe4) pipe6 = make_pipeline(permutation_importance(), pipe5) results = [] for clf in [clf1, clf2, clf3]: results.append(clf.fit(X_train[y == i], y[y == i])) for clf in [clf4]: results.append(clf.fit(X[y == i], y[y == i])) for clf in [clf5]: results.append(clf.fit(X[y == i], y[y == i])) for clf in [pipe1]: results.append(clf.fit(X[y == i], y[y == i])) for clf in [pipe2]: results.append(clf.fit(X[y == i], y[y == i])) for clf in [pipe3]: results.append(clf.fit(X[y == i], y[y == i])) for clf in [pipe4]: results.append(clf.fit(X[y == i], y[y == i])) for clf in [pipe5]: results.append(clf.fit(X[y == i], y[y == i])) for clf in [pipe6]: results.append(clf.fit(X[y == i], y[y == i])) print("Accuracy Score: %s" % accuracy_score(y_test, predictions)) print("Confusion Matrix: 
%s" % confusion_matrix(y_test, predictions)) print("Classification Report: 
%s" % classification_report(y_test)) plt.figure() plt.title("ROC Curve") plt.plot([0, 1], [0, 1], 'k--') plt.plot([0, 1], [0.75 * np.ones((len(roc),))]) plt.xlim([0.0, 1]) plt.ylim([0.0, 1]) plt.show() plt.figure() plt.title("AUC") plt.plot([0, 1], [0.75 * np.ones((len(roc),))]) plt.show() plt.figure() plt.title("Decision Tree Classifier") tree = DecisionTreeClassifier() tree = tree.fit(X[y == i], y[y == i]) plottedGraphic = tree.plotGraphic() plottedGraphic[0].show() plt