0

I am a newbie to python and was experimenting with local and global variables. 'example1' produced the output '6,16,6 ', which is as expected.

x=6
def example1():
  print x
  print (x+10)
  print x  
example1()

In the 2nd example:

x=6
def example2():
   print x
   print (x+10)
print x  
example2()

I expected '6,16,6' as the o/p, but got '6,6,16 ' as the output. Can someone explain why this happened in 'example2()'?

(I was of the view that the 2nd 'print x' statement in 'example2' is referring to the global variable x (which equals 6), and hence, felt that '6,16,6' should be the output)

1
  • 2
    In the second code sample, the second print x is executed before example2. Hence 6,6,16. Commented Feb 17, 2016 at 7:13

3 Answers 3

1

In your second example, the first value of x will be 6. Then you are calling the method example2() which will firstly print x ( which is 6 ) and then x+10.

So the output will be:

6
6
16

For a better understanding, here is the order of execution for your program:

x=6
def example2():
   print x
   print (x+10)
print x  # this will be called first, so the first output is 6
example2() # then this, which will print 6 and then 16
Sign up to request clarification or add additional context in comments.

3 Comments

This cleared the concept for me. Thanks a lot,Alex !
Sorry if it sounds rudimentary, but I'm not sure how to. Can you tell me ?
You'll see an outline of a checkmark on the left side of each answer. It'll be right below the up and down vote arrows.
0
x=6
def example2():
   print x +"2nd call"
   print (x+10)
print x+" 1st call"  # This print gets call first
example2()

I think this explains.1st print gets called first as its out of function and before function too If u want output as 6,16,6 make chages as follow

x=6
def example2():
   print x
   print (x+10) 
example2()
print x 

2 Comments

The first edit that you suggested is something I can use from now on, to understand the sequence of execution of my programs. Thank you for both the edits!
N.B: In line #3 and #5 of the 1st edit, explicit typecasting of integer into string would be needed, while printing the concatenation of an integer and string. Else, one would encounter an error. An easy way to do this would be : print str(x)+"2nd call"
0

Demonstrate Local and Global Variables.

Local variables are variables that are declared inside a function. Global variables are variables that are declared outside a function.

Program to demonstrate the use of Local and Global Variables.

global_var = 5                     #Global Variable
def add(a,b):
    local_var = a+b                #Local Variable
    print("The sum is",local_var)
    print("The global variable is",global_var)
add(10,20)

Output is:

The sum is 30
The global variable is 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.