Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

Let's say that we are having some series as below:

1+2+3+....+n

In c with for loop, we can easily do this:

for(i=1;i<=n;i++)

{

    sum += i;

}

In Python, I am able to do this using while loop:

while(num <= n):

      sum += num

      num = num+1

But not with for loop.

1 Answer

0 votes
by
I guess its similar to C only

for i in range(n):

     sum+=i

Related questions

0 votes
1 answer
asked Dec 12, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Feb 14, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Dec 17, 2020 in Python by laddulakshana (16.4k points)

Browse Categories

...