Create Global variable in Python

Make Global variable in Python from a local variable:

When a variable is defined inside a function, it’s local and can’t be used outside its function.

To make it global, we can use “global” keyword in Python.

When using the global keyword, the variable will belong to the global scope.

def mafun():
  global x
  x = "lets go viral"

mafun()

print("Heyy " + x)



Output:

Heyy lets go viral



Bilel-A

Enthusiastic Network Engineer specializing in Cisco ACI, passionate about solving challenges. A lifelong learner who loves gaining and sharing knowledge. Profile: https://www.linkedin.com/in/bilel-ameur-71116b2b5
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x