0

Explain the result

f=[1, 2, 4, 6, 8] print (str(f [2] - f [4] % 3) * 3)

9th Jan 2020, 11:13 AM
Владислав Китаев
Владислав Китаев - avatar
2 odpowiedzi
+ 15
Владислав Китаев f[2] the third element of list which is f[2] = 4 f[4] fifth element of list which is f[5] = 8 str() convert all things in string format. So str(4-8%3) => str(4-2) => str(2) Where: 8%3 is returning remainder which is 2 str(2)*3 any string multiplication with integer will repeat the number of times so here it's 3 so 3 Time string 2 is printed which is "222" so output printed as 222.
9th Jan 2020, 11:21 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 1
Here's an explanation, step by step. The first thing we're going to do is get the values of f[2] and f[4]. f[2] is equal to 4, and f[4] is equal to 8. Now, the expression to calculate looks like this: str(4 - 8 % 3) * 3 According to the order of operations, we should calculate the modular value of 8 by 3 first, which returns 2 as that is the remainder after dividing 8 by 3. Then, we can subtract 2 from 4. Now, the expression looks like this: str(2) * 3 Now we convert 2 to a string, and multiply it by 3. This returns "222" because when multiplying a string by a integer, we'll get the same string repeated integer times. Therefore, the result is "222".
9th Jan 2020, 11:27 AM
Jianmin Chen
Jianmin Chen - avatar