Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

image text in transcribed

CODE:

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("\tUsing 'hasattr'to test the list:", my_object) result = hasattr(my_object, "__iter__") # True print("\tShow Result: Is the list iterable? %s" % result) # Now show you can step through the items in your iterable object using a for loop print("\tThe items in our iterable are:") for item in my_object: print("\t\t", item) print(" DEMO 2: Testing if a tuple is iterable.") my_object = (4, 3, 2, 1) # is a tuple iterable? print("\tUsing 'hasattr'to test the tuple:", my_object) # a. add code to test if this tuple is iterable using hasattr() and print out the result.  # Now show you can step through the items in your iterable object using a for loop print("\tThe items in our iterable are:") # add for loop here.  print(" DEMO 3: Testing if a dictionary is iterable.") my_object = {1:'A', 2:'B', 3:"C"} # is a dictionary iterable? print("\tUsing 'hasattr'to test the dictionary:", my_object) # b. add code to test if this tuple is iterable using hasattr() and print out the result.  # Now show you can step through the items in your iterable object using a for loop print("\tThe items in our iterable are:") # add for loop here.  print(" DEMO 4: Testing if a string is iterable using 'hasattr'.") my_object = "Philadelphia" # is a string iterable? print("\tUsing 'hasattr'to test the string:", my_object) # c. add code to test if this tuple is iterable using hasattr() and print out the result.  # Add code to test if the string has the "__getitem__" attribute/method.  # Regardless of the answer, show you can step through the items in the string using a for loop print("\tThe characters in our string are:") # add for loop here.  
a. Fill in this table. Demo # 1 Type of Object | Result of call hasattr(my_object, "_iter_') list 2 tuple dictionary 4string

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions