for i in iterable_object:
# do something
pass
else:
# for 语句块中没有执行过break的时候执行的代码块
pass
var = input("please enter a number:\n")
if var == '1':
# case '1':
pass
elif var == '2':
# case '2':
pass
elif var == '3':
# case '3'
pass
else:
# default
pass
def fun([args[, option[, *args[, **kwargs]]]]):
pass
def fun1():
print("hello world")
f = fun1
f()
a = 2
print(lambda a: a**2)
'''brief
details
'''
def fun(*args, **kwargs):
'''brief
details
'''
# do something
pass
def fun(arg1: int, arg2: list) -> int:
""" docstring """
# do something
return 1
li = [i for i in range(1, 100, 2)]
li = [i for i in li if i % 3 == 0]
li = [[i*j for i in range(1, 10)] for j in range(1, 10)]
from xxx improt *
python filename.py [args]
python -m filename.py [args]
python -m filename.py
动态添加搜索目录:
import sys
sys.path.append("module\_dir")
with open("file.txt", "r") as f:
f.readlines()
格式化输出
try:
# 尝试执行的语句块
pass
except Error as err: # 错误/异常类型
# 异常处理代码块, try代码块发生错误时执行
pass
else:
# try代码块未发生错误时执行的代码块
pass
finally:
# 无论错误是否发生都会执行的代码块
pass
强制抛出一个异常:
raise instance_of_exceprion_or_error
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。