📅  最后修改于: 2023-12-03 15:21:31.100000             🧑  作者: Mango
在编程世界中,我们常常需要进行各种各样的计算。下面将会介绍一些常见的计算范例,并提供相应的代码片段。
import math
x = 16
result = math.sqrt(x)
print(result) # 4.0
import math
x = math.pi/4
result = math.sin(x)
print(result) # 0.7071067811865475
x = 2
y = 3
result = x**y
print(result) # 8
x = "Hello"
y = "World"
result = x + " " + y
print(result) # "Hello World"
x = "abcde"
result = x[::-1]
print(result) # "edcba"
x = "hello world"
result = len(x)
print(result) # 11
x = [3, 1, 4, 1, 5, 9, 2, 6, 5]
result = sorted(x)
print(result) # [1, 1, 2, 3, 4, 5, 5, 6, 9]
x = [3, 1, 4, 1, 5, 9, 2, 6, 5]
result = list(set(x))
print(result) # [1, 2, 3, 4, 5, 6, 9]
x = [1, 2, 3, 4, 5]
result = sum(x)
print(result) # 15
with open("file.txt", "r") as f:
result = f.read()
print(result)
with open("file.txt", "w") as f:
f.write("Hello World")
以上就是一些常见的计算范例,希望对您的编程工作有所帮助。