1.编译器
用Chocolatey(Windows系统上的包管理器)安装python环境,Chocolatey可以自动配置环境变量。
Viasual Studio Code+Python扩展
2.基础语法
2.1 if语句
if x < 0:
x = 0
print('negative changed to zero')
elif x == 0:
print('Zero')
elif x== 1:
print('Single')
else:
print('More')
2.2 for语句
a = ['cat','window','defenstrate']
for x in a:
print(x,len(x))
for i in range(5):
print(i)
for n in range(2,10):
for x in range(2,n):
if n % x == 0:
print(n, 'equals', x,'*',n//x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')
循环语句可以有一个 else 子句; 该子句会在以下情况被执行: 循环因迭代到列表末尾而终止 (for 语句), 或者, 当循环条件为假 (while 语句), 同时它不会在循环因 break 语句终止的情况下被执行
2.3 range()函式
range(5,10) ->出力5~10,类似vba的矩阵选择
range(5,20,3)->每隔3个出力,5,8,11,14,17
2.4 pass语句
pass语句,类似于plsql中的null,什么都不做
2.5 定义函式
def fib(n):
a,b = 0,1
while a<n:
print(a,end=' ')
a,b=b,a+b
print()
#调用方式
fib(2000)
2.6 深入函式定义
def ask_ok(prompt,retries=4,complaint='Yes or no, please!'):
默认参数
ask_ok('Do you really want to quit?')
ask_ok('Ok to overwrite the file?',2)
ask_ok('Ok to overwrite the file?',2,'Come on, only yes to no!')
关键字参数
ask_ok(retries=2,complaint=‘Come on, only yes to no!’)
下列情况非法
无参数,关键字参数和非关键字参数混在,同一个参数给多个值,未知关键字
2.7 任意参数
def ask_ok(*args)