Question: a. Test all ten objects provided to see if they pass this test. The first two are done for you. Just paste in the line:
a. Test all ten objects provided to see if they pass this test. The first two are done for you. Just paste in the line:
check_if_iterable(my_object)
b. For each object that is Iterable, print out all its items using a for loop.

CODE:
import collections import math import random # How to test if various objects are Iterable using the collections module # Basic boiler-plate code is: # if isinstance(my_object, collections.Iterable): # my_object is iterable # else: # my_object is NOT iterable # Check each of these objects to see if they are Iterable using Type Checking and the collections module. def check_if_iterable(the_object): result = isinstance(the_object, collections.Iterable) if isinstance(my_object, collections.Iterable): print "Yes, the object '%s' of type '%s' is Iterable!" % (my_object, type(my_object).__name__) else: print "Sorry, the object '%s' of type '%s' is NOT Iterable!" % (my_object, type(my_object).__name__) return result # Example 1 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = 7 # a lucky integer print " Example 1 : This object has type:", type(my_object).__name__ check_if_iterable(my_object) # Example 2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = "seven" # a string print " Example 2 : This object has type:", type(my_object).__name__ check_if_iterable(my_object) # it is Iterable, so print out all the characters. for ch in my_object: print ch, # Example 3 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = range(1, 8) # a list print " Example 3 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. # Example 4 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = (7, 6, 5 , 4, 3, 2, 1) # a tuple print " Example 4 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. # Example 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = math #this object is a module print " Example 5 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. # Example 6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = math.sqrt # this object is a function from the math module print " Example 6 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. # Example 7 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = max # this object is a built-in function print " Example 7 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. # Example 8 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = random.choice # this object is an instancemethod print " Example 8 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. # Example 9 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = {1:'A', 2:'B', 3:"C"} # is a dictionary iterable? print " Example 9 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. # Example 10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * my_object = ['ant', 'bet', 'cat', 'dog', 'eel', ["fish", "fin", "pond"]] # a list of strings print " Example 10 : This object has type:", type(my_object).__name__ # Check is the object is iterable using check_if_iterable() # If it is, use a for loop to print out all its items. Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
