How to sum a list in python using while loop

Simple use if statement with a while loop to calculate the Sum of n numbers in Python. Taken a number input from the user and stored it in a variable num.

Use a while loop to iterate until num gets zero. In every iteration, add the num to sum, and the value of num is decreased by 1.

Simple example code sum of natural numbers up to num.

num = 15
sum = 0

# use while loop to iterate until zero
while num > 0:
    sum += num
    num -= 1
print("The sum is", sum)

Output:

How to sum a list in python using while loop

User input number sum

sum = 0

num = int(input("Enter a number: "))
if num < 0:
    print("Please enter a positive number")
else:
    sum = 0

# use while loop to iterate until zero
while num > 0:
    sum += num
    num -= 1
print("The sum is", sum)

Output:

Enter a number: 10
The sum is 55

Do comment if you have any doubts or suggestions on this Python sum topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

How to sum a list in python using while loop

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

How to sum a list in python using while loop
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

General Knowledge

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

How to sum a list in python using while loop

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

How to sum a list in python using while loop

  • eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.

How do you sum a list in Python using a FOR loop?

If you need to sum numbers taken from user input in a for loop, use the input() function. Copied!.
Declare a new variable and set it to 0 ..
Use a for loop to iterate over a sequence of numbers..
Reassign the variable to its value plus the current number..

How do you sum a while loop?

Simple use if statement with a while loop to calculate the Sum of n numbers in Python. Taken a number input from the user and stored it in a variable num. Use a while loop to iterate until num gets zero. In every iteration, add the num to sum, and the value of num is decreased by 1.

How do you sum items in a list in Python?

Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

How do you print a sum of numbers in a while loop?

tot is a new variable here. You just need to update sum . So instead of tot = sum + number you'd do sum = sum + number and replace the tot in print statement too with sum . And btw, please use a different variable name for sum .