⊗pyPmFnSI 3 of 128 menu

Interaction of Scopes in Python

There are cases when external (global) and internal (local) variables have the same name:

num = 1 def func(): num = 2 print(num)

It is important to understand that the local num, although it has the same name, is also global, but for Python it is a different variable. This means that the global num is NOT overwritten inside the function, but retains its original value. When calling the func function, only the value of the local num will be printed to the console, and via print - the value of the global variable:

func() # 2 print(num) # 1

What will be the result of running the following code:

num = 10 def func(): num = 5 return num func() print(num)

What will be the result of running the following code:

num = 3 def func(): num = 4 return num num = func() print(num)

What will be the result of running the following code:

num = 1 def func(): num = 2 return 1 num = func() print(num)

What will be the result of running the following code:

num1 = 1 def func(): num2 = 2 func() print(num1)

What will be the result of running the following code:

num1 = 1 def func(): num2 = 2 func() print(num2)

What will be the result of running the following code:

num1 = 1 def func(): num2 = 2 func() num2 = 3 print(num2)

What will be the result of running the following code:

num1 = 1 num2 = 2 def func(): num2 = 3 func() print(num2)

What will be the result of running the following code:

num1 = 1 def func(): num1 = 2 print(num1)
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline