自学Python-入门001

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值