Variable values after function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Variable values after function

x=5 def f() x=2 print (x) f() print (x) Get this output: 2 5 Why "x" value is not 2 at the end?

19th Mar 2018, 7:13 PM
Jon Míkel Ibarreche Harfush
1 Answer
+ 4
there are two different x: global x is 5 inner x is 2 f() prines inner x print(x) prints global !
19th Mar 2018, 7:17 PM
Oma Falk
Oma Falk - avatar