local vs global variables

# in functions we use Global vs Local variables
# Global means as the name suggests it's Global (accessed anywhere in file)

def player_health():
  # Local variable 
  # Local variables can't be accessed outside of a function
  player_health = 78
  
# error
print(player_health)


# Global Variables
player_strength = 2
def increase_strength():
	player_strength = 3		# this will give you an error because the player_strength
    # is defined in the global scope so you can't modify it in a function
    
    # if you try to print the global variable in function you can do it
    print(player_strength)
    
# Local
	# only accessed/edited by the functions only
# Global
	# accessed/edited in global level not in functions
    # only accessed by functions but not editable
    
# if you got something by this example please upvote it

  

4.17
6
Phoenix Logan 186120 points

                                    Local variables:
These variables can be used or exist 
only inside the function. These variables
are not used or referred by any other function.

Global variables:
These variables are the variables which
can be accessed throughout the program.
Global variables cannot be created
whenever that function is called.

4.17 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
Local vs. global variables example in js Local vs. global variables example Local vs. global variables difference local and global variable local and global variables examples distinguish between local and global variables local and global variables examoles What are global variables and local variables global vs local variable can something be both local and global var global vs local variables javascript global vs local local variables and global variables define local and global variables local vs global variables difference bw the local and global variables difference between global and local varia;ble difference between local and global variables javascript local vs global variables local variable vs global variable difference between local and global variable what is the difference between local variable and global variable local vs global variable difference between a global and local variable global and local variable what difference between global variables and local variables global vs local variables in javascript difference between global and local variable global and local variables difference between a global and a local variable Discuss the difference between global variable and local variable js local vs global variable local and global variables can a variable be both local and global what is the difference between local variables and global variables what is the difference between local and global variables what is the difference between a global and local variable global variables vs local variable in functions global and local variables in python local and global variable python WHICH IS MORE PREFERRDD GLOBAL VARIABLE OR LOCAL VARIABLE local variables vs global variables local vs global variables in sql global vs local scope python global and local variable python global variable vs local variable python global variable local and global global vs local variables
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source