Just to make a quick comment in the form of an answer. If you wanted to make a list of all the odd number in a between 0 and 10, the end points don't matter. If the list was from 0 to 11 then the end point matters. Just remember that range[0,11] = [0,1,2,3,4,5,6,7,8,9,10]
and won't include 11.
Now to explain the generator that is being used. To make to list using numbers
as defined by you, you could do this.
odds = []
for num in numbers:
if num % 2 == 1:
odds.append[num]
This would give you odds = [1,3,5,7,9]
. Python has something call list
comprehension that make it really easy to do things like this. The above can be easily written like this
odds = [num for num in number if num % 2 == 1]
and since you want the sum off all the numbers in the list and the sum
function takes list, the answer is just
sum[[num for num in number if num % 2 == 1]]
Note that most of the answer don't have brackets. This is because without it it becomes a generator.
odds = [num for num in number if num % 2 == ]
print odds #-->
odds.next[] # 1
odds.next[] # 3
...
Since sum
takes these as well and generator are faster and less memory intensive than list that is why the best answer is
sum[num for num in numbers if num % 2 == 1]
The following article shows how given an integer list, we can produce the sum of all its odd and even digits.
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.
Method 1 : Using loop, str[] and int[]
In this, we first convert each element to string and then iterate for each of its element, and add to respective summation by conversion to integer.
Python3
test_list
=
[
345
,
893
,
1948
,
34
,
2346
]
print
[
"The original list is : "
+
str
[test_list]]
odd_sum
=
0
even_sum
=
0
for
sub
in
test_list:
for
ele
in
str
[sub]:
if
int
[ele]
%
2
=
=
0
:
even_sum
+
=
int
[ele]
else
:
odd_sum
+
=
int
[ele]
print
[
"Odd digit sum : "
+
str
[odd_sum]]
print
[
"Even digit sum : "
+
str
[even_sum]]
Output
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 40
Method 2: Using loop and sum[]
In this, we perform task of getting summation using sum[], and loop is used to perform the task of iterating through each element.
Python3
test_list
=
[
345
,
893
,
1948
,
34
,
2346
]
print
[
"The original list is : "
+
str
[test_list]]
odd_sum
=
0
even_sum
=
0
for
sub
in
test_list:
odd_sum
+
=
sum
[[
int
[ele]
for
ele
in
str
[sub]
if
int
[ele]
%
2
=
=
1
]]
even_sum
+
=
sum
[[
int
[ele]
for
ele
in
str
[sub]
if
int
[ele]
%
2
=
=
0
]]
print
[
"Odd digit sum : "
+
str
[odd_sum]]
print
[
"Even digit sum : "
+
str
[even_sum]]
Output
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 40
Method 3: Using list comprehension
Python3
test_list
=
[
345
,
893
,
1948
,
34
,
2346
]
odd_sum
=
0
even_sum
=
0
odd_sum
+
=
sum
[[
int
[ele]
for
sub
in
test_list
for
ele
in
str
[sub]
if
int
[ele]
%
2
=
=
1
]]
even_sum
+
=
sum
[[
int
[ele]
for
sub
in
test_list
for
ele
in
str
[sub]
if
int
[ele]
%
2
=
=
0
]]
print
[
"Odd digit sum : "
+
str
[odd_sum]]
print
[
"Even digit sum : "
+
str
[even_sum]]
Output
Odd digit sum : 36 Even digit sum : 40
Method 4: Using the enumerate function
Python3
test_list
=
[
345
,
893
,
1948
,
34
,
2346
]
odd_sum
=
0
even_sum
=
0
odd_sum
+
=
sum
[[
int
[ele]
for
i, sub
in
enumerate
[test_list]
for
ele
in
str
[sub]
if
int
[ele]
%
2
=
=
1
]]
even_sum
+
=
sum
[[
int
[ele]
for
i, sub
in
enumerate
[test_list]
for
ele
in
str
[sub]
if
int
[ele]
%
2
=
=
0
]]
print
[
"Odd digit sum : "
+
str
[odd_sum]]
print
[
"Even digit sum : "
+
str
[even_sum]]
Output
Odd digit sum : 36 Even digit sum : 40