
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Declare a Variable in Python Without Assigning a Value
Python variable is a name that you give to something in your program. It basically helps you keep track of objects, which are things like numbers, words or any other data. When you assign an object to a variable, you can use that name to refer to that object later. The data, on the other hand, is still contained within the object.
For example, a is assigned the value 100. Here 'a' is a variable.
a = 100
This assignment creates an integer object with the value 100 and assigns the variable a to point to that object.
In the above example, we assigned a value(100) to the variable, but in this article, we will see how to declare a variable without assigning any value.
Using the None keyword
As Python is dynamic, there is no need to declare variables; they are created automatically in the first scope to which they are allocated. It is only necessary to use a standard assignment statement.
The None is a special object of type NoneType. It refers to a value that is either NULL or not accessible. If we do not want to give a variable a value, we can set it to None.
Example
In the below example, the variable value1 is declared and assigned with an integer value but the another variable value2 is declared and assigned with a None that will not store any value.
# Define a variable with an integer value and another with None value1 = 10 print(type(value1)) value2 = None print(value2)
Output
When you run the program, it will show this output -
<type'int'> None
Using Empty Strings or Empty Lists
In Python, you can create variables like strings and lists without giving them any value at first. This means you're setting up the variables, but they don't have anything inside them yet.
Example
In the below example, we have declared a list with no elements in it and a variable with no characters in it.
# Define an empty list and a variable with no value lst=[] var = "" print(lst) print(var)
Output
After running the program, you will get this result -
[]
Using Type Hints (Type Annotations)
You can use type hints to add information about what kind of data a variable should have in Python 3.6 and above versions. You can tell what type of data a variable will hold without giving it a value right away with type hints.
Example
In the following example, we have created a variable and annotated it:
#variable with no values variable_without_value: str print(str)
Output
This output will be displayed when the program runs -
<class 'str'>