- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to declare a variable in Python without assigning a value to it?
Python variable is a symbolic name for an object that serves as a reference or pointer. You can refer to an object by its name once it has been assigned to a variable. The data, on the other hand, is still contained within the object.
For example, a is assigned the value 100. ‘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 have assigned a value(100) to the variable, but in this article, we will see how to declare a variable without assigning any value to it.
Using the None keyword
As Python is dynamic, there is no need to declare variables; they have been created automatically in the first scope to which they are allocated. It's 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 don't want to give a variable a value, we can set it to None.
Example
In the following example, a variable value1 is declared and assigned with an integer value whereas the next variable value2 is declared and assigned with a None that will not store any value.
value1 = 10 print(type(value1)) value2 = None print(value2)
Output
The following output shows the type of the variables.
<type'int'> None
Using Empty Strings or Empty Lists
Variables like strings and lists in python can be initialized with empty values, in a way by doing do, we are declaring these variables without values.
Example
In the below example, we have declared a list with no elements in it and a var variable with no characters in it.
lst=[] var = "" print(lst) print(var)
Output
The following output is obtained when the above code is executed.
[]
Using Type Hints (Type Annotations)
Python type hints are introduced (since python 3.6) to enable static typing. Using this, you annotate variables. By annotating variables, you can declare them without assigning values.
Example
In the following example we have created a variable and annotated it −
variable_without_value: str #variable with no values print(str)
Output
<class 'str'>