Computer Science, asked by ineedanswerplease, 24 days ago

CS HW-2-7-2021:
l1=[1,2,3,4]
l2=[3,4,5,6]
write a python program that accepts 2 lists and add all the even numbers from
both lists which ever matchs​

Answers

Answered by Equestriadash
2

\tt l1\ =\ eval(in put("Enter\ a\ list:\ "))\\l2\ =\ eval(in put("Enter\ a\ list:\ "))\\s\ =\ 0\\for\ i\ in\ l1:\\{\ \ \ \ \ }if\ i\ in\ l2\ and\ i\%2\ ==\ 0:\\{\ \ \ \ \ }{\ \ \ \ \ }s\ =\ s\ +\ i\\print(s,\ "is\ the\ sum\ of\ the\ even\ numbers\ present\ in\ both\ lists.")

Once both lists have been entered, we assign a variable to represent the sum of the even numbers that exist in both lists. To get the sum, we start a for loop, which is an iteration statement that performs repeated checking over a given range. We use a conditional statement to check if the element is present in the second list as well, and if it's divisible by 2. If it satisfies both conditions, it gets added to the variable earlier assigned to represent the sum.

We used \bf eval() as it accepts any data type and proceeds with the upcoming statements according to the data type.

Similar questions