+ 3
How is the output of 7? Someone please explain me.
a = [1, 2, 3, 5] print(3* a[1] + 3% 2)
4 Antworten
+ 14
Tanvir Hasan ,
Since the values stored in the list starts from 0... so,
a[0]----> 1
a[1]----> 2
a[2]----> 3
a[3]----> 5
Then (3*2+1),
So 7....
Since a[1]=2 and 3%2=1
+ 9
list starts at 0 , so
a[1] = 2
% means remainder, so
3%2 = 1
Put it all together.
3*2 +1 = 7
+ 4
a = [1, 2, 3, 5]
0, 1, 2, 3 <-- index
E.g. a[0] => 1, a[1] => 2, etc
Let's break down the expression. Based on the order of operations, multiplication (*) and remainder (%) are evaluated before addition (+). I've included parentheses here to emphasize the order.
((3*a[1]) + (3%2))
=> ((3*2) + (3%2))
=> (6 + 1)
=> 7
0
Python counts using index
So,
a = [1,2,3,4,5] is five numbers but python counts starting from 0. So
1 = 0,
2 = 1,
3 = 2,
4 = 3,
5 = 4.
While the % sign means modulo ie the remainder of a division. 2%2 = 0.
Then finish it off using BODMAS.Happy day 👋