wap in python to display the given series.1,2,512,29,70.......
Answers
Answer:
Let’s start with a couple of nitpicks.
You’re not asking for the sum of those numbers. You’re asking for an example of source code, in Python, which can compute that sum (and, presumably, provide the result as output).
Also the sum of all numbers between 1 and 50 is infinite. The sum of all the integers in that range is a bit more tractable.
Now then … moving on to the question: this is a trivial programming exercise. You should not have to post on a public forum, potentially consuming the time and efforts of millions of forum participants with something that you should be able to figure out with only a modicum of effort. So it’s reasonable for us to beg the question, to question your intentions and even to rebuke you for doing so.
Are you trying to learn Python? Have you ever used any programming language? Did you try a search: Google: python sum of integers in range?
That said here’s the simple example: python -c "print(sum(range(1,51)))"
This is a complete command as it would be entered (or pasted) into a command prompt (Unix, Linux, or MacOS shell, MS-Windows command prompt, etc).
It calls the print() function (Python 3.x). If you’re using earlier versions of Python then this is, technically, calling the print statement with an expression that just happens to be contained in an extraneous, albeit harmless, set of parentheses.
This print() function (or statement) is called on the results of the sum() builtin function. That’s a function which iterates over a sequence of numbers (or any sort of iterable yielding numeric data).
That call to sum() is processing the values yielded from a call to the range() builtin function. The range() function in different versions of Python either returns a list (sequence) of integer values, or it yields the integers in some range (and optionally by some stepping value).
I’m supplying the starting and stopping values to the range() function. It would be harmless to call it with range(51) … omitting the first argument which defaults to zero. That’s harmless when calling sum() because, of course, zero is the additive identity; you can add it as many times as you like to a sum without changing the result. I call it with a stop argument of 51 because Python ranges are inclusive from the start but exclusive of their end point … they go from some number, including it, up to (but not including) another number).
There’s a reason for all this extra verbiage. All these details about print functions vs. print statements, of sequences and iterables, and of inclusive vs. exclusive range terminators.
Learning how to program is more than simply copying and pasting code examples. It’s important to understand certain nuances, semantics, of how Python (or any other programming language interpreter or compiler) works in order to create meaningful and non-trivial snippets of code.
Go take a tutorial. There are many of them available freely for Python with audio-visual lectures and interactive, web-based, exercises.
11.3K viewsView 8 Upvoters
Related Questions
More Answers Below
How do you find the sum of a given range of integers in python?
What is the sum of 1 to 100?
What is the sum of 1 to 50?
Using the while structure, how do I write a Python program to find the sum of integers between 1 and 500 and print their sum?
How do I sum even numbers in range?
Question:-
Write a python program to display the series.
1,2,5,12,29,70
Logic:-
2=1*2+1
5=2*2+1
12=5*2+2
29=12*2+5
Program:-
n=int(input("Enter the number of terms for the series: "))
a, b, c=1,0,0
for i in range(0,n):
c=a+2*b
a=b
b=c
print(c, end=" ")