Python check if the variable is an integer

By: On:

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 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.


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 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.

Python check if the variable is a string

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 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

Check 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

Get the type of object in python

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

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

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.

unboundlocalerror local variable referenced before assignment python

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 greater than operator
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 less than operator
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 check if user input is a number or string
Python check if user input is a number or string

In this way, we can check if the variable is an integer in python.


You may like the following Python tutorials:

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

Leave a Comment