हिंदी

The code provided below is intended to swap the first and last elements of a given tuple. However, there are syntax and logical errors in the code. Rewrite it after removing all errors.

Advertisements
Advertisements

प्रश्न

The code provided below is intended to swap the first and last elements of a given tuple. However, there are syntax and logical errors in the code. Rewrite it after removing all errors. Underline all the corrections made. 

def swap_first_last(tup)
    if len(tup) < 2:
    return tup
    new_tup = (tup[-1],) + tup[1:-1] + (tup[0])
    return new_tup

result = swap_first_last((1, 2, 3, 4))
print("Swapped tuple: " result)
कोड लेखन
Advertisements

उत्तर

def swap_first_last(tup):
if len(tup) < 2
return tup
new_tup = (tup[-1],) + tup[1:-1] + (tup[0],)
return new_tup

result = swap_first_last((1, 2, 3, 4)
print("Swapped tuple:", result)

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2024-2025 (March) Board Sample Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×