1

Suppose I want to add several numbers together like: 1. Find even numbers between 1-100. 2. Find odd numbers between 2-200. 3. Add them.

So for this, I can check for even numbers and odd numbers respectively, but to add them, they must be stored somewhere. Now how can I do this?

i.e. store the output of the first step, store the output of second step and then add them together.

3
  • 1
    A list of numbers can be stored in a list. See your Python tutorial's section about lists. Commented Jan 23, 2013 at 9:21
  • 1
    You're actually wrong that the numbers have to be stored anywhere: you could just sum them directly as you find them. Commented Jan 23, 2013 at 9:28
  • Thankyou eumiro and Duncan. @bereal I never asked for a solution to the problem, the even and odd problem was just made to explain what I wanted to learn. I just wanted to know how can I store results in a list and use them later. My actual problem is different. That is why I used 'suppose' in the beginning. Cheers! Commented Jan 23, 2013 at 10:13

5 Answers 5

5

Find even numbers between 1-100:

>>> l = [i for i in range(1,101) if i % 2 == 0]
>>> print l
[2, 4, 6, ..., 100]

Find odd numbers between 2-200:

>>> l2 = [i for i in range(2,200) if i % 2 != 0]
>>> print l2
[3, 5, 7, ..., 199]

Find the sum:

>>> total = sum(l) + sum(l2)
>>> print total
12540

What I've done is List Comprehensions, a loop which creates values for whatever factors you want. Here's a link to the documentation about it: http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much everyone for helping me. So, in python, we can use range to generate a list of numbers and then use sum() to add them. But, in general ( in other programming languages or without using range) how can we store the numbers generated after a test ( like even or odd ) in a list or array to use that later to perform operations on it like addition or subtraction?
As a demonstration of list comprehension syntax, this is ok. But one should never, ever, build a list of consecutive even (or odd) numbers doing % 2 to each candidate: finding the first and then counting in steps of two is (almost) infinitely more efficient.
@user2003243 You can use other loops to get list of numbers. Here, one = 1 mylist = [] while one != 101: mylist.append(one) one += 1
No problem :). If you found it helpful, (I don't wish to sound greedy or anything :/) could you please one-up it or accept my answer to help other viewers who may see this forum later :).
I tried that as soon as you answered! But as this is my first question and I have zero reputation, I could not have that pleasure to vote up. :D
|
2

Even Numbers List:

a = [i for i in range(2,101,2)]

Odd Numbers List:

b = [i for i in range(3,200,2)]

Sum:

c = sum(a) + sum(b)

3 Comments

List b misses the number 1. So it should be range(1,200,2)
@Haidro the question is asking for odd number between 2 and 200
@nsconnetor my mistake, sorry
1

This is what containers like lists are for:

numbers = []  # Setup an empty list

for number in range(10):  # Loop over your numbers
    numbers.append(number)  # Append the number to your list

print sum(numbers)  # 45

Comments

0

Results of first and second steps can be stored in 2 different lists.

list1 = [2, 4, 6 .. ]
list2 = [1, 3, 5 .. ]

Lists are documented on python docs at http://docs.python.org/2/tutorial/datastructures.html#more-on-lists

Comments

0

You don't really need a list.

>>> sum(x for x in range(1,100) if x % 2)
2500
>>> sum(x for x in range(2,200) if not x % 2)
9900

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.