Predict the output of the Python code given below:

def Diff(N1,N2):

if N1>N2:

return N1-N2

else:

return N2-N1

NUM= [10,23,14,54,32]

for CNT in range (4,0,-1):

A=NUM[CNT]

B=NUM[CNT-1]

print(Diff(A,B),'#', end=' ')

 

Answer:

Question 24 Choice 1 -Teachoo.png

Output: 22 # 40 # 9 # 13 #

Explanation:

  • The function Diff( ) takes 2 numbers as parameters and finds the difference by subtracting the smaller number from the larger number and returns the difference.
  • NUM is an array of 5 numbers.
  • The for loop iterates from 4 to 0 by decrementing by 1.
  • A and B are assigned 2 consecutive numbers from the array NUM and the difference is calculated and printed. This is repeated for all 2 consecutive numbers in the array.
Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.