parm530
10/10/2019 - 3:56 PM

Variables

# 
# Example file for variables
#

# Declare a variable and initialize it
f = 0
print(f)

# # re-declaring the variable works
f = "abc"
print(f)

# # ERROR: variables of different types cannot be combined
print("This is a string" + 123)
# Fix
print("This is a string" + str(123))

# Global vs. local variables in functions
def someFunction():
    f = "def"
    print(f)

someFunction()
print(f) # this will error out if you comment out f in the above lines of code

# If you declared f as global in the function someFunction:
# AND comment in line 6 above, then you have made f global to all scopes in
# this file

def someFunction():
    global f 
    f = "def"
    print(f)

someFunction()
print(f)

del f # Deletes the variable named f, used to undefine variables in real time
print(f) # Error due to the above line of code that deletes f