A student is learning Python using the interactive interpreter mode. The student issues the commands:
>>> routers=[] >>> switches=[] >>> devices=[\"RT1\", \"RT2\", \"RT3\", SW1\", \"SW2\", \"SW3\"] >>> devices=devices + [\"RT4\", \"SW4\"] >>> for i in devices: if \"R\" in i: routers.append(i) else: switches.append(i) >>> switches
What is the result?
- ['SW4', 'SW3', 'SW2', 'SW1']
- ['SW1', 'SW2', 'SW3']
- ['SW4', 'SW1', 'SW2', 'SW3']
- ['SW4']
- ['SW1', 'SW2', 'SW3', 'SW4']
Explanation: In Python, two lists can be concatenated by using the + (plus sign) operator. The second list appends to the first list and makes a new list. The Python for function is used to loop through the elements in a list and perform an operation on an element. The if statement can be used within the for function to determine the proper action based on the matching condition of each element.
Exam with this question: 1.3.4 Quiz - Python Review
Please login or Register to submit your answer