What will be the output of the following Python code?
def foo(x): x[0] = ['def'] x[1] = ['abc'] return id(x) q = ['abc', 'def'] print(id(q) == foo(q))
True
False
None
Error
sys.argv
os.argv
argv
none of the mentioned
def foo(i, x=[]): x.append(x.append(i)) return x for i in range(3): y = foo(i) print(y)
[[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
[[0], [[0], 1], [[0], [[0], 1], 2]]
[0, None, 1, None, 2, None]
def f1(): x=15 print(x) x=12 f1()
12
15
1512
def f1(): x=100 print(x) x=+1 f1()
100
101
99
def san(x): print(x+1) x=-2 x=4 san(12)
13
10
2
5
def f1(): global x x+=1 print(x) x=12 print("x")
13 x
x
def f1(x): global x x+=1 print(x) f1(15) print("hello")
error
hello
16
16 hello
def f1(a,b=[]): b.append(a) return b print(f1(2,[3,4]))
[3,2,4]
[2,3,4]
[3,4,2]
def f(p, q, r): global s p = 10 q = 20 r = 30 s = 40 print(p,q,r,s) p,q,r,s = 1,2,3,4 f(5,10,15)
1 2 3 4
5 10 15 4
10 20 30 40
5 10 15 40
x = 5 def f1(): global x x = 4 def f2(a,b): global x return a+b+x f1() total = f2(1,2) print(total)
7
8
x=100 def f1(): global x x=90 def f2(): global x x=80 print(x)
90
80
Read the following Python code carefully and point out the global variables?
y, z = 1, 2 def f(): global x x = y+z
y and z
x, y and z
Neither x, nor y, nor z
list
set
dictionary
tuple
x=1 def cg(): global x x=x+1 cg() x
1
0
The local variable is shadowed
Undefined behavior
The global variable is shadowed
a=10 globals()['a']=25 print(a)
25
Junk value
def f(): x=4 x=1 f() x
4
A function that calls itself
A function execution instance that calls another execution instance of the same function
A class method that calls another class method
An in-built method that is automatically called
Recursive function can be replaced by a non-recursive function
Recursive functions usually take more memory space than non-recursive function
Recursive functions run faster than non-recursive function
Recursion makes programs easier to understand