Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a. Tuples Complete the code to test if the tuple my_object = (4, 3, 2, 1) is Ite

ID: 3920808 • Letter: A

Question

a. Tuples

Complete the code to test if the tuple my_object = (4, 3, 2, 1) is Iterable. Be sure to use a for loop at the end to iterate through all the items in the tuple.

b. Dictionaries

Complete the code to test if the dictionary my_object = {1:'A', 2:'B', 3:"C"} is iterable. Be sure to use a for loop at the end to iterate through all the items in the dictionary.

c. Strings

Complete the code to test if the string my_object = "Philadelphia" is Iterable. Regardless of the answer, use a for loop at the end to iterate through all the items in the string.

CODE:

a. Fill in this table. Demo # 1 Type of Object | Result of call hasattr(my_object, "_iter_') list 2 tuple dictionary 4string

Explanation / Answer

from __future__ import print_function

# There are several ways to test if an object in Python is iterable.
# An iterable object must implement the __iter__ method.
# So we can test if my_object is iterable using:
# hasattr(my_object, '__iter__')

print("Test if each of the following objects is iterable using the hasattr method to check for __iter__.")

print(" DEMO 1: Testing if a list is iterable.")
my_object = [1, 2, 3, 4] # is a list iterable?
print(" Using 'hasattr'to test the list:", my_object)

result = hasattr(my_object, "__iter__") # True
print(" Show Result: Is the list iterable? %s" % result)

# Now show you can step through the items in your iterable object using a for loop
print(" The items in our iterable are:")
for item in my_object:
    print(" ", item)


print(" DEMO 2: Testing if a tuple is iterable.")
my_object = (4, 3, 2, 1) # is a tuple iterable?
print(" Using 'hasattr'to test the tuple:", my_object)

# a. add code to test if this tuple is iterable using hasattr() and print out the result.
print(" Show Result: Is the tuple iterable? %s" %hasattr(my_object,"__iter__"))

# Now show you can step through the items in your iterable object using a for loop
print(" The items in our iterable are:")
# add for loop here.
my_iter = iter(my_object)
for item in my_iter:
    print(" ",item)


print(" DEMO 3: Testing if a dictionary is iterable.")
my_object = {1:'A', 2:'B', 3:"C"} # is a dictionary iterable?
print(" Using 'hasattr'to test the dictionary:", my_object)

# b. add code to test if this tuple is iterable using hasattr() and print out the result.
print(" Show Result: Is the dictionary iterable? %s" %hasattr(my_object,"__iter__"))

# Now show you can step through the items in your iterable object using a for loop
print(" The items in our iterable are:")
# add for loop here.
my_iter = iter(my_object)
for item in my_iter:
    print(" ",item," : ",my_object[item])


print(" DEMO 4: Testing if a string is iterable using 'hasattr'.")
my_object = "Philadelphia" # is a string iterable?
print(" Using 'hasattr'to test the string:", my_object)

# c. add code to test if this tuple is iterable using hasattr() and print out the result.
print(" Show Result: Is the string iterable? %s" %hasattr(my_object,"__iter__"))


# Add code to test if the string has the "__getitem__" attribute/method.
print(" Show Result: Is string has getitem? %s" %hasattr(my_object,"__getitem__"))

# Regardless of the answer, show you can step through the items in the string using a for loop
print(" The characters in our string are:")
# add for loop here.
my_iter = iter(my_object)
for item in my_iter:
    print(" ",item)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote