How to create python global variable?

admin
By -
0

 What is python global variable and how to we use it?




In python does not have specific method to declare variable 

For creating Variable in python 

we just  create name of variable and assign the value like int ,string, float

for assign int we just use equal and for string we have to use double quote ""

and for float using number with decimal


like this 

var1 = 23 

var2 = "MyName"


var3 = 45.5

here we create three python variable with different data types and this is method of creating global variable because its not inside any function or block


What is global variable

In python that variable is outside of any function and declare alone is global variable.

So that we can use this with any function without passing reference.

Example -

outsidevar = 23


def oufunc():

   print("Nothing")


How to declare  block Variable into global 


For this we are using global keyword

def oufunc():

  global var2

oufunc()

print(var2)



Thanks


Hope You  like it and its solve your problem.

For more update follow and share

Have nice day!!!

Post a Comment

0Comments

Put Your Thought or Query Here

Post a Comment (0)