Python: Declaring a Variable without assigning it a Value

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
5 min

banner

# Table of Contents

  1. Python: Declaring a Variable without assigning it a Value
  2. Declaring a variable that stores an empty String
  3. Using Variable Annotations in Python
  4. Declaring a variable that stores an empty List
  5. Declaring a variable that stores an empty Dictionary
  6. Declaring a variable that stores an empty Set
  7. Declaring a variable that stores an empty Tuple
  8. Initializing a variable to 0

# Python: Declaring a Variable without assigning it a Value

To declare a variable without assigning it a value in Python set the variable to None.

The None keyword is used to define variables that store a null value and are likely to get reassigned to another value in the future.

If you need to declare a variable of a specific type without assigning it a value, you can set it to an empty string, an empty list, an empty dictionary, etc.

Here is an example of declaring a variable that stores a None value.

main.py
my_variable = None print(my_variable) # ๐Ÿ‘‰๏ธ None print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'NoneType'>

declare variable setting it to none

The code for this article is available on GitHub

I've also written a detailed guide on how to check if a variable is None in Python.

main.py
my_variable = None if my_variable is None: print('It is None') else: print('It is NOT None')

The None value in Python is very similar to the null value in other languages.

It is mostly used when:

  1. Declaring a variable whose value is not yet known or cannot be computed at the time of declaration.
  2. Setting a variable that previously stored a value to None to free up memory.

You can initialize a variable to None and then reassign it, giving it another value.

main.py
my_variable = None print(my_variable) # ๐Ÿ‘‰๏ธ None my_variable = 'bobbyhadz.com' print(my_variable) # ๐Ÿ‘‰๏ธ bobbyhadz.com

initialize to none and reassign to another value

The code for this article is available on GitHub

We initially set the variable to None but later set it to the string "bobbyhadz.com".

You can think of the None value as a placeholder that is used until you have the actual value that should be stored.

None objects don't have useful attributes and built-in methods (such as str.upper() or list.append()).

If you want to read more about using None values in Python, check this article out.

# Declaring a variable that stores an empty String

If you need to declare a variable of string type without assigning it a value, set it to an empty string.

main.py
my_variable = '' print(repr(my_variable)) # ๐Ÿ‘‰๏ธ ''

initialize variable to empty string

The code for this article is available on GitHub

The empty string value is useful when you need to declare a variable that you know will store a string in the future but don't yet have the actual string.

For example, you might need to fetch the string from an API, query a database or construct it by concatenating other strings.

It is much better to initialize a variable that you know will store a string in the future to an empty string than to set it to None.

This is because you are still able to call string methods on the empty string without getting any errors.

main.py
my_variable = '' print(repr(my_variable)) # ๐Ÿ‘‰๏ธ '' print(repr(my_variable.upper())) # ๐Ÿ‘‰๏ธ '' my_variable = 'bobbyhadz.com' print(my_variable.upper()) # ๐Ÿ‘‰๏ธ BOBBYHADZ.COM

still able to call string methods on empty string

On the other hand, if you try to call a string method on a None object, you'd get an AttributeError.

# Using Variable Annotations in Python

Python also has a relatively new syntax called variable annotations.

This syntax enables you to type variables and declare them without an initial value.

main.py
first_name: str # No initial value age: int # No initial value list_of_strings: list[str] # No initial value

using variable annotations in python

The code for this article is available on GitHub

These variables are typed but are not defined.

If you try to access any of them, you would get an error:

main.py
first_name: str # No initial value age: int # No initial value list_of_strings: list[str] # No initial value # โ›”๏ธ NameError print(age)

nameerror name is not defined

However, if you hover over any of these variables you will be able to see its type.

hover over variable to see its type

You can read more about the syntax for variable annotations on this page.

However, note that this syntax is used very rarely in Python.

# Declaring a variable that stores an empty List

If you need to declare a variable of list type but don't yet have any of the items that the list will store, declare an empty list.

main.py
my_variable = [] print(my_variable) # ๐Ÿ‘‰๏ธ [] print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'list'>
The code for this article is available on GitHub

A list is an ordered collection of elements that you can access by index, e.g. print(my_list[0]).

However, make sure you don't try to access a list at an index that is out of range because you'd get an IndexError.

An empty list is one that doesn't store any elements, however, it still enables you to call list-specific methods.

main.py
my_variable = [] print(my_variable) # ๐Ÿ‘‰๏ธ [] my_variable.append('bobby') my_variable.extend(['hadz', '.', 'com']) print(my_variable) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

still able to call list methods on empty list

# Declaring a variable that stores an empty Dictionary

If you need to declare an empty dictionary, use an empty set of curly braces {}.

main.py
my_variable = {} print(my_variable) # ๐Ÿ‘‰๏ธ {} print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'dict'>

declaring variable that stores empty dictionary

The code for this article is available on GitHub

A dictionary is a mapping of key-value pairs.

You can declare an empty dictionary and use the bracket notation syntax to add key-value pairs to it later on.

main.py
my_variable = {} print(my_variable) # ๐Ÿ‘‰๏ธ {} my_variable['name'] = 'bobby' my_variable['age'] = 30 my_variable['site'] = 'bobbyhadz.com' # {'name': 'bobby', 'age': 30, 'site': 'bobbyhadz.com'} print(my_variable) print(my_variable['name']) # ๐Ÿ‘‰๏ธ bobby

The same bracket notation syntax is used if you need to get a dictionary value by key.

As in the previous examples, you are still able to call dictionary methods on an empty dict object.

main.py
my_variable = {} print(my_variable) # ๐Ÿ‘‰๏ธ {} print(list(my_variable.keys())) # ๐Ÿ‘‰๏ธ [] print(list(my_variable.values())) # ๐Ÿ‘‰๏ธ []

# Declaring a variable that stores an empty Set

If you need to declare a variable that stores an empty Set, you have to use the set() class.

main.py
my_variable = set() print(my_variable) # ๐Ÿ‘‰๏ธ set() print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'set'>

Make sure you don't try to use an empty set of curly braces {} to attempt to declare a set because that way you declare a dictionary.

A set object is an unordered collection of unique elements.

main.py
my_variable = set() my_variable.add('bobby') my_variable.add('bobby') my_variable.add('bobby') my_variable.add('hadz') my_variable.add('com') # ๐Ÿ‘‡๏ธ {'hadz', 'com', 'bobby'} print(my_variable)

calling set methods on empty set

Notice that I added the same value multiple times to the set.

However, set objects don't contain any duplicates, so they automatically get discarded.

The insertion order of elements is also not preserved.

# Declaring a variable that stores an empty Tuple

If you need to declare a variable that stores an empty tuple, use an empty set of parentheses ().

main.py
my_variable = () print(my_variable) # ๐Ÿ‘‰๏ธ () print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'tuple'>

declaring empty tuple

The code for this article is available on GitHub

A tuple is an ordered, immutable collection of elements.

main.py
my_variable = ('bobby', 'hadz', 'com') print(my_variable) # ๐Ÿ‘‰๏ธ ('bobby', 'hadz', 'com') print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'tuple'>

As opposed to lists, tuples cannot be changed, so declaring an empty tuple doesn't make much sense.

You can also use the tuple class to achieve the same result.

main.py
my_variable = tuple() print(my_variable) # ๐Ÿ‘‰๏ธ () print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'tuple'>

Tuples are immutable, so they have much fewer built-in methods than lists.

# Initializing a variable to 0

The closest thing to declaring a variable without assigning it a value when it comes to numbers is setting the variable to 0.

main.py
my_variable = 0 print(my_variable) # ๐Ÿ‘‰๏ธ 0 print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'int'>

initializing variable to 0

The code for this article is available on GitHub

As shown in the code sample, when you initialize a variable to 0, its type is int.

If you need to initialize a variable to 0 and have its type be float, set it to 0.0.

main.py
my_variable = 0.0 print(my_variable) # ๐Ÿ‘‰๏ธ 0.0 print(type(my_variable)) # ๐Ÿ‘‰๏ธ <class 'float'>

initialize variable to zero with type float

I've also written an article on how to clear all variables in a Python script.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev