How to Declare a Variable Without Assigning a Value in Python?

One of my team members recently got a requirement while doing some Python programming: to declare a variable without assigning a value. I suggested multiple methods. In this tutorial, I will show you different methods to declare a variable without assigning a value in Python with examples.

In Python, you can declare a variable without assigning it a value by using the None keyword. None is a special constant that represents the absence of a value or a null value. For example, you can declare a variable like first_name = None and assign it a value later in your code, such as first_name = "John".

Declare a Variable Without Assigning a Value in Python

Now, let me show you different methods to declare a variable without assigning a value in Python.

1. Using None

In Python, the most common way to declare a variable without assigning it a value is by using the None keyword. None is a special constant in Python that represents the absence of a value or a null value.

Let me show you an example of this.

# Declaring variables without assigning values
first_name = None
last_name = None
age = None

# Later in the code, these variables can be assigned values
first_name = "John"
last_name = "Doe"
age = 30

print(f"Name: {first_name} {last_name}, Age: {age}")

In this example, we initially declare three variables (first_name, last_name, and age) without assigning any values to them. Later, we assign values to these variables and print them out.

Here is the exact output you can see in the screenshot below:

Declare a Variable Without Assigning a Value in Python

Check out How to Use Global Variables in Python Classes?

2. Using Empty Data Structures

Sometimes, you might want to initialize a variable with an empty data structure, such as an empty list, dictionary, or set. This can be useful when populating these structures later in your code.

Here is an example and the complete code.

# Declaring variables with empty data structures
shopping_list = []
user_info = {}
unique_ids = set()

# Later in the code, these variables can be populated
shopping_list.append("Milk")
shopping_list.append("Eggs")
user_info["name"] = "Jane Doe"
user_info["email"] = "jane.doe@example.com"
unique_ids.add(101)
unique_ids.add(102)

print(f"Shopping List: {shopping_list}")
print(f"User Info: {user_info}")
print(f"Unique IDs: {unique_ids}")

In this example, we declare three variables with empty data structures: an empty list (shopping_list), an empty dictionary (user_info), and an empty set (unique_ids). Later, we populate these data structures and print their contents.

Here you can see the output in the screenshot below:

python declare a variable without assigning value

Check out How to Initialize an Empty Variable in Python?

3. Using Type Annotations (Python 3.6+)

With the introduction of type hints in Python 3.6, you can declare a variable with a specific type without assigning a value to it. This is particularly useful for improving code readability and for static type checkers like MyPy.

Let me show you an example to help you understand it better.

# Declaring variables with type annotations
from typing import Optional

first_name: Optional[str] = None
last_name: Optional[str] = None
age: Optional[int] = None

# Later in the code, these variables can be assigned values
first_name = "Alice"
last_name = "Smith"
age = 25

print(f"Name: {first_name} {last_name}, Age: {age}")

In this example, we use type annotations to declare three variables with specific types (str and int) without assigning values to them initially. We use Optional from the typing module to indicate that these variables can either be of the specified type or None.

Read How to Access a Local Variable Outside a Function in Python Without Using global?

Example – Declare a Variable Without Assigning a Value in Python

Now, let me show you a real-time example of declaring a variable without assigning a value in Python.

Suppose you’re developing a program to manage voter information for an upcoming election. You might need to declare variables for voter details that will be populated later.

Here is the complete Python code.

# Declaring variables for voter information
voter_id = None
first_name = None
last_name = None
state = None

# Later in the code, these variables can be assigned values
voter_id = 123456
first_name = "Emma"
last_name = "Johnson"
state = "California"

print(f"Voter ID: {voter_id}, Name: {first_name} {last_name}, State: {state}")

In this example, we declare variables for voter_id, first_name, last_name, and state without assigning initial values. Later, we assign values to these variables to store voter information.

I executed the above Python code, and you can see the output in the screenshot below:

Declare a Variable Without Assigning a Value in Python Example

Conclusion

I hope you now know how to declare a variable without assigning a value in Python using different methods like using None, empty data structures, type annotations, etc. However, I recommend using the None method for this.

You may like the following tutorials:

Leave a Comment