How is the output of 7? Someone please explain me. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How is the output of 7? Someone please explain me.

a = [1, 2, 3, 5] print(3* a[1] + 3% 2)

14th Aug 2023, 6:47 AM
Tanvir Hasan
Tanvir Hasan - avatar
4 Answers
+ 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
14th Aug 2023, 6:56 AM
Riya
Riya - avatar
+ 9
list starts at 0 , so a[1] = 2 % means remainder, so 3%2 = 1 Put it all together. 3*2 +1 = 7
14th Aug 2023, 6:58 AM
Chris Coder
Chris Coder - avatar
+ 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
14th Aug 2023, 7:00 AM
Mozzy
Mozzy - avatar
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 👋
14th Aug 2023, 10:24 PM
Jeremiah J
Jeremiah J - avatar