Find and write the output of the following python code: 2 List = ["P", 20, "R", 10, "S", 30] Times = 0 Alpha = ""Sum = 0 for I in range(1, 6, 2): Times = Times + I Alpha = Alpha + List[I -1] + "#" Sum = Sum + List[I] print(Times, Sum, Alpha)
Answers
Explanation:
the output of the following python code: 2 List = ["P", 20, "R", 10, "S", 30] Times = 0 Alpha = ""Sum = 0 for I in range(1, 6, 2): Times = Times + I Alpha = Alpha + List[I -1] + "#" Sum = Sum + List[I] print(Times, Sum, Alpha)
Given :
List = ["P" , 20 , "R" , 10 , "S" , 30]
Times = 0
Alpha = ""
Sum = 0
for I in range(1 , 6 , 2):
Times = Times + I
Alpha = Alpha + List[I -1] + "#"
Sum = Sum + List[I]
print(Times, Sum, Alpha)
Output :
1 20 P#
4 30 P#R#
9 60 P#R#S#
Explanation :
Step 1 :
When I = 1 < 6 True
Time = 0 + 1 = 1
Alpha = "" + List[1 - 1] + "#" = "" + List[0] + "#" = "" + "P" + "#" = "P#"
Sum = 0 + List[1] = 0 + 20 = 20
print(Times, Sum, Alpha) = 1 20 P#
Step 2 :
When I = 1 + 2 = 3 < 6 True
Time = 1 + 3 = 4
Alpha = "P#" + List[3 - 1] + "#" = "P#" + List[2] + "#" = "P#" + "R" + "#" = "P#R#"
Sum = 20 + List[3] = 20 + 10 = 30
print(Times, Sum, Alpha) = 4 30 P#R#
Step 3 :
When I = 3 + 2 = 5 < 6 True
Time = 4 + 5 = 9
Alpha = "P#R#" + List[5 - 1] + "#" = "P#R#" + List[4] + "#" = "P#R#" + "S" + "#" = "P#R#S#"
Sum = 30 + List[5] = 30 + 30 = 60
print(Times, Sum, Alpha) = 9 60 P#R#S#
Step 4 :
When I = 5 + 2 = 7 < 6 False
Therefore, loop will terminate.
So, output is :
1 20 P#
4 30 P#R#
9 60 P#R#S#