Advertisement
# Python - Global variables
# Declaring and initializing global variables
year = 2019
message = 'Hello'
gravity = 9.8
# Defining the function show_values()
def show_values():
print('year : ', year)
print('message : ', message)
print('gravity : ', gravity)
print('planet : ', planet)
# Declaring another global variable before the first statement in the program execution.
planet = 'Earth'
# Calling the show_values() function, this is the first statement in the program execution.
show_values()
year : 2019
message : Hello
gravity : 9.8
planet : Earth
# Python - Shadowing issue while accessing the global variables
year = 2019
message = 'Hello'
gravity = 9.8
# Defining the function show_values()
def show_values():
print('Global year : ', year)
print('Global message : ', message)
print('Global gravity : ', gravity)
# Declaring local variables with the same name as global variables in a function
# does not impact the original values of global variables
def put_values():
year = 2020
message = 'Have a good day'
gravity = 10.8
print('Local year : ', year)
print('Local message : ', message)
print('Local gravity : ', gravity)
# Calling the show_values() function
show_values()
# Calling the put_values() function
put_values()
# Calling the show_values() function
show_values()
Global year : 2019
Global message : Hello
Global gravity : 9.8
Local year : 2020
Local message : Have a good day
Local gravity : 10.8
Global year : 2019
Global message : Hello
Global gravity : 9.8
Advertisement
# Python - Trying to modify the value of a global variable in a function
# Declaring and initializing a global variable
message = 'Hello'
# Defining the function show_message()
def show_message():
message = message + 'and goodbye!'
print('message : ', message)
# Calling the show_message() function
show_message()
Traceback (most recent call last):
File "C:/Users/RadheKrishna/Desktop/Python Programs/global7.py", line 21, in
show_values()
File "C:/Users/RadheKrishna/Desktop/Python Programs/global7.py", line 14, in show_message
message = message + 'and goodbye!'
UnboundLocalError: local variable 'message' referenced before assignment
# Python - Using the global keyword in a function to modify values of global variables
# Defining global variables
nickname = 'Mr Blue'
age = 30
height = 180
# Defining a function which modifies values of global variables
def modify_values():
# Using global keyword to declare the same global variables
global nickname,age, height
# Successfully modifying values of global variables
nickname = nickname + ' Bond'
age = age + 3
height = height + 5
# Defining the function show_values()
def show_values():
print('nicnmame : ', nickname)
print('age : ', age)
print('height : ', height)
print('Original values of global variables :')
# Calling the show_values() function
show_values()
# Calling the modify_values() function to modify global variables
modify_values()
print('Modified values of global variables :')
# Calling the show_values() function to display modified values of global variables
show_values()
Original values of global variables :
nicnmame : Mr Blue
age : 30
height : 180
Modified values of global variables :
nicnmame : Mr Blue Bond
age : 33
height : 185
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement