本文记录了初学
python
常用的两则实用技巧,分享给大家供大家参考之用。具体如下:
1.可变参数
示例代码如下:
由于在args变量前有*前缀,所有多余的函数参数都会作为一个元组存储在args中。如果使用的是**前缀,多余的参数则会被认为是一个字典的键/值对。
2.exec语句将字符串str当成有效Python代码来执行。
execfile(filename [,globals [,locals ]])函数可以用来执行一个文件。
Python 3.14.3
微软官方的 Python 扩展,是 VS Code 安装量最高的扩展(209M+)。集成 IntelliSense(通过 Pylance)、调试(通过 Python Debugger)、代码检查、格式化、重构和单元测试等功能。支持 Jupyter Notebook、虚拟环境管理和多 Python 版本切换。
下载
立即学习
“
Python免费学习笔记(深入)
”;
示例代码如下:
希望本文所述对大家的Python程序设计有所帮助。
>>> def powersum(power, *args):
... '''''Return the sum of each argument raised to specified power.'''
... total = 0
... for i in args:
... total += pow(i, power)
... return total
...
>>> powersum(2, 3, 4)
25
>>> powersum(2, 10)
100
>>> exec 'print "Hello World"'
Hello World>>> execfile(r'c:\test.py')
hello,world!
