Predict the output of the following code:a = ['Mary', 'had', 'a', 'little', 'lamb']; for i in range(len(a)): ... print i, a[i];Mary,had,a,little,lambAnswer 1 4 lamb 3 l 1 MaryAnswer 21 Mary0 had2 lambAnswer 34 Mary3 had2 little1 lambAnswer 4 0 Mary 1 had 2 a 3 little 4 lamb
Question
Predict the output of the following code:a = ['Mary', 'had', 'a', 'little', 'lamb']; for i in range(len(a)): ... print i, a[i];Mary,had,a,little,lambAnswer 1 4 lamb 3 l 1 MaryAnswer 21 Mary0 had2 lambAnswer 34 Mary3 had2 little1 lambAnswer 4 0 Mary 1 had 2 a 3 little 4 lamb
Solution
The output of the given Python code will be:
0 Mary 1 had 2 a 3 little 4 lamb
This is because the 'range(len(a))' function generates a list of indices for the list 'a', and the 'for' loop iterates over these indices. For each iteration, the value of 'i' is the current index and 'a[i]' is the corresponding element in the list 'a'. The 'print' function then prints these values.
Similar Questions
What will the output of the following program be?L1 = ['Vikas', 'Akshay', 'Sanskar', 'Mahima']print(L1[1][-1])
What will be the output of the following Python code?l=[[1, 2, 3], [4, 5, 6]]for i in range(len(l)): for j in range(len(l[i])): l[i][j]+=10print(l)
What will the following code generate?for i in range(0, 12, 3): print(i)A.0 1 2 3 4 5 6 7 8 9 10 11B.0 3 6 9C.0 1 2 3 4 5 6 7 8 9 10 11 12D.0 3 6
Predict the correct output for the following code. x = "abcdef"i = "a"while i in x: print('i', end = " ")Select one:a b c d e fi i i i i i …no outputa a a a a a …
What will be the output of following?x = ‘abcd’for i in range(len(x)): print(x[i])
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.