配合以前两篇文章使用:
python易忘操作和小知识点集锦
常用算法模板与知识点
使用 Python 3.x
一、小功能
# 把数字转换为货币字符串
import locale
# Set the locale to United States
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
# Example number
amount = 1234567.89
# Format as currency string
formatted_amount = locale.currency(amount, grouping=True)
# Display the formatted currency string
print(f'Formatted Amount: {
formatted_amount}')
# 生成随机字符串
import random
import string
def generate_random_string(length):
characters = string.ascii_letters + string.digits
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
# Generate a random string of length 8
random_string = generate_random_string(8)
# Display the random string
print(f'Random String: {
random_string}')
# Base64编码字符串
import base64
# Original string
original_string = 'Hello, 你好!'
# Encode the string to Base64
base64_encoded_string = base64.b64encode(original_string.encode('utf-8')).decode('utf-8')
# Display the result
print(f'Original String: {
original_string}')
print(f'Base64 Encoded String: {
base64_encoded_string}')
# 格式化时间
from datetime import datetime
# Get the current date
current_date = datetime.utcnow()
# Format the date
formatted_date = current_date.strftime('%A, %B %d, %Y')
# Display the result
print(f'Formatted Date: {
formatted_date}')
# 显示当前时间
# Get the current date
current_date = datetime.now()
# Format the current date as a string
formatted_date = current_date.strftime('%m/%d/%Y') # Adjust the format as needed
# Display the result
print(f'Current Date: {
formatted_date}')
# 比较两个时间
# Example dates
date1 = datetime.strptime('2022-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2023-01-01', '%Y-%m-%d')
# Compare dates
if date1 < date2:
print(f'{
date1} is earlier than {
date2}')
elif date1 > date2:
print(f'{
date1} is later than {
date2}')
else:
print(f'{
date1} is equal to {
date2}')
# 获取时间戳
current_date = datetime.now()
numeric_date = int(current_date.timestamp() * 1000) # 毫秒时间戳
# 从数组中移除值
# Example list
original_list = [1, 2, 3, 4, 5]
item_to_remove = 3
# Find the index of the item to remove
try:
index_to_remove = original_list.index(item_to_remove)
# Remove the item from the list
original_list.pop(index_to_remove)
print('Original List:', original_list)
except ValueError:
print('Item not found in the list.')
# 从数组中随机取值
import random
# Example list
my_list = [1, 2, 3, 4, 5, 6, 7, 8

最低0.47元/天 解锁文章
596

被折叠的 条评论
为什么被折叠?



