16
loading...
This website collects cookies to deliver better user experience
# Declare a variable and initialize it
v = 1
print(v)
1
(Integer)# Declare a variable and initialize it
v = 1
print(v)
# re-declaring the variable works
v = "abc"
print(v)
abc
(String)# ERROR: variables of different types cannot be combined
print( "abc" + 1 )
error
TypeError: can only concatenate str (not "int") to str# Global vs. local variables in functions
Variable = "ABC"
#Define Some Function
def Function():
Variable = "XYZ"
print(Variable)
#End of Function
Function()
print(Variable)
XYZ
ABC
# Global vs. local variables in functions
Variable = "ABC"
#Define Some Function
def Function():
global Variable
Variable = "XYZ"
print(Variable)
#End of Function
Function()
print(Variable)
XYZ
XYZ
Variable = "Local"
affects the Variable that's outside the function.# Global vs. local variables in functions
Variable = "ABC"
#Define Some Function
def Function():
global Variable
Variable = "XYZ"
print(Variable)
#End of Function
Function()
print(Variable)
#Delete Variable Definition
del Variable
print(Variable)
XYZ
XYZ
NameError: name 'Variable' is not defined