Python 函式 練習

2022-09-11 21:42:32 字數 3594 閱讀 6531

def sum(*args):

val =0

for i in

args:

val = i+val

return val

a = 10

b = 20 

def test(a, b):

print(a, b)

c = test(b,a) 

print(c)

a = 20, b = 10 , c =none

因為函式中沒有return, 因此返回的是none

a = 10

b = 20 

def test(a, b):

a = 3

b = 5

print(a, b) #a =3,b =5

c = test(b,a)

print(c) #c=none

print(a, b) #a = 10 , b = 20

a = 10 b = 20, c =none

將每個實參的每個元素依次新增到函式的動態傳參args裡面

eg:傳入函式兩個引數【1,2,3】(22,33)最終args為(1,2,3,22,33)

1 list = [1,2,3]

2 tuple = (22,33)

3def test(*args):

4print

(args)

5 test(*list,*tuple)

1 dic = 

2 dic1 =

3def test(**kwargs):

4print

(kwargs)

5 test(**dic,**dic1)

6.1a = 2

print(a)

##成立

6.2a = 2

a += 1

print(a)

##不成立,因為函式中的a是全域性變數,不能直接賦值,需要在函式中輸入global a **

6.3   

a = 1

def inner():

print(a)

inner()

##成立,看沒問題,改的話就會報錯

6.4a = 1

def inner():

a += 1

print(a)

inner()

##報錯,因為直接在函式內的函式修改上外面函式的變數,因此在內部函式中新增 nonlocal a **即可

1

defmy_min(a, b):

2return a if a > b else

b3 mmin = my_min(103,203)

4print(mmin)

eg:傳入的可迭代物件為[1,"python", "sun wu kong "]返回的結果為'1_python_sun wu kong '

def  func(it):

result = ""

for el in

it: result += str(el) + "_"

return result[:-1

]print(func([

1,"asd

","23

"]))

def func(*n):

return

def

cal(n):

if n == 1:

return

n

return n * cal(n-1)

print(cal(3))

def

puke():

hua = ["

紅心","

梅花","

黑桃","方塊"

] dian = ["

a","

2","

3","

4","

5","

6","

7","

8","

9","

10","

j","

q","k"

] result =

for huase in

hua:

for dianshu in

dian:

return result

def inner():

print(666)

inner()

用兩種以上的方式,執行inner()函式

#

# 第一種

def  

definner():

print(666)

inner()

## 第二種

def  

definner():

print(666)

return

inner

fn()

def calc(a, b, c, d=1, e=2):

return (a+b)*(c-d)+e

請分別寫出下列標號**的輸出結果,如果出錯請寫出error。

print(calc(1,2,3,4,5))## (1+2)*(3-4)+5

print(calc(1,2))  ## error

print(calc(e=4,c=5,a=2,b=3)) ## (2+3)*(5-1)+4

print(calc(1,2,3)) ## (1+2)*(3-1)+2

print(calc(1,2,3,e=4)) ## (1+2)*(3-1)+4 

print(calc(1,2,3,d=5,4)) ##error

def extendlist(val,list=):## 預設值在記憶體中只會產生乙份,(位址)

return list

list1 = extendlist(10)   ## 預設值位址列表

list2 = extendlist(123,) ## 產生新的位址列表

list3 = extendlist("a")   ## 預設值位址列表,既 list1 和 list 3 相同

print("list1=%s"%list1)  ## list1 = ["10","a"] 

print("list2=%s"%list2) ## list2 = ["123"]

print("list3=%s"%list3) ## list3 = ["10","a"]

測試 ## print(id(list1))

## print(id(list3))

for i in range(1,10):

for j in range(1,i+1):

print("

%s*%s=%s

"%(i,j,i*j),end=""

)

print() #

#print()預設值end = "\n"

Python函式練習

def upperandlowercount str upper count,lower count 0,0for i in str if i.isupper upper count 1elif i.islower lower count 1else continue return upper co...

Python練習 高階函式

當乙個函式的引數,傳的是另乙個函式的時候,這樣的函式就是高階函式 變數可以指向函式,如果乙個變數指向了乙個函式,那麼,可通過該變數來呼叫這個函式 ap 函式接收兩個引數,乙個是函式,乙個是iterable,map將傳入的函式依次作用到序列的每個元素,並把結果作為新的iterator返回。def f ...

(十二)Python函式練習

函式 定義函式 defprint me test 列印任何傳入的字串 print test 呼叫函式 print me 我要呼叫使用者自定義函式 print me 再次呼叫同一函式 不定長引數 加了星號 的引數會以元組 tuple 的形式匯入 defprint info arg1,var tuple...