【ps VS python对比】python入门学习(三)

本节关于控制流以及函数定义

条件分支

python:

if  true:
	a+b 
elif:
	a-b 
else:
	a == b 

powershell

if(true){
	$a+$b
}
elseif{
	$a-$b
}
else{
	$a -eq $b 
}

while 条件循环

python:

while a == b :
	a+b
	break 
	continue
else :
	print("hello world ")

powershell

while($a -eq $b ){
	a+b
	break
	continue
} 

for 循环

python:

mydict= {1:'mon', 'line1':3332}
for k,v in mydict.items():
    print(k,v )
for x in range(5,20):  
	print(x)

powershell

$a =(1,2,3,4,5,6,7,8)
foreach($x in $a ){
    $x
}

foreach($x in 10..45){
	$x
} 

def /方法

python:

outsider = 5
def hello (a,b ):
    global insider   #内部定义的全局变量
    insider = b
    result = a + b + outsider
    insider2 = 5    #局部变量,函数外无法访问
    return result
print(hello(4,555))
print(insider)

print("#map(def_name,paralist1,paralist2... )")
hello1 = [1,2,3,4,5]    #两个list 长度不一致,
hello2 = [12,34,56,78]
print(map(hello,hello1,hello2))
print(list(map(hello,hello1,hello2)))
print("#lambda表达式,最终会返回一个匿名函数")

print(map(lambda a: a*3, hello1))
print(list( map(lambda a: a*3, hello1)))

==== RESTART: C:/Users/noaca/AppData/Local/Programs/Python/Python38/hello.py ===
564
555
#map(def_name,paralist1,paralist2... )
<map object at 0x00000195DA992460>
[18, 41, 64, 87]
#lambda表达式,最终会返回一个匿名函数
<map object at 0x000002A13877DBB0>
[3, 6, 9, 12, 15]

# def 固定参数和顺序定义
def s_fun(a, b, c=44):   
    return a +b +c
print(s_fun(1,2))
print(s_fun(1,2,3))
==== RESTART: C:/Users/noaca/AppData/Local/Programs/Python/Python38/hello.py ===
47
6

#def可变参数定义
def s_fun1(*a):   #无名的可变参数
    result = 0 
    for x in zip(a):
        print(x[0])
        
def s_fun2(*a):   #无名的可变参数
    result = 0 
    for x in zip(a):
        print(x)
        

s_fun1(1,2,3,"abcd")
        
s_fun2(1,2,3,"abcd")

#def可变参数定义
def v_fun1(**a):   #有名的可变参数,字典
    result = 0 
    for x in zip(a):
        print(x[0])
        
def v_fun2(**a):   
    result = 0 
    for x in zip(a):
        print(x)
        

v_fun1(gg='no1',gte2=5,syss=55,abcd='cc')
        
v_fun2(gg='no1',gte2=5,syss=55,abcd='cc')
==== RESTART: C:/Users/noaca/AppData/Local/Programs/Python/Python38/hello.py ===
1
2
3
abcd
(1,)
(2,)
(3,)
('abcd',)
gg
gte2
syss
abcd
('gg',)
('gte2',)
('syss',)
('abcd',)

powershell
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值