Write a Python program to determine if a variable is defined or not.

Write a Python program to determine if a variable is defined or not.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
try:
  x = 1
except NameError:
  print("Variable is not defined....!")
else:
  print("Variable is defined.")
try:
  y
except NameError:
  print("Variable is not defined....!")
else:
  print("Variable is defined.")
  

Final output

Leave a Comment