Computer Science, asked by Alisaraj185gmailcom, 5 months ago

write a programme in python to input a list cantaing a number and calculate the all odd element present in the list if the list is (1,2,3,4,5,6,7) then sum =1+3+5+7=16​

Answers

Answered by Oreki
2

\textsf{\large \textbf{One-Liner}}

  \texttt{print(`Sum of odd elements -',}\\\texttt{\hspace{5em} sum(i for i in [1, 2, 3, 4, 5, 6, 7] if i \% 2 != 0))}

\textsf{\large \textbf{Detailed}}

  \texttt{lst = [1, 2, 3, 4, 5, 6, 7]}\\\\\texttt{odd\_sum = 0}\\\texttt{for element in lst:}\\\texttt{\hspace{1em} if element \% 2 != 0:}\\\texttt{\hspace{2.4em} odd\_sum += element}\\\\\texttt{print("Sum of odd elements -", odd\_sum)}

\textsf{\large \textbf{After Execution}}

  \texttt{Sum of odd elements - 16}

Similar questions