Check if the variable is empty or not in Python
In this tutorial, we will learn an easy yet important topic of how to check if the variable is empty or not in Python
The various sequences are:
- Lists
- Tuples
- String
- Dictionary
- Numpy Array
Checking variable empty or not in Python
Now let’s first understand a little bit about these sequences.
LISTS – Check empty or not
- It is a data structure or data sequence in Python.
- Mutable in nature.
- Changeable in nature.
- Syntax to define a list is [ ].
TUPLES – check empty or not
- These are another type of data structure or data sequence.
- It’s immutable in nature.
- It’s irreversible which means no change can occur at any point.
- Syntax to define a list is ( ).
STRING – Check empty or not
- These are another type of data structure.
- It represents Unicode characters.
- [ ] can be used to access strings.
DICTIONARY – Check empty or not
- Dictionary is a random or unordered collection of data values.
- It has a key which is immutable in nature.
- Key should be unique also.
- Syntax to define a dictionary is { }.
NUMPY ARRAY – Check empty or not
- Numpy array is a grid of values.
- Values should be the same.
- Defined using numpy.array[ ].
Now let’s implement them using the python language.
NOTE: Any required explanation have been provided in code itself.
#Syntax to define a list l=[int(x) for x in input().split()] if len(l)==0: # len function to find the length of list print("The List is Empty") else: print("The list is not Empty")
Output:
-NO INPUT FROM USER SIDE- The List is Empty
#Syntax to define a String l=[x for x in input().split()] if len(l)==0: # len function to find the length of list print("The String is Empty") else: print("The String is not Empty")
Output:
-NO INPUT FROM USER SIDE- The String is Empty
#Syntax to define a list l=[int(x) for x in input().split()] a = tuple(l) #Convertig a list to tuple if len(l)==0: # len function to find the length of list print("The Tuple is Empty") else: print("The Tuple is not Empty")
Output:
-NO INPUT FROM USER SIDE- The Tuple is Empty.
#Syntax to define a list d={x for x in input().split()} if(len(d)==0):#To find the length of dictionary print("Dictionary is Empty") else: print("Dictionary is not empty")
Output:
-NO INPUT FROM USER SIDE- Dictionary is Empty.
#Importing Numpy import numpy as np a = np.array([x for x in input().split()])#Syntax for defining Numpy Array if len(a)==0:#Len of Numpy Array print("Numpy array is empty") else: print("Numpy array is not empty")
Also read:
Leave a Reply