Skip to main content

How to Declare Variables Without Initial Values in Python

In Python, unlike some other languages, you don't explicitly "declare" a variable's type before assigning a value. Variables are created upon first assignment. However, there are conventional ways to indicate that a variable exists but doesn't have a meaningful value yet.

This guide explores these conventions, including using None, empty collections (strings, lists, dicts, etc.), and type hints.

The most common and Pythonic way to declare a variable without assigning a specific initial value is to set it to None. None represents the absence of a value.

my_variable = None
print(my_variable) # Output: None
print(type(my_variable)) # Output: <class 'NoneType'>

# Later in the code, assign the actual value
my_variable = 'tutorialreference.com'
print(my_variable) # Output: tutorialreference.com
  • Use None when you know a variable will eventually hold a value, but you don't have it at the point of declaration.
  • It acts as a placeholder.

Using Empty Collections (Specific Types)

If you know the variable will eventually hold a specific type of collection, initialize it with an empty version of that type. This allows you to immediately use methods associated with that type.

Empty String ('')

Use '' if the variable will later hold string data.

my_string = ''
print(repr(my_string)) # Output: ''
print(my_string.upper()) # Output: '' (String methods work)

my_string = "Hello"
print(my_string.upper()) # Output: HELLO

Empty List ([])

Use [] for variables intended to hold lists.

my_list = []
print(my_list) # Output: []

my_list.append('item1')
print(my_list) # Output: ['item1']

Empty Dictionary ({})

Use {} for variables intended to hold dictionaries.

my_dict = {}
print(my_dict) # Output: {}

my_dict['key'] = 'value'
print(my_dict) # Output: {'key': 'value'}

Empty Set (set())

Use set() (not {}) to create an empty set.

my_set = set()
print(my_set) # Output: set()

my_set.add(1)
print(my_set) # Output: {1}
note

Using {} creates an empty dictionary, not an empty set.

Empty Tuple (())

Use () for empty tuples.

my_tuple = ()
print(my_tuple) # Output: ()

# Note: Tuples are immutable, so you can't add elements later.
# You'd typically create a *new* tuple with the desired elements.

Initializing Numeric Variables to 0

For numeric variables (integers or floats), initializing to 0 or 0.0 is the standard practice if you need a starting numeric value.

count = 0
print(count) # Output: 0
print(type(count)) # Output: <class 'int'>

total = 0.0
print(total) # Output: 0.0
print(type(total)) # Output: <class 'float'>

Variable Annotations (Type Hinting)

Python 3.6+ introduced variable annotations (type hints). While primarily for static analysis tools and code clarity, they look like declarations without assignment. However, they do not actually create the variable.

first_name: str  # Annotation, variable NOT defined yet
age: int # Annotation, variable NOT defined yet

# Trying to access 'age' before assignment will cause an error:
# print(age) # ⛔️ NameError: name 'age' is not defined

# You still need to assign a value later:
age = 30
print(age) # Output: 30
  • Type hints declare the intended type but don't initialize the variable.
  • Use them for type checking and documentation, not as a substitute for initialization with None or an empty value if you need the variable to exist immediately.

Conclusion

While Python doesn't have explicit variable declarations like some other languages, using None is the standard convention for indicating a variable exists but lacks an initial value.

  • For variables intended to hold collections, initializing with an empty version ('', [], {}, set()) is often more practical as it allows immediate use of type-specific methods.
  • Type hints (var: type) declare intended types but don't create the variable itself. Choose the initialization approach that best reflects the variable's purpose and intended type.