一、Python基礎語法
Python是一門高級編程語言,它的語法簡單易懂,學習起來也比較簡單,不需要太多的編程基礎。Python的基礎語法包括變量、數據類型、運算符、控制語句等,下面我們來具體看看。
1. 變量
<html>
<head>
<title>Python 變量</title>
</head>
<body>
<?php
# 定義變量x和y
x = 6
y = 7
# 輸出變量x和y的值
print("x 的值為:", x)
print("y 的值為:", y)
?>
</body>
</html>
2. 數據類型
Python支持多種數據類型,包括數字、列表、元組、字典等。下面是常見的數據類型以及對應的代碼。
2.1 數字類型
<html>
<head>
<title>Python 數字類型</title>
</head>
<body>
<?php
# 定義數字
num1 = 5
num2 = 3.14
num3 = 100.2j
# 輸出數字類型和對應的值
print(type(num1), num1)
print(type(num2), num2)
print(type(num3), num3)
?>
</body>
</html>
2.2 列表類型
<html>
<head>
<title>Python 列表類型</title>
</head>
<body>
<?php
# 定義列表
list1 = [1, 2, 3, 4]
list2 = ["apple", "banana", "cherry"]
# 輸出列表類型和對應的值
print(type(list1), list1)
print(type(list2), list2)
?>
</body>
</html>
3. 運算符
Python支持多種運算符,包括算術運算符、比較運算符、邏輯運算符等。下面是常見的運算符以及對應的代碼。
3.1 算術運算符
<html>
<head>
<title>Python 算術運算符</title>
</head>
<body>
<?php
# 定義變量x和y
x = 10
y = 3
# 輸出計算結果
print("x + y = ", x + y)
print("x - y = ", x - y)
print("x * y = ", x * y)
print("x / y = ", x / y)
print("x % y = ", x % y)
print("x ** y = ", x ** y)
print("x // y = ", x // y)
?>
</body>
</html>
3.2 比較運算符
<html>
<head>
<title>Python 比較運算符</title>
</head>
<body>
<?php
# 定義變量x和y
x = 6
y = 4
# 輸出比較結果
print("x > y is ", x > y)
print("x < y is ", x = y is ", x >= y)
print("x <= y is ", x <= y)
?>
</body>
</html>
3.3 邏輯運算符
<html>
<head>
<title>Python 邏輯運算符</title>
</head>
<body>
<?php
# 定義變量x、y和z
x = 8
y = 3
z = 5
# 輸出邏輯運算結果
print("x > y and x > z is ", x > y and x > z)
print("x > y or x > z is ", x > y or x > z)
print("not x > y is ", not x > y)
?>
</body>
</html>
4. 控制語句
Python支持多種控制語句,包括if語句、for循環、while循環等。下面是常見的控制語句以及對應的代碼。
4.1 if語句
<html>
<head>
<title>Python if語句</title>
</head>
<body>
<?php
# 定義變量x和y
x = 6
y = 4
# if語句判斷
if x > y:
print("x 比 y 大")
elif x < y:
print("x 比 y 小")
else:
print("x 和 y 相等")
?>
</body>
</html>
4.2 for循環
<html>
<head>
<title>Python for循環</title>
</head>
<body>
<?php
# 定義列表
fruits = ["apple", "banana", "cherry"]
# for循環輸出列表中的值
for fruit in fruits:
print(fruit)
?>
</body>
</html>
4.3 while循環
<html>
<head>
<title>Python while循環</title>
</head>
<body>
<?php
# 定義變量i
i = 1
# while循環輸出1到5的數字
while i <= 5:
print(i)
i += 1
?>
</body>
</html>
二、Python標準庫
Python標準庫是Python內置的模塊庫,包括文件操作、網絡編程、正則表達式等多種功能。下面我們來具體看看。
1. 文件操作
Python的文件操作可以使用內置函數open()來打開文件,使用內置函數close()來關閉文件,使用read()和write()函數來讀取和寫入文件內容。
<html>
<head>
<title>Python 文件操作</title>
</head>
<body>
<?php
# 打開文件
f = open("filename.txt", "w")
# 往文件中寫入內容
f.write("Hello, Python")
# 關閉文件
f.close()
?>
</body>
</html>
2. 網絡編程
Python的網絡編程可以使用內置的socket模塊來實現基於TCP或UDP的套接字通信。
<html>
<head>
<title>Python 網絡編程</title>
</head>
<body>
<?php
import socket
# 創建一個套接字對象
s = socket.socket()
# 連接服務端
s.connect(("www.baidu.com", 80))
# 發送數據
s.send(b"GET / HTTP/1.1\r\n\r\n")
# 接收數據
data = s.recv(1024)
# 輸出數據
print(data.decode())
# 關閉套接字
s.close()
?>
</body>
</html>
3. 正則表達式
Python的正則表達式可以使用內置的re模塊來進行文本匹配和替換。
<html>
<head>
<title>Python 正則表達式</title>
</head>
<body>
<?php
import re
# 匹配字符串中的數字
pattern = r'\d+'
# 查找匹配的結果
result = re.findall(pattern, "hello123world456")
# 輸出匹配的結果
print(result)
?>
</body>
</html>
三、Python高級特性
Python的高級特性包括列表推導式、生成器、裝飾器等多種功能,可以提高代碼的效率和可讀性。
1. 列表推導式
Python的列表推導式可以簡化代碼,將循環和if語句合併到一個列表中。
<html>
<head>
<title>Python 列表推導式</title>
</head>
<body>
<?php
# 定義列表
list1 = [1, 2, 3, 4, 5]
# 使用列表推導式篩選出偶數
even_list = [x for x in list1 if x % 2 == 0]
# 輸出篩選結果
print(even_list)
?>
</body>
</html>
2. 生成器
Python的生成器可以按需生成數據,減少內存佔用。
<html>
<head>
<title>Python 生成器</title>
</head>
<body>
<?php
# 定義生成器
def generator():
for i in range(5):
yield i
# 輸出生成器中的值
for x in generator():
print(x)
?>
</body>
</html>
3. 裝飾器
Python的裝飾器可以在不修改函數代碼的前提下,給函數添加額外的功能。
<html>
<head>
<title>Python 裝飾器</title>
</head>
<body>
<?php
# 定義裝飾器
def my_decorator(func):
def wrapper():
print("Before function is called.")
func()
print("After function is called.")
return wrapper
# 定義函數並使用裝飾器
@my_decorator
def say_hello():
print("Hello, Python!")
# 調用函數
say_hello()
?>
</body>
</html>
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/239307.html