Last updated: Apr 13, 2024
Reading timeยท5 min
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.
my_variable = None print(my_variable) # ๐๏ธ None print(type(my_variable)) # ๐๏ธ <class 'NoneType'>
I've also written a detailed guide on how to check if a variable is None in Python.
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:
None
to free up
memory.You can initialize a variable to None
and then reassign it, giving it another
value.
my_variable = None print(my_variable) # ๐๏ธ None my_variable = 'bobbyhadz.com' print(my_variable) # ๐๏ธ bobbyhadz.com
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.
If you need to declare a variable of string type without assigning it a value, set it to an empty string.
my_variable = '' print(repr(my_variable)) # ๐๏ธ ''
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.
None
.This is because you are still able to call string methods on the empty string without getting any errors.
my_variable = '' print(repr(my_variable)) # ๐๏ธ '' print(repr(my_variable.upper())) # ๐๏ธ '' my_variable = 'bobbyhadz.com' print(my_variable.upper()) # ๐๏ธ BOBBYHADZ.COM
On the other hand, if you try to call a string method on a None
object,
you'd get an AttributeError.
Python also has a relatively new syntax called variable annotations.
This syntax enables you to type variables and declare them without an initial value.
first_name: str # No initial value age: int # No initial value list_of_strings: list[str] # No initial value
These variables are typed but are not defined.
If you try to access any of them, you would get an error:
first_name: str # No initial value age: int # No initial value list_of_strings: list[str] # No initial value # โ๏ธ NameError print(age)
However, if you hover over any of these variables you will be able 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.
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.
my_variable = [] print(my_variable) # ๐๏ธ [] print(type(my_variable)) # ๐๏ธ <class 'list'>
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.
my_variable = [] print(my_variable) # ๐๏ธ [] my_variable.append('bobby') my_variable.extend(['hadz', '.', 'com']) print(my_variable) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
If you need to declare an empty dictionary, use an empty set of curly braces
{}
.
my_variable = {} print(my_variable) # ๐๏ธ {} print(type(my_variable)) # ๐๏ธ <class 'dict'>
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.
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.
my_variable = {} print(my_variable) # ๐๏ธ {} print(list(my_variable.keys())) # ๐๏ธ [] print(list(my_variable.values())) # ๐๏ธ []
If you need to declare a variable that stores an empty Set
, you have to use
the set() class.
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.
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)
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.
If you need to declare a variable that stores an empty tuple, use an empty set
of parentheses ()
.
my_variable = () print(my_variable) # ๐๏ธ () print(type(my_variable)) # ๐๏ธ <class 'tuple'>
A tuple is an ordered, immutable collection of elements.
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.
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.
0
The closest thing to declaring a variable without assigning it a value when it
comes to numbers is setting the variable to 0
.
my_variable = 0 print(my_variable) # ๐๏ธ 0 print(type(my_variable)) # ๐๏ธ <class 'int'>
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
.
my_variable = 0.0 print(my_variable) # ๐๏ธ 0.0 print(type(my_variable)) # ๐๏ธ <class 'float'>
I've also written an article on how to clear all variables in a Python script.
You can learn more about the related topics by checking out the following tutorials: