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. Output:
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.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.
- Home
- PHP
- MySQL
- MongoDB
- HTML
- Javascript
- Node.js
- Express.js
- Python
- Jquery
- R
- Kotlin
- DS
- Blogs
- Theory of Computation
General Knowledge
Learn Popular Language
A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities... In this article, we have mentioned the analyzed results of the best programming language for 2021... Python has a set of useful Libraries and Packages that minimize the use of code... In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more... In this
article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...Blogs
Stateful vs Stateless
Best programming language to learn in 2021
How is Python best for mobile app development?
Learn all about Emoji
Data Science Recruitment of Freshers
Follow us
- eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.