In this Python tutorial, we will discuss how to check if the variable is an integer in Python. We will also check:
- Python check if the variable is an integer
- Python check if the variable is a string
- Check if the variable is a list or a tuple in python
- Check if the variable is not null in python
- Get the type of object in python
- Check if the variable exists python
- UnboundLocalError: local variable referenced before assignment python
- Python greater than operator
- Python less than operator
- Python check if user input is a number or string
- Python import variable from another file
- Python dynamic variable name
Python check if the variable is an integer
To check if the variable is an integer in Python, we will use isinstance() which will return a boolean value whether a variable is of type integer or not.
- How to create a string in Python + assign it to a variable in python
- How to create a variable in python
- Function in Python
Example:
my_variable = 56
print(isinstance(my_variable, int))
After writing the above code (python check if the variable is an integer), Ones you will print ” isinstance() “ then the output will appear as a “ True ”. Here, isinstance() will check if a variable is an integer or not and if it is an integer then it will return true otherwise false.
You can refer to the below screenshot to check if the variable is an integer.
Python check if the variable is a string
In python, to check if the variable is a string, we will use built-in function isinstance() which will return a boolean value whether a variable is of type string or not.
Example:
my_string = "Welcome"
print(isinstance(my_string, str))
After writing the above code (python check if the variable is a string), Ones you will print ” isinstance() “ then the output will appear as a “ True ”. Here, isinstance() will check if a variable is a string or not, and if it is a string then it will return true otherwise false.
You can refer to the below screenshot to check if the variable is a string.
- Remove character from string Python
- How to handle indexerror: string index out of range in Python
- Python convert list to string
- Python String Functions
Check if the variable is a list or a tuple in python
To check if the variable is a list or a tuple in Python, we can use type() to check the data type of a variable in python.
Example:
value = ['Tim', 'John', 'Mark']
if type (value) == tuple:
print('value is a tuple')
else:
print('value is list)
After writing the above code (python check if the variable is a list or a tuple in python), Ones you will print then the output will appear as a “ value is list ”. Here, type(value) is the variable name which is used to check if a variable is a list or tuple.
You can see the below screenshot if the variable is a list or a tuple in python
Check if the variable is not null in python
To check if the variable is not null in Python, we will use if condition to check whether the variable is none or not none.
Example:
value = 'New to python'
if value is not None:
print('Value is not null')
After writing the above code (python check if the variable is not null in python), Ones you will print then the output will appear as a “ Value is not null ”. Here, the if condition is used to check if the value is none or not.
You can see the below screenshot if the variable is not null in python
Get the type of object in python
In python, to get the type of object in python, we will use type() and the parameter is passed to type for getting the type of an object.
Example:
new_set = {'X', 'Y', 'Z'}
print("Type is : ",type(new_set))
After writing the above code (get the type of object in python), Ones you will print then the output will appear as a “ Type is: <class ‘set’> ”. Here, the type() is used to get the class type of variable.
You can see the below screenshot to get the type of object
Check if the variable exists python
In python, if a variable exists then it is defined locally or globally. A variable defined inside the function is called a local function and the variable defined outside the function is called a global variable.
Example:
def fun1():
my_variable = 10
my_local = "my_variable" in locals()
print(my_locals)
fun1()
After writing the above code (check if the variable exists python), Ones you will print “my_local” then the output will appear as a “ True ” because it is defined locally. Here, we use locals() to check whether the variable is local or global.
You can see the below screenshot check if the variable exists python
UnboundLocalError: local variable referenced before assignment python
The local variable referenced before assignment this error is raised when we try to use a variable before it has been assigned in the local scope.
Example:
val = 10
def fun():
print(val)
val = val+1
fun()
After writing the above code we get an error. Ones you will print ” val ” then the error will appear as a “ UnboundLocalError: local variable ‘ val ‘ referenced before assignment “. This error is raised because we assign a value to a variable that does not have a local scope so, it throws an error.
You can see the below screenshot local variable referenced before assignment python
Here, we will see how to resolve the ” local variable referenced before assignment “ error. We need to define it in the global context in order to solve this error.
Example:
val = 10
def fun():
global val
print(val)
val = val+1
print(val)
fun()
After writing the above code. Ones you will print ” val ” then the output will appear as a “ 10 11 “. Here, after defining the function we will specify the global variable explicitly in the function to manipulate the value. We will print the ‘ val ‘, so my global variable value is 10 and it gets incremented to 1.
You can see the below screenshot UnboundLocalError is solved.
Python greater than operator
The greater than operator is denoted by (>), it checks whether the left value is greater than the one on the right side or not.
Example:
a1 = 15
a2 = 13
print(a1 > a2)
After writing the above code (python greater than operator), Ones you will print then the output will appear as a “ True ”. Here, the left value “a1” is greater than the right “a2” so, it returns true.
You can see the below screenshot for python greater than operator
Python less than operator
The less than operator is denoted by (<) in Python, it checks whether the left-side value is lesser than that on the right side or not.
Example:
a1 = 20
a2 = 44
print(a1 < a2)
After writing the above code (python less than operator), Ones you will print then the output will appear as a “ True ”. Here, the left value “a1” is less than the right “a2” so, it returns true.
You can see the below screenshot for python less than operator
Python check if user input is a number or string
Using isdigit() method we can check, whether the user entered input is a number or string. If it is a number then it will return the input is number else it will return input is a string.
Example:
val = input("Enter the number: ")
if val.isdigit():
print("User input is number")
else:
print("User input is string")
After writing the above code (python check if user input is a number or string), Ones you will print then the output will appear as an “ Enter the number: 22 User input is number ”. Here, “isdigit()” will return true if the entered number is a digit.
You can see the below screenshot python check if user input is a number or string
Python import variable from another file
- Importing a file gives the current program access to variables and functions defined in that file. We will use import to retrieve all variables and function from another file.
- Let’s take two files N.py and M.py in the same folder.
Example:
# In N.py
n = 12
print('Saying hello in N')
You can see the below screenshot:
Example:
# In M.py
from N import n
print('The value of n is %s in M' % str(n))
After writing the above code (python import variable from another file), Ones on executing M.py the output will appear as ” Saying hello in N The value of n is 12 in M “. The line from N import n is executed. It takes the referenced script(N.py). So, M.py has the variable from another file.
You can see the below screenshot for python import variable from another file
Python dynamic variable name
To create a dynamic variable name, we will use a dictionary. A dictionary has a key and value whereas, a key act as a variable name to its corresponding value, and it can be a variety of type string or integers.
Example:
name = "Joe"
value = False
my_dict = {name: value}
print(my_dict["Joe"])
After writing the above code (python dynamic variable name), Ones you will print “my_dict[“Joe”]” the output will appear as ” False “. Here, we can take the variable name dynamically.
You can see the below screenshot python dynamic variable name
In this way, we can check if the variable is an integer in python.
You may like the following Python tutorials:
- How to concatenate strings in python
- How to convert an integer to string in python
- How to split a string using regex in python
- ValueError: math domain error
- Python – stderr, stdin and stdout
- Python GUI Programming
- Python input and raw_input function
In this tutorial, we learned how to check variable in python.
- Python check if the variable is an integer
- Python check if the variable is a string
- Check if the variable is a list or a tuple in python
- Check if the variable is not null in python
- Get the type of object in python
- Check if the variable exists in python
- UnboundLocalError: local variable referenced before assignment python
- Python greater than operator
- Python less than operator
- Python check if user input is a number or string
- Python import variable from another file
- Python dynamic variable name
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.