x#

python学习之路-day3

点点圈 提交于 2020-07-29 06:42:36
1.函数   代码: def test(x,y): print (x) print (y) test(y =2,x=1) # 关键字参数 与形参顺序无关 test(1,2) # 位置参数 与形参一一对应 test(1,y=2) # 如果有多个参数,关键字参数要放在位置参数后面 View Code 2.进度条 import sys,time # 导入包 for i in range(20 ): sys.stdout.write( " # " ) # 终端打印# sys.stdout.flush() # 立即写入内存 time.sleep(0.1) # 控制时间0.1S View Code 3.局部变量与全局变量 x=0 # 全局变量 def test(): x =1 # 局部变量 只作用于当前函数内(只有字符串和整数不能修改,对于数组或字典等复杂的对象可以修改) print (x) print (x) def test_2(): global x # 使用global 方法将局部变量声明成全局变量 x=2 print (x) test() test_2() View Code 来源: oschina 链接: https://my.oschina.net/u/4313107/blog/4338283