Question-122: When you run below program, then what would happen or printed?
list = [1, 2, 3, 4, 5]
for i in range(len(list) // 2):
list[i], list[len(list) - i - 1] = list[len(list) - i - 1], list[i]
print(list)
- [1, 2, 3, 4, 5]
- Get all Questions and Answer from here
- You need to have paid subscription to access all questions
- Thanks for considering Python Certification Material
Answer: C
Exp:
Python Special
- This is a special way, provided in Python to swap variable.
- You don’t need to use any third temporary variable.
- Simple and straight forward.
first = 'John' second='Mike'
print ("Before Swap ", first, second)
first, second = second, first
print ("After Swap ", first, second) |
Another way to reverse a list.
- Using Python swap feature you can reverse list as below.
- As our list is having 5 elements.
-
You can access to full explanation to question and answer from this page.