Skip to main content

35.Python Global Variables

Python Global Variables
     A function is typically referred as a block of code which allows you to pass data and return data as a result.A function can also have many variables.A variable declared outside the function is known as Global variable and the variable created within the function is known as Local variable.A local variable cannot be used outside the function but a Global variable can be used both inside and outside the function.

For example,
Program:
def myfun():
    a=18 #Local variable
    print(a)
myfun()
Output:
18
    Here,a variable is created within a function,so it is a local variable.The scope of local variable extends only within the function. If you try to print the local variable outside the function,the program will display error
For example,
Program:
def myfun():
    a=18 #Local variable
    print(a)
myfun()
print(a)#Trying to print Local variable
Output:
18
NameError: name 'a' is not defined
    A global variable is created outside the function as follows:
Program:
a=18#Global variable
def myfun():
    print(a)
myfun()
print(a)#Trying to print Global variable
Output:
18
18
    Thus ,a global variable can be used both inside and outside the function.Now,when you want use both global and local variable,the functions gives priority to the local variable.If the local variable is not present inside the function,it access the global variable,otherwise not. 
Program:
a=18#Global variable
def myfun():
    a=12#Local variable
    print(a)#print Local variable
myfun()
print(a)#print Global variable
Output:
12
18
    Now,if you want to alter or update the global variable inside the function,use the global keyword.The global is a keyword denotes to use or denote the global variable.
Program:
a=18#Global variable
def myfun():
    global a
    a=12#Updates Global variable
    print(a)#print Global variable
myfun()
print(a)#print Global variable
Output:
12
12
    In order to use both local variable and global variable and also it must be possible to alter the values of global and local variable.In that case ,globals() is used.
syntax:
globals()['globalvariable name']
Program:
a=18    #Global variable
def myfun():
a=16    #Local variable
print(a)
globals()['a']=25
  #globals function
myfun()
print(a)    #to print Global variable
Output:
16
25
    To learn more about python,check out the video below!!
Happy Learning!!

Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl