python基础操作代码片
Easul Lv4

函数设置参数与返回值类型

折叠代码块PYTHON 复制代码
1
2
3
4
5
6
7
# 参数列表,前边是变量,后边是类型,用:隔开
# 如果需要设置默认值,需要在类型后用 = 连接默认值
# 使用 -> 来指示返回值类型
def test(sql: str, type: int = 1) -> list:
print(sql)
print(type)
return [1, 2]

for循环获取键值

参考

遍历

折叠代码块PYTHON 复制代码
1
2
3
4
5
6
# 遍历下标与值
for index, value in enumerate(arrList):
print(f"{index}:{value}")
# 遍历字典
for key in dicts:
print(dicts[key])

字符串

折叠代码块PYTHON 复制代码
1
2
3
4
5
6
7
8
def strOperation():
first = "1"
second = "asdf"
str1 = f"{first}: {second}"
str2 = "{}:{}".format(first, second)
print(str2)

strOperation()

全局变量

折叠代码块PYTHON 复制代码
1
2
3
4
5
hello = "asdf"
def test():
# 函数里边用外边的变量需要使用global
global hello
print(hello)

list

折叠代码块PYTHON 复制代码
1
2
3
4
# 获取list的切片的下标1-2的元素
newList = oldList[1: 3]
# 清空list
newList.clear()
 评论
来发评论吧~
Powered By Valine
v1.5.2