-
输入输出
- 读取整数:
int(input())
- 读取一行:
input().strip()
- 输出格式化浮点数:
print('{:.2f}'.format(float_var))
- 读取整数:
-
数组和列表
- 创建列表:
my_list = [1, 2, 3]
- 列表切片:
subset = my_list[start:end]
- 列表反转:
reversed_list = my_list[::-1]
- 创建列表:
-
字符串处理
- 字符串拼接:
result = str1 + str2
- 字符串查找:
index = my_string.find('substring')
- 字符串反转:
reversed_str = my_string[::-1]
- 字符串拼接:
-
条件语句
- if-else 语句:
pythonCopy code
if condition: # code block else: # code block
- if-else 语句:
-
循环结构
- for 循环:
pythonCopy code
for item in my_list: # code block
- while 循环:
pythonCopy code
while condition: # code block
- for 循环:
-
数学运算
- 绝对值:
abs_value = abs(number)
- 幂运算:
result = base ** exponent
- 向下取整:
floor_value = math.floor(float_var)
- 绝对值:
-
集合和字典
- 创建字典:
my_dict = {'key': 'value'}
- 字典遍历:
for key, value in my_dict.items():
- 集合操作:
intersection = set1 & set2
- 创建字典:
-
文件操作
- 文件读取:
pythonCopy code
with open('filename.txt', 'r') as file: content = file.read()
- 文件读取:
-
排序算法
- 列表排序:
sorted_list = sorted(my_list)
- 自定义排序:
sorted_list = sorted(my_list, key=lambda x: x[1])
- 列表排序:
-
递归
- 递归函数:
pythonCopy code
def recursive_function(param): # base case if condition: return something # recursive case else: return recursive_function(modified_param)
- 递归函数:
2023亚太赛论文模板+50个常用代码分享
最新推荐文章于 2024-07-14 23:58:50 发布