Python是一种强大的编程语言,用于各种计算和数据处理任务。以下是一些常用的Python计算方法:
全部都运行过的
a + b
a - b
a * b
a / b
a % b
a ** b
a = 5
b = 2
result = a + b
print(result)
math
模块中。例如,可以使用math.sqrt()
计算平方根,使用math.sin()
计算正弦值等。import math
x = 16
square_root = math.sqrt(x)
print(square_root)
random
模块可以生成随机数。import random
random_number = random.randint(1, 100)
print(random_number)
str1 = "Hello"
str2 = " World"
result = str1 + str2
print(result)
my_list = [1, 2, 3, 4, 5]
my_list.append(6) # 添加元素
print(my_list)
if
、elif
和else
关键字进行条件判断。x = 10
if x > 0:
print("x是正数")
elif x < 0:
print("x是负数")
else:
print("x是零")
for
和while
循环进行迭代操作。for i in range(5):
print(i)
counter = 0
while counter < 5:
print(counter)
counter += 1
bin()
、oct()
和hex()
函数将数字转换为相应进制的字符串。decimal_number = 15
binary_representation = bin(decimal_number)
octal_representation = oct(decimal_number)
hexadecimal_representation = hex(decimal_number)
print(f"Binary: {binary_representation}")
print(f"Octal: {octal_representation}")
print(f"Hexadecimal: {hexadecimal_representation}")
abs()
函数可以得到一个数的绝对值。number = -10
absolute_value = abs(number)
print(absolute_value)
round()
函数进行四舍五入。floating_point_number = 3.14159
rounded_number = round(floating_point_number, 2) # 保留两位小数
print(rounded_number)
max()
和min()
函数找到一组数字的最大值和最小值。numbers = [2, 7, 1, 9, 4]
max_value = max(numbers)
min_value = min(numbers)
print(f"Max: {max_value}, Min: {min_value}")
math
模块提供了radians()
和degrees()
函数,用于将角度转换为弧度,以及将弧度转换为角度。import math
angle_in_degrees = 90
angle_in_radians = math.radians(angle_in_degrees)
print(f"{angle_in_degrees} degrees is {angle_in_radians} radians")
angle_in_radians = math.pi / 2
angle_in_degrees = math.degrees(angle_in_radians)
print(f"{angle_in_radians} radians is {angle_in_degrees} degrees")
0b
前缀表示二进制。binary_number = 0b1101
print(binary_number) # 输出13(十进制)
0o
前缀表示八进制。octal_number = 0o17
print(octal_number) # 输出15(十进制)
0x
前缀表示十六进制。hexadecimal_number = 0xA
print(hexadecimal_number) # 输出10(十进制)
int()
函数将其转换为十进制。同样,使用bin()
、oct()
和hex()
函数将十进制数字转换为二进制、八进制和十六进制。decimal_number = 42
binary_representation = bin(decimal_number)
octal_representation = oct(decimal_number)
hexadecimal_representation = hex(decimal_number)
print(f"Binary: {binary_representation}")
print(f"Octal: {octal_representation}")
print(f"Hexadecimal: {hexadecimal_representation}")
datetime
模块的 datetime
类,可以获取当前的日期和时间。from datetime import datetime
current_time = datetime.now()
print(current_time)
timedelta
类来表示时间差。from datetime import datetime, timedelta
current_time = datetime.now()
future_time = current_time + timedelta(days=7)
time_difference = future_time - current_time
print(f"Future time: {future_time}")
print(f"Time difference: {time_difference}")
from datetime import datetime
# 时间对象转换为字符串
current_time = datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print(f"Formatted time: {formatted_time}")
# 字符串转换为时间对象
time_string = "2022-01-01 12:30:00"
converted_time = datetime.strptime(time_string, "%Y-%m-%d %H:%M:%S")
print(f"Converted time: {converted_time}")
pytz
。# 需要先pip install pytz安装
from datetime import datetime
import pytz
# 获取带时区信息的时间
current_time_utc = datetime.now(pytz.utc)
print(f"Current time in UTC: {current_time_utc}")
# 转换时区
eastern_timezone = pytz.timezone('US/Eastern')
current_time_eastern = current_time_utc.astimezone(eastern_timezone)
print(f"Current time in Eastern Time: {current_time_eastern}")
datetime
模块的 date
类,可以获取当前的日期。from datetime import date
current_date = date.today()
print(current_date)
date
类来创建特定的日期。from datetime import date
specific_date = date(2022, 1, 1)
print(specific_date)
date
类和 timedelta
类来计算日期之间的差异。from datetime import date, timedelta
current_date = date.today()
future_date = current_date + timedelta(days=30)
date_difference = future_date - current_date
print(f"Future date: {future_date}")
print(f"Date difference: {date_difference.days} days")
from datetime import date
# 日期对象转换为字符串
current_date = date.today()
formatted_date = current_date.strftime("%Y-%m-%d")
print(f"Formatted date: {formatted_date}")
# 字符串转换为日期对象
date_string = "2022-01-01"
converted_date = date.fromisoformat(date_string)
print(f"Converted date: {converted_date}")
25.日期加法:
from datetime import datetime, timedelta
current_date = datetime.now()
days_to_add = 5
new_date = current_date + timedelta(days=days_to_add)
print(f"Current date: {current_date}")
print(f"New date after adding {days_to_add} days: {new_date}")
26.日期减法:
from datetime import datetime, timedelta
current_date = datetime.now()
days_to_subtract = 3
new_date = current_date - timedelta(days=days_to_subtract)
print(f"Current date: {current_date}")
print(f"New date after subtracting {days_to_subtract} days: {new_date}")
27.结合日期和时间的加法和减法:
from datetime import datetime, timedelta
current_datetime = datetime.now()
hours_to_add = 2
new_datetime = current_datetime + timedelta(hours=hours_to_add)
print(f"Current datetime: {current_datetime}")
print(f"New datetime after adding {hours_to_add} hours: {new_datetime}")