[Python Programming Basics to Advanced]: Local and Global Variables (LEGB Rule) | Lab 18P-2

preview_player
Показать описание
This Python programming playlist is designed to take beginners with zero programming experience to an expert level. The course covers installation, basic syntax, practical scenarios, and efficient logic building. The course material includes PDF handouts, review questions, and covers a wide range of topics, from data types to advanced functions like Lambda and Recursive functions, Generators, and JSON data parsing.

In this lesson we will learn about the Scope of a Variable. Commonly a variables can be Local or Global. But it can also be Enclosing or Built-in. Python follows LEGB rule to pick the variable value.

Moreover, we will learn how to create our own module.

Complete Playlist:

If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:

Lab Manual 18 can be downloaded from here:

Review Questions:
Without running the program, guess the output of this program and explain your answer:
def test1(y):
global x
x=20
a=100*y
b=a+test2()
return b
def test2():
global x
return x/2

## Main Program ##
x=10
print(test1(1))
print(x)

#PythonProgramming #python #pythontutorial
Рекомендации по теме
Комментарии
Автор

print(test1(1)) : Answer = 110.0
print(x) : Answer = 20

hamidiftikhar
Автор

Output will be:-
110
20
x value is changed to 20 as x is set to global in test1() and in test2(), so the program executes using x=20 and returns the value of b in first print statement. And in second print statement value of x in main program is printed which is also equal to 20 as x is global.

muhammad-ahmad
Автор

The expected output is :
110
20
In both user-defined functions, the local value of x is set to the global value. As the scope set to global, so its change the value of x from ( any previous value i.e, 10 in this case ) to (any new assigned value i.e, 20 in this case ). So now the value of X is 20 in the main program as well as in both functions. So all calculations are done by using 20 as the value of x so we get the expected output.

abdurrehmansarwar
Автор

The expected output is
110
20
The reason being that the "global x" in test1(y) allows it's local "x" to be stored in the global "x", i.e; 20 which is later used when test2() is called, returning "x/2" i.e; 10, which goes on to compute "b" i.e; 110. At the print statements, x=20 and b=110.

Upon running, the output is
110.0
20
The tiny misguess came from not realizing that x/2 computes a floating point number, and hence the 110.0

muhammadalihaider
Автор

Task)
The outputs expected will be 110 and 20.
At the start, the value of x=10 but when it is called by function def test1(y) it changes from 10 to 20 . And when function def test2() is called global x the value is 20 . Finally, when b is returned, its value is 110.

hishamawan
Автор

The measured and self calculated output is:
110
20
Explanation:
Firstly, the (def test1(y)) has a global x so that will access the x in the main program. Secondly,
def test2() will give the half of global x and the value of y is given in the first print (print(test1(1))) which is (1).
The calculated (b) will be 110, and the value of x in main program will be the value of global x.

ahmedimran
Автор

The Output will be:
110
20
Because, at first the (x) has an Global value of 10, after which function test1() is called, in which the Global value of (x) is updated to 20, then the value of (a) comes out to be 100 and while calculating the value of (b) we jumped into function test2(), in which Global value of (x)(which is now 20) is divided by 2 and returned to previous function test1() and the value of (b) becomes 110 and returned to the main program to be printed, after which Global value of (x):20 is printed which was updated in the first function.

MuhammadTalha-xmul
Автор

Expected outputs are:
110
20
Reason:
When the program calls the first user defined function, the value of 'x' becomes 20 from 10(and is marked as global, and will remain 20 throughout the program). the first user defined function calls the second user defined function to calculate 'b' (i.e 110). In the end the values 110 and 20 are printed by the main program.

AliIrfan-soxf
Автор

output:
110
20
explanation:
In both user-defined functions, the local value of x is set to the global value. As the scope set to global, so its change the value of x from (any previous value i.e, 10 in this case) to (any new assigned value i.e, 20 in this case). So now the value of x is 20 in the main program as well as in both functions. So all calculations are done by using 20 as the value of x so we get the expected outpu

MuhammadQasim-erwb
Автор

The expected output will be :
110
20
Reason:
----Initially, the global value of x=10. When it is called by function def test1(y), it's value changed from 10 to 20 globally.
And when function def test2() call global x it's value is 20 and the function test2() returns 10 . So, the value of print(x) is 20.
----Using def test1(y), as y is assigned 1 in print(test1(1)) so a=100*y
becomes a=100 and b=a+test2() becomes b=100+10=110.
So the value of print(test1(1)) is 110.

noorulhuda
Автор

Assalam-o-Alakium Sir,
The expected output will be:-

110

20

REASON:-

Initially, 'x' is global variable in 1st Function which will change its value from 10 to 20 and a=100*y which gives 100(given:y=1). Moreover, second function will return the value of 'x': 20/2=10. Finally, b=a+test(2) will give 100+10=110. In this way, b=110 and value of 'x' is taken from main program due to function test1 as x=20 .

zainzakir
Автор

The output will be:
110
20
At def test 1 (y) function, it has global x so it must change the value of x in main program which is 10 and then it becomes 20. Now a=100*y and y=1 in main program i.e print(test1(1)). Now a=100 and for b there is another function def test2() where global x is called and it must return its half i.e 10.Then it will be add up(b=100+10) so b=110.

huzaifasarwar
Автор

The output will be:
110
20
as print(test(1)) calls first function and there global x changes x=20 and then in that function 2nd function is being called which returns the value of x as 10 and then in first function y=1and b=100+10 so print(test(1))=110 and due to global x of function 1 the print(x) of main program also changes to 20 hence print(x)=20.

nukhbaiqbal
Автор

Output might be:
1) 110
2) 20



Firstly, x is set to be 10 but its value changes as a global x is present in test1(). The value is global again in case of test2() so it will be 20. After calculating, b=110 is returned. print(x) returns 20 as x was given value 20 as global in test1() function.

laveezayasin
Автор

The expected outputs might be:
110
20
x is marked as global inside the first function. Therefore, the value of x will change from 10 to 20. After the test2() function is called, b is calculated as 110.

zainulhassan
Автор

Expected Output:
110
20
At first the (x) has an Global value of 10, after which function test1() is called, in which the Global value of (x) is updated to 20, then the value of (a) comes out to be 100 and while calculating the value of (b) we jumped into function test2(), in which Global value of (x)(which is now 20) is divided by 2 and returned to previous function test1() and the value of (b) becomes 110 and returned to the main program to be printed, after which Global value of (x):20 is printed which was updated in the first function.

haris
Автор

Review task:
The expected output from the mentioned program will be:
110
20
Reasoning:
At the start, x is 10 but in test1 x is a global variable so its value updated to 20. Then for test2() again x is global so its value is 20 so when the function is called it returns x=10. Now it comes to test1 where it is called, so b=a+test2()=100+10=110.

MuhammadQasim-kwer
Автор

The expected output ;
110
20
REASON:-
Initially, 'x' is global variable in 1st Function which will change its value from 10 to 20 and a=100*y which gives 100(given:y=1). Moreover, second function will return the value of 'x': 20/2=10. Finally, b=a+test(2) will give 100+10=110. In this way, b=110 and value of 'x' is taken from main program due to function test1 as x=20 .

irfanshehzada
Автор

explaining the pattern of the program here, since in the main program x=10 before calling the test1() function, so x inside(since its global) will change the value of x to 20. which is further divided inside secnd function whihc is called back in frst function, impies here : a=100, b= 110 and x= 20 .

dawood
Автор

The expected output is:
110
20
As value of x in main program was 10 but in the first user defined function, it was global so value of x changed to 20 and as y=1 in main function so a=100 and then in second user defined function also x was global so value of x remains 20 and it returns 10 to to b and the first output after calculations is 110 and value of x is 20.

MuhammadAhmad-dshu