Declare a Variable Without Assigning a Value in Python

As a developer, I was working on a large data processing project for a client in California. I faced a situation where I needed to declare variables first and only assign values later in the program.

If you have been coding in Python for a while, you know that, unlike some other programming languages (like Java or C++), Python does not allow you to declare a variable without assigning it a value.

But here’s the good news: there are a few practical workarounds that I use in my projects. In this tutorial, I’ll share with you the different ways I declare a variable without assigning it an actual value right away.

Method 1: Use None in Python

In Python, to declare a variable without assigning it a value, set it to None. The None keyword in Python is used to define variables that store a null or not accessible value.

USA_coordinates = None
print(USA_coordinates)

Output:

None

You can see the output in the screenshot below.

How to Declaring a Variable Without Assigning it a Value in Python

This method initializes a variable with None, representing the absence of a value in Python.

Method 2: Use an Empty Python List

If you need to declare a variable of list type, declare an empty list. An empty list in Python does not store any elements. However, it still enables you to call list-specific methods.

Texas_population = []

print(Texas_population)
print(type(Texas_population))

Output:

[]
<class 'list'>

You can see the output in the screenshot below.

Declare a variable without assigning it a value with empty list in Python

This method declares a variable as an empty list, allowing list operations without storing any elements.

Method 3: Use an Empty Dictionary in Python

In Python, a dictionary is a mapping of key-value pairs. If you need to declare an empty dictionary, use an empty set of curly braces {}.

cities = {}

print(cities) 

print(type(cities))

Output:

{}
<class 'dict'>

You can see the output in the screenshot below.

Python program to declare a variable without assigning it a value with an empty dictionary

This method declares a variable as an empty dictionary, ready to store key-value pairs.

Method 4: Use an Empty Set in Python

This is another way to declare a variable without assigning it a value with an empty set. In Python, a set object is an unordered collection of unique elements.

car_brands = set()

print(car_brands)
print(type(car_brands))

Output:

set()
<class 'set'>

You can see the output in the screenshot below.

How to declare a variable without assigning it a value with an empty set

This method initializes a variable as an empty set, ready to store unique elements.

Method 5: Use an empty tuple() Class in Python

In Python, a tuple is an ordered, immutable collection of elements. I will use the tuple() class to achieve the result in this approach.

my_variable = tuple()

print(my_variable) 
print(type(my_variable))

Output:

()
<class 'tuple'>

You can see the output in the screenshot below.

Declare a variable without assigning it a value with an empty tuple() class in Python

This method creates an empty tuple, an immutable ordered collection ready to store elements.

I hope you like this article. Here, I have covered how to declare a variable without assigning it a value in Python with different approaches and descriptive examples.

I have explained various ways to declare a variable without assigning it a value in Python, such as using None, with an empty list, empty dictionary, empty set() class, and empty tuple() class.

Every approach has advantages and use cases; choose according to your requirements.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.