本节关于控制流以及函数定义
python:
if true:
a+b
elif:
a-b
else:
a == b
powershell
if(true){
$a+$b
}
elseif{
$a-$b
}
else{
$a -eq $b
}
python:
while a == b :
a+b
break
continue
else :
print("hello world ")
powershell
while($a -eq $b ){
a+b
break
continue
}
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
}
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',)