Python is a powerful and flexible programming language, and one reason for its widespread use is its rich standard library and third-party modules. In this article, I will introduce some commonly used Python modules and provide some specific code examples to facilitate readers' understanding and application.
import datetime # 获取当前日期和时间 current_time = datetime.datetime.now() print("当前日期和时间:", current_time) # 格式化日期和时间字符串 formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S") print("格式化后的日期和时间:", formatted_time)
import os # 创建文件夹 folder_name = "test_folder" os.mkdir(folder_name) print("文件夹已创建:", folder_name) # 删除文件夹 os.rmdir(folder_name) print("文件夹已删除:", folder_name)
import random # 生成随机整数 random_number = random.randint(1, 10) print("随机整数:", random_number) # 随机选择列表中的元素 fruits = ["apple", "banana", "orange", "grape"] random_fruit = random.choice(fruits) print("随机水果:", random_fruit)
import re # 查找匹配的模式 string = "Hello, world!" pattern = r"world" match = re.search(pattern, string) print("匹配的模式位置:", match.start(), "-", match.end())
Here is just an introduction to some commonly used Python modules. Of course, there are many other modules, such as math, json, multiprocessing, etc. Each module has its own specific functions and uses. By utilizing these modules, we can handle various tasks more easily in Python and improve the efficiency and quality of our code.
Summary: Python’s standard library and third-party modules provide a wealth of functions that can meet a variety of programming needs. Mastering the use of common modules can help us better develop Python programs. The above examples are only a small part of them. Readers can continue to expand their knowledge and skills by consulting official documents and learning more practices.
The above is the detailed content of What are the common Python modules?. For more information, please follow other related articles on the PHP Chinese website!