How do you confirm if an object is iterable using code?

How do you confirm if an object is iterable using code?

Program
This is how you can use code to check if an item is iterable or not. We are using the iter() function. When you pass an item that is not iterable as an argument to the iter() function, it returns a TypeError. Below, we write a short script that checks if a given object is an iterable.
def myfun():
    arr = ['i', 'love', 'working', 'with', 'Python']
    try:
        iter_check = iter(arr)
    except TypeError:
        print('Object a not iterable')
    else:
        print('Object a is iterable')
                 
if __name__=="__main__":
    myfun()


Output:

Object a is iterable


Program
def myfun():
    b = 24.24
    try:
        iter_check = iter(b)
    except TypeError:
        print('Object a not iterable')
    else:
        print('Object a is iterable')
                 
if __name__=="__main__":
    myfun()


Output:

Object a not iterable



More Questions


49 . How do you confirm if an object is iterable using code?
50 . Write a python program to sort list with sorted & lambda
51 . Write a python program to access news using python
52 . Write a python program to print lowercase names
53 . Write a python program to Find Index Using Enumerate
54 . Write a python program to checking if a string is empty or not ?
55 . Write a python program to merge nested lists ?
56 . Write a python program to Checking if a File Exists or Not ?
57 . Write a python program to convert uppercase strings and we want to convert them into lowercase strings and remove duplicates ?
58 . Write a python program to display list of numbers and we want to return all the numbers from the list that are divisible by 2 and remove duplicates at the same time.
59 . Write a python program to calculate average using python *args ?



For latest job updates join Telegram Channel: https://t.me/sateeshm