python璇█鍩虹3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python璇█鍩虹3相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e4%bc%a0%e9%80%92%e5%8f%82%e6%95%b0' title='浼犻€掑弬鏁?>浼犻€掑弬鏁?/a>   鍙橀噺   copy   python鍑芥暟   灏变細   鏍囪瘑   exce   cti   澶栭儴   

涓€锛?/strong>python鍑芥暟

    鏄粍缁囧ソ鐨勶紝鍙噸澶嶄娇鐢ㄧ殑锛岀敤鏉ュ疄鐜板崟涓€锛屾垨鐩稿叧鑱斿姛鑳界殑浠g爜鍧椼€備互鍓嶄娇鐢ㄨ繃鐨勪竴浜汸ython鎻愪緵鐨勫唴寤哄嚱鏁帮紝濡俻rint()銆乵ax(4,18)銆乵in(100,50)銆傚綋鐒舵垜浠嚜宸卞垱寤虹殑鍑芥暟锛屽彨鍋氳嚜瀹氫箟鍑芥暟銆?/p>

    鈶犮€佷唬鐮佽閬靛惊鐨勫師鍒欙細楂樺唴鑱氾紝浣庤€﹀悎

        妯″潡鍐呴儴涔嬮棿鐨勮仈绯伙紝搴旇闈炲父绱у瘑鐨?/p>

        妯″潡涓庢ā鍧椾箣闂寸殑鑱旂郴锛屽簲璇ュ敖閲忓皯

    鈶°€?strong>浼樼偣锛?/strong>

        鍑芥暟鑳芥彁楂樺簲鐢ㄧ殑妯″潡鎬э紝鍜屼唬鐮佺殑閲嶅鍒╃敤鐜囥€?nbsp; 

    鈶€佸畾涔夊嚱鏁扮殑鍦哄悎锛?/strong>

        鍑℃槸鎴戜滑闇€瑕佹弿杩版煇涓姛鑳界殑鏃跺€欙紝閮藉皢瀹冨畾涔変负鍑芥暟

    鈶c€佸嚱鏁板畾涔夎鍒欙細

        鍑芥暟浠g爜鍧椾互 def 鍏抽敭璇嶅紑澶达紝鍚庢帴鍑芥暟鏍囪瘑绗﹀悕绉板拰鍦嗘嫭鍙?()銆備换浣曚紶鍏ュ弬鏁板拰鑷彉閲忓繀椤绘斁鍦ㄥ渾鎷彿涓棿锛屽渾鎷彿涔嬮棿鍙互鐢ㄤ簬瀹氫箟鍙傛暟銆?nbsp; 鍑芥暟鍐呭浠ュ啋鍙疯捣濮嬶紝骞朵笖缂╄繘銆?/p>

        return [琛ㄨ揪寮廬 缁撴潫鍑芥暟锛岄€夋嫨鎬у湴杩斿洖涓€涓€肩粰璋冪敤鏂广€備笉鍐檙eturn锛岀浉褰撲簬杩斿洖 None銆?/p>

    鈶ゃ€佸畾涔夊嚱鏁版牸寮忥細

        def 鍑芥暟鍚嶏紙鍙傛暟鍒楄〃锛?

            鍑芥暟浣?/p>

        鍑芥暟璋冪敤锛?/p>

            鍑芥暟涓嶈皟鐢ㄥ氨涓嶄細鎵ц锛岃皟鐢ㄥ嚱鏁伴€氳繃鍑芥暟鍚嶅畬鎴?/p>

    鈶ャ€?strong>寤鸿锛?/strong>

        鍑芥暟鐨勭涓€琛岃鍙ワ紝鍙互閫夋嫨鎬у湴浣跨敤鏂囨。瀛楃涓诧紝鐢ㄤ簬瀛樻斁鍑芥暟璇存槑锛屽嚱鏁板畾涔夋椂锛屼竴涓嚱鏁扮殑鍔熻兘瑕佸崟涓€銆?/p>

1銆佺畝鍗曞畾涔夊嚱鏁?/p>

#瀹氫箟鍑芥暟
def hello() :
    print("Hello Python ...!")
#璋冪敤鍑芥暟 鍑芥暟涓嶈皟鐢紝涓嶆墽琛?hello()

 杩愯缁撴灉锛?/span>

Hello Python ...!

銆€2銆佺畝鍗曞疄鐜颁笟鍔?/p>

#瀹氫箟鍑芥暟锛屽疄鐜版墦鍗?琛?鍒楃殑*鍙?def print_tr() :
    for i in range(5):
        for j in range(5):
            print("* ",end="")
        print()
#璋冪敤
print_tr()

杩愯缁撴灉锛?/p>

* * * * * 
* * * * * 
* * * * * 
* * * * * 
* * * * *

銆€3銆佸湪鍑芥暟涓紝瀹氫箟鍙傛暟

    鍑芥暟鏄惁闇€瑕佹湭鐭ユ暟鍙備笌锛岄渶瑕佸彲浠ョ敤鍙傛暟瀹氫箟鍑烘潵銆?/p>

#璁$畻涓や釜鏁颁箣鍜?#2涓暟鏄湭鐭ユ暟锛屾湭鐭ユ暟鐩稿綋浜巔ython鐨勫弬鏁?def add(a,b):
    #print(鈥榹0}+{1}={2}鈥?format(a,b,a+b))
    print(鈥?s+%s=%s鈥?(a,b,a+b))
#璋冪敤
add(7,8)

杩愯缁撴灉锛氥€€

7+8=15

4銆佸湪鍑芥暟涓紝鎸囧畾杩斿洖鍊?/p>

    return [琛ㄨ揪寮廬 缁撴潫鍑芥暟锛岄€夋嫨鎬у湴杩斿洖涓€涓€肩粰璋冪敤鏂癸紝涓嶅甫琛ㄨ揪寮忕殑return鐩稿綋浜庤繑鍥濶one銆?/p>

#璋僡ddCopy01()鍑芥暟锛岃繑鍥炰袱涓暟涔嬪拰鐨勫€?def addCopy01(a,b):
    return a+b
#璋冪敤
result = addCopy01(100,200)
print(result)
print(鈥?------------------------------鈥?
#return 鐩存帴缁撴潫鍑芥暟锛屸€樺嚱鏁扮粨鏉熲€樻彁绀猴紝娌℃湁杈撳嚭銆?def test_method():
    for i in range(10):
        print(i)
        return  # return缁撴潫鍑芥暟     break缁撴潫寰幆
    print(鈥樺嚱鏁扮粨鏉熲€?
#璋冪敤
test_method()

杩愯缁撴灉锛?/p>

300
-------------------------------
0

5銆佸弬鏁扮被鍨?/p>

鈶犮€佸繀闇€鍙傛暟

    蹇呴渶鍙傛暟椤讳互姝g‘鐨勯『搴忎紶鍏ュ嚱鏁般€傝皟鐢ㄦ椂鐨勬暟閲忓繀椤诲拰澹版槑鏃剁殑涓€鏍枫€傝皟鐢ㄥ嚱鏁帮紝浣犲繀椤讳紶鍏ュ弬鏁帮紝涓嶇劧浼氬嚭鐜拌娉曢敊璇細

#蹇呴』浼犲叆锛?涓弬鏁?def addCopy01(a,b):
    return a+b
#璋冪敤
addCopy01(100,200)

銆€鈶°€佸叧閿瓧鍙傛暟

    鍏抽敭瀛楀弬鏁板拰鍑芥暟璋冪敤鍏崇郴绱у瘑锛屽嚱鏁拌皟鐢ㄤ娇鐢ㄥ叧閿瓧鍙傛暟鏉ョ‘瀹氫紶鍏ョ殑鍙傛暟鍊笺€備娇鐢ㄥ叧閿瓧鍙傛暟鍏佽鍑芥暟璋冪敤鏃跺弬鏁扮殑椤哄簭涓庡0鏄庢椂涓嶄竴鑷达細

#鍏抽敭瀛楀弬鏁帮細
def printinfo(name, age):
    print("鍚嶅瓧: ", name)
    print("骞撮緞: ", age)
#璋冪敤    
printinfo(age=50, name="寮犱笁")

銆€杩愯缁撴灉锛?/span>

鍚嶅瓧:  寮犱笁
骞撮緞:  50

鈶€侀粯璁ゅ弬鏁?/p>

    璋冪敤鍑芥暟鏃讹紝濡傛灉娌℃湁浼犻€掑弬鏁帮紝鍒欎細浣跨敤榛樿鍙傛暟銆?/p>

#榛樿鍙傛暟锛?def printinfo(name, age=35):
    print("鍚嶅瓧: ", name)
    print("骞撮緞: ", age)
#璋冪敤   
printinfo(name="寮犱笁",age=50)
print("------------------------")
printinfo(name="寮犱笁")

杩愯缁撴灉锛氥€€銆€

鍚嶅瓧:  寮犱笁
骞撮緞:  50
------------------------
鍚嶅瓧:  寮犱笁
骞撮緞:  35

鈶c€佷笉瀹氶暱鍙傛暟

    鏈夋椂鍙兘闇€瑕佷竴涓嚱鏁拌兘澶勭悊姣斿綋鍒濆0鏄庢椂鏇村鐨勫弬鏁般€傝繖浜涘弬鏁板彨鍋氫笉瀹氶暱鍙傛暟銆?/p>

    鍔犱簡鏄熷彿锛?锛夌殑鍙傛暟鍚嶏紝浼氬瓨鏀炬墍鏈夋湭鍛藉悕鐨勫彉閲忓弬鏁般€傚鏋滃湪鍑芥暟璋冪敤鏃讹紝娌℃湁鎸囧畾鍙傛暟锛屽畠灏辨槸涓€涓┖鍏冪粍銆?/p>

#鏌ョ湅涓嶅畾闀垮弬鏁帮紝浼犲叆鐨勫€?def add2(a,*b):
    print(a,end="")
    print(b)
#璋冪敤
add2(1)  #1()
add2(1,2) #1(2,)
add2(1,2,3) #1(2, 3)
print("------------------------")
#姹傚拰 [涓嶉檺鍒跺嚑涓弬鏁癩
def add3(a,*b):
    sum = 0
    for i in b:
        sum += i
    return a+sum
#璋冪敤
print(add3(1))
print(add3(1,2))
print(add3(1,2,3))

杩愯缁撴灉锛?/p>

1()
1(2,)
1(2, 3)
------------------------
1
3
6

python 浣跨敤 lambda 鏉ュ垱寤哄尶鍚嶅嚱鏁般€傛墍璋撳尶鍚嶏紝鎰忓嵆涓嶅啀浣跨敤 def 璇彞鏉ュ畾涔変竴涓嚱鏁般€?/p>

    鈶犮€乴ambda 鍑芥暟鐨勮娉曞彧鍖呭惈涓€涓鍙?/p>

    鈶°€佸懡鍚嶇┖闂达細python涓瓨鏀惧彉閲忓悕鐨勭┖闂存垨瀛楀吀锛?/p>

娉ㄦ剰锛?/p>

    鈶犮€乴ambda 鍙槸涓€涓〃杈惧紡锛屽嚱鏁颁綋姣?def 绠€鍗曞緢澶氥€?/p>

    鈶°€?lambda鐨勪富浣撴槸涓€涓〃杈惧紡锛岃€屼笉鏄竴涓唬鐮佸潡銆備粎浠呰兘鍦╨ambda琛ㄨ揪寮忎腑灏佽鏈夐檺鐨勯€昏緫杩涘幓銆?/p>

    鈶€?lambda 鍑芥暟鎷ユ湁鑷繁鐨勫懡鍚嶇┖闂达紝涓斾笉鑳借闂嚜宸卞弬鏁板垪琛ㄤ箣澶栨垨鍏ㄥ眬鍛藉悕绌洪棿閲岀殑鍙傛暟銆?/p>

sum = lambda a,b:a+b
print(sum(10,20))

杩愯缁撴灉锛?/p>

30

7銆佸弬鏁颁紶閫掗棶棰?/p>

 鈶犮€佸璞℃槸鐪熷疄瀛樺湪鐨勬暟鎹紝绫诲瀷灞炰簬瀵硅薄锛屽彉閲忔槸娌℃湁绫诲瀷鐨勩€?/strong>

number = [1,2,3,4,5,6]
info = "Hello"

瑙i噴锛歔1,2,3,4,5,6] 鏄?List 绫诲瀷锛?ldquo;Hello” 鏄?String 绫诲瀷锛岃€屽彉閲弉umber涓巌nfo鏄病鏈夌被鍨嬶紝瀹冧粎浠呮槸涓€涓璞$殑寮曠敤(涓€涓寚閽?,鍙互鏄寚鍚?List 绫诲瀷瀵硅薄锛屼篃鍙互鏄寚鍚?String 绫诲瀷瀵硅薄

鈶°€佸彲鏇存敼涓庝笉鍙洿鏀瑰璞?/strong>

    python 涓竴鍒囬兘鏄璞★紝浼犻€掑弬鏁板垎涓轰紶涓嶅彲鍙樺璞″拰浼犲彲鍙樺璞?/p>

    python 涓?string瀛楃涓?tuple鍏冪粍,鍜宯umbers鏁存暟,鏄笉鍙洿鏀圭殑瀵硅薄,鑰宭ist鍒楄〃,dict瀛楀吀绛夊垯鏄彲浠ヤ慨鏀圭殑瀵硅薄銆?/p>

     浣跨敤鍑芥暟鐨勬椂鍊欙紝濡傛灉浼犲叆鍙傛暟鏄笉鍙彉鐨勶紝閭d箞鍙傛暟閲岄潰瀵规暟鎹仛鐨勪换浣曟搷浣滐紝閮戒笉浼氬鍑芥暟澶栭潰鐨勬暟鎹甫鏉ュ奖鍝嶃€備笉鍙彉绫诲瀷锛氬fun锛坅锛夛紝浼犻€掔殑鍙槸a 鐨勫€硷紝娌℃湁褰卞搷a瀵硅薄鏈韩銆傛瘮濡傚湪 fun锛坅锛夊唴閮ㄤ慨鏀?a 鐨勫€硷紝鍙槸淇敼鍙︿竴涓鍒剁殑鏂板璞★紝涓嶄細褰卞搷 a 鏈韩銆?/p>

# 涓嶅彲鍙樼被鍨嬪弬鏁颁紶閫?num = 1
def change(num):
    num = 3 #娌℃湁瀵筺um鎿嶄綔锛岄噸鏂板畾涔変簡涓€涓猲um
    print(num)
change(num)
print(num)  #涓嶄細褰卞搷鍑芥暟澶栵紝num鏈韩鐨勫€?print(鈥?---------------------鈥?
# 杩斿洖鍊间緥澶?num2 = 1
def change(num2):
    print(num2)   # 1
    num2 = 3
    return num2
num_result = change(num2)
print(num_result)  #杩斿洖鍊间緥澶?3
print(num2)  #鍑芥暟鍐呮敼鍙橈紝涓嶅奖鍝嶅嚱鏁板鐨勪笉鍙彉瀵硅薄 1

杩愯缁撴灉锛?/p>

3
1
----------------------
1
3
1

鍙彉瀵硅薄绫诲瀷锛氬 fun锛坙a锛夛紝鍒欐槸灏?la 鐪熸鐨勪紶杩囧幓锛屽湪鍑芥暟鍐呴儴锛屼慨鏀瑰悗锛宖un澶栭儴鐨刲a涔熶細鍙楀奖鍝?/p>

list_num = [1,2]
def change(list_num):
    list_num.append(3)
    print(list_num)  #[1, 2, 3]
change(list_num)
print(list_num)  #[1, 2, 3]

杩愯缁撴灉锛?/p>

[1, 2, 3]
[1, 2, 3]

浜岋細鍙橀噺浣滅敤鍩?/strong>

    Python 涓紝绋嬪簭鐨勫彉閲忓苟涓嶆槸鍦ㄥ摢涓綅缃兘鍙互璁块棶鐨勶紝璁块棶鏉冮檺鍐冲畾浜庤繖涓彉閲忔槸鍦ㄥ摢閲岃祴鍊肩殑銆傚彉閲忕殑浣滅敤鍩熷喅瀹氫簡鍦ㄥ摢涓€閮ㄥ垎绋嬪簭鍙互璁块棶鍝釜鐗瑰畾鐨勫彉閲忓悕绉般€?/p>

Python鐨勪綔 鐢ㄥ煙涓€鍏辨湁4绉嶏紝浠庡皬鍒板ぇ鍒嗗埆鏄細

    L 锛圠ocal锛?灞€閮ㄤ綔鐢ㄥ煙

        灞€閮ㄤ綔鐢ㄥ煙锛屽嵆鍑芥暟涓畾涔夌殑鍙橀噺

        灞€閮ㄥ彉閲忥紙鍙兘鍦ㄥ嚱鏁板唴閮ㄤ娇鐢級

    E 锛圗nclosing锛?闂寘鍑芥暟澶栫殑鍑芥暟涓?/strong>

        宓屽鐨勭埗绾у嚱鏁扮殑灞€閮ㄤ綔鐢ㄥ煙锛屽嵆鍖呭惈姝ゅ嚱鏁扮殑涓婄骇鍑芥暟鐨勫眬閮ㄤ綔鐢ㄥ煙锛屼絾涓嶆槸鍏ㄥ眬鐨勶紱

    G 锛圙lobal锛?鍏ㄥ眬浣滅敤鍩?/strong>

        鍏ㄥ眬鍙橀噺锛屽氨鏄ā鍧?python鏂囦欢)绾у埆瀹氫箟鐨勫彉閲忥紱

        瀹氫箟鍦ㄥ嚱鏁板鐨勫彉閲?/p>

    B 锛圔uilt-in锛?鍐呭缓浣滅敤鍩?/strong>

        绯荤粺鍥哄畾妯″潡閲岄潰鐨勫彉閲忥紝姣斿int, bytearray绛夈€?/p>

    鎬荤粨锛氫互 L –> E –> G –>B 鐨勮鍒欐煡鎵撅紝鍗筹細鍦ㄥ眬閮ㄦ壘涓嶅埌锛屼究浼氬幓灞€閮ㄥ鐨勫眬閮ㄦ壘锛堜緥濡傞棴鍖咃級锛屽啀鎵句笉鍒板氨浼氬幓鍏ㄥ眬鎵撅紝鍐嶈€呭幓鍐呭缓涓壘锛屽唴寤轰腑鎵句笉鍒扮▼搴忔姤閿?/p>

name = str(鈥樺紶涓夆€? #python鍐呯疆鐨勪綔鐢ㄥ煙 ->鍐呭缓浣滅敤鍩?y = 2 #鍏ㄥ眬 ->鍏ㄥ眬浣滅敤鍩?def outer():
    a = 3 #闂寘 -> 闂寘
    def inner():
        b=2  #灞€閮?->灞€閮ㄤ綔鐢ㄥ煙
        print(a)
    inner()
    #print(b) #鎶ラ敊
outer()
#print(a)  #鎶ラ敊

杩愯缁撴灉锛?/p>

3

1銆佷綔鐢ㄥ煙涓€

    Python 涓彧鏈夋ā鍧楋紙module锛夛紝绫伙紙class锛変互鍙婂嚱鏁帮紙def銆乴ambda锛夋墠浼氬紩鍏ユ柊鐨勪綔鐢ㄥ煙锛堟湁浣滅敤鍩熺殑闄愬埗锛夛紱鍏跺畠鐨勪唬鐮佸潡锛堝 if/elif/else/銆乼ry/except銆乫or/while绛夛級鏄笉浼氬紩鍏ユ柊鐨勪綔鐢ㄥ煙鐨勶紝涔熷氨鏄杩欎簺璇彞鍐呭畾涔夌殑鍙橀噺锛屽閮ㄤ篃鍙互璁块棶銆?/p>

# 瀹炰緥涓?msg 鍙橀噺瀹氫箟鍦?if 璇彞鍧椾腑锛屼絾澶栭儴杩樻槸鍙互璁块棶鐨勩€?if True:
    msg = "ok"
print(msg)
# 瀹炰緥涓?msg 瀹氫箟鍦ㄥ嚱鏁颁腑锛屽垯瀹冨氨鏄眬閮ㄥ彉閲忥紝澶栭儴涓嶈兘璁块棶锛?def test():
    msg_inner = 鈥榦k鈥?# print(msg_inner) #鎶ラ敊锛屾棤娉曡闂眬閮ㄥ彉閲?

杩愯缁撴灉锛?/p>

ok

2銆佷綔鐢ㄥ煙浜?/p>

    鈶犮€佸眬閮ㄥ彉閲忥細瀹氫箟鍦ㄥ嚱鏁板唴閮ㄧ殑鍙橀噺鎷ユ湁涓€涓眬閮ㄤ綔鐢ㄥ煙锛屽眬閮ㄥ彉閲忓彧鑳藉湪鍏惰澹版槑鐨勫嚱鏁板唴閮ㄨ闂€?/p>

    鈶°€佸叏灞€鍙橀噺锛氬畾涔夊湪鍑芥暟澶栫殑鎷ユ湁鍏ㄥ眬浣滅敤鍩? 鍏ㄥ眬鍙橀噺鍙互鍦ㄦ暣涓▼搴忚寖鍥村唴璁块棶銆?/p>

    鈶€佽皟鐢ㄥ嚱鏁版椂锛屾墍鏈夊湪鍑芥暟鍐呭0鏄庣殑鍙橀噺鍚嶇О閮藉皢琚姞鍏ュ埌浣滅敤鍩熶腑銆?/p>

total = 0
def sumcopyt(arg1,arg2):
    total= arg1+arg2
    print("灞€閮ㄥ彉閲?,total) # 缁撴灉鏄細3鑰屼笉鏄?(浠?L –> E –> G –>B 鐨勫厛鍚庤鍒欐煡鎵?
    return total
sumcopyt(1,2)
print(鈥樺叏灞€鍙橀噺鈥?total)

杩愯缁撴灉锛?/p>

灞€閮ㄥ彉閲?3
鍏ㄥ眬鍙橀噺 0

3銆佷綔鐢ㄥ煙涓?/p>

    涓€涓笉鍦ㄥ眬閮ㄤ綔鐢ㄥ煙閲岀殑鍙橀噺榛樿鏄彧璇荤殑锛屽鏋滆瘯鍥句负鍏剁粦瀹氫竴涓柊鐨勫€硷紝python璁や负鏄湪褰撳墠鐨勫眬閮ㄤ綔鐢ㄥ煙閲屽垱寤轰竴涓柊鐨勫彉閲忥紝涔熷氨鏄鍦ㄥ綋鍓嶅眬閮ㄤ綔 鐢ㄥ煙涓紝濡傛灉鐩存帴浣跨敤澶栭儴浣滅敤鍩熺殑鍙橀噺锛岄偅涔堣繖涓彉閲忔槸鍙鐨勶紝涓嶈兘淇敼銆?/p>

global锛氶€氳繃閲岄潰鐨勬搷浣滐紝鏀瑰彉澶栭潰鐨勫€?/strong>

name=鈥?0鈥?def change_name():
    name=鈥?00鈥?    print(鈥樺眬閮ㄥ彉閲忕殑鍊硷細鈥?name)
change_name()
print(鈥樺叏灞€鍙橀噺鐨勫€硷細鈥?name)  #鍏ㄥ眬鐨勫彉閲忥紝骞舵病鏈変慨鏀?print(鈥?-------------------------鈥?
name2=鈥?0鈥?def change_name2():
    #鎯宠灞€閮ㄧ殑total鍜屽叏灞€鐨則otal鍙樹负鍚屼竴涓猼otal锛堥€氳繃閲岄潰鐨勬搷浣滐紝鏀瑰彉澶栭潰鐨勫€硷級
    global name2
    name2=鈥?00鈥?    print(鈥樺眬閮ㄥ彉閲忕殑鍊硷細鈥?name2)
change_name2()
print(鈥樺叏灞€鍙橀噺鐨勫€硷細鈥?name2)  #鍏ㄥ眬鐨勫彉閲忥紝宸茬粡淇敼鍟?

杩愯缁撴灉锛?/p>

灞€閮ㄥ彉閲忕殑鍊硷細100
鍏ㄥ眬鍙橀噺鐨勫€硷細50
--------------------------
灞€閮ㄥ彉閲忕殑鍊硷細100
鍏ㄥ眬鍙橀噺鐨勫€硷細100

4銆佷綔鐢ㄥ煙鍥?/p>

    濡傛灉瑕佷慨鏀瑰祵濂椾綔鐢ㄥ煙鍗抽棴鍖呭嚱鏁帮紙enclosing 浣滅敤鍩燂紝澶栧眰闈炲叏灞€浣滅敤鍩燂級涓殑鍙橀噺鍒欓渶瑕?strong>nonlocal 鍏抽敭瀛?/p>

def method_01():
    a = 1
    def method_02():
        nonlocal a
        a=2
        print(a)
    method_02()
    print(a)

method_01()

杩愯缁撴灉锛?/p>

2
2

涓夛細Python鍐呯疆鍑芥暟    

    涓轰簡鎻愰珮绋嬪簭鍛樼殑寮€鍙戞晥鐜囷紝Python 鎻愪緵浜嗗緢澶氬彲浠ョ洿鎺ユ嬁鏉ョ敤鐨勫嚱鏁般€?/p>

    鍐呭鍙傝€冿細http://www.runoob.com/python3/python3-built-in-functions.html

銆€

以上是关于python璇█鍩虹3的主要内容,如果未能解决你的问题,请参考以下文章

Map | Go璇█鍩虹

SpringBoot蹇呯煡蹇呬細-yaml鍩虹璇硶

銆愬涔燙++ 鏁欑▼銆戜簩銆丆++鍩虹璇硶銆佹敞閲婂拰鍙橀噺

鎴戠殑鍏ㄦ爤涔嬭矾-C璇█鍩虹涔婥绋嬪簭杩愯娴佺▼

python鍩虹瀛︿範6-mongodb銆乻ys銆佹帴鍙e紑鍙戙€佹搷浣渆xcel

Flutter鍏ラ棬鍩虹

(c)2006-2024 SYSTEM All Rights Reserved IT常识