Open In App

Declare variable without value - Python

Last Updated : 12 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A variable is a container that stores a special data type. Unlike C/C++ or Java, a Python variable is assigned a value as soon as it is created, which means there is no need to declare the variable first and then later assign a value to it. This article will explore How to Declare a variable without value.

Using None

A common way to declare a variable without a value in Python is to initialize it with None. The None object in Python represents the absence of a value or a null value.

Python
a = None

print(a)
print(type(a))

Output
None
<class 'NoneType'>

Explanation:

  • Variable 'a' is declared with the 'None' keyword which is equallent to 'null'. It means that the variable is not assigned any value.
  • The type() function defines the type of the object created.

Declaring Empty String

A string is a set of characters enclosed within either single or double quotes. The following example demonstrait how to declare a string without assigning a value to it.

Python
s = ""

print(s)
print(type(s))

Output
<class 'str'>

Declaring Empty Data Structures

Apart from strings, we can also declare data structures like lists, dictionary, and tuple without a value in Python.

Python
# empty list
a = []
print(a)
print(type(a))

# empty dictionary
b = {}
print(b)
print(type(b))

# empty tuple
c = ()
print(c)
print(type(c))

Output
[]
<class 'list'>
{}
<class 'dict'>
()
<class 'tuple'>

Next Article
Article Tags :
Practice Tags :

Similar Reads