- Python Program Examples
- Python Program Examples
- Python Print Hello World
- Python Get Input from User
- Python Add Two Numbers
- Python Check Even or Odd
- Python Check Prime or Not
- Python Check Alphabet or Not
- Python Check Vowel or Not
- Python Check Leap Year or Not
- Python Check Reverse equal Original
- Python Check Positive Negative Zero
- Python Add Subtract Multiply Divide
- Python Make Simple Calculator
- Python Add Digits of Number
- Python Find Sum of Natural Numbers
- Python Find Average Percentage
- Python Find Average of n Numbers
- Python Find Grade of Student
- Python Print Multiplication Table
- Python Find Square Root of Number
- Python Print Prime Numbers
- Python Solve Quadratic Equation
- Find Numbers Divisible by Number
- Python Find Factors of Number
- Python Find Reverse of Number
- Python Swap Two Numbers
- Python Swap Two Variables
- Python Find Largest of Two Number
- Python Find Largest of Three Number
- Python Find Factorial of a Number
- Python Find HCF & LCM
- Python Find Area of Square
- Python Find Area of Rectangle
- Python Find Area of Triangle
- Python Find Area of Circle
- Python Find Perimeter of Square
- Python Find Perimeter of Rectangle
- Python Find Perimeter of Triangle
- Python Find Circumference of Circle
- Python Print ASCII Values
- Python Print Fibonacci Series
- Python Display Powers of 2
- Python Generate Armstrong Numbers
- Python Check Armstrong or Not
- Python Check Palindrome or Not
- Python Find nCr & nPr
- Python Kilometres to Miles
- Python Fahrenheit to Celsius
- Python Celsius to Fahrenheit
- Python Decimal to Binary
- Python Decimal to Octal
- Python Decimal to Hexadecimal
- Python Binary to Decimal
- Python Binary to Octal
- Python Binary to Hexadecimal
- Python Octal to Decimal
- Python Octal to Binary
- Python Octal to Hexadecimal
- Python Hexadecimal to Decimal
- Python Hexadecimal to Binary
- Python Hexadecimal to Octal
- Python Display Calendar
- Python Print Pattern Programs
- Python Print Floyd Triangle
- Python 1D Array Program
- Python Add Two Matrices
- Python Subtract Two Matrices
- Python Transpose Matrix
- Python Multiply Matrices
- Python Print String
- Python Find Length of String
- Python Compare Two Strings
- Python Copy String
- Python Concatenate String
- Python Reverse a String
- Python Remove Vowels from String
- Remove Punctuations from String
- Python Remove Word in String
- Python Count Character in String
- Python Count Each Vowels
- Python Count Word in Sentence
- Python Remove Spaces from String
- Python Sort String in Alphabetical
- Python Uppercase to Lowercase
- Python Lowercase to Uppercase
- Python Different Set of Operations
- Python Swap Two Strings
- Python Generate Random Numbers
- Python Read a File
- Python Write to File
- Python Copy Files
- Python Merge Two Files
- Python List Files in Directory
- Python Delete Files
- Python Print Date and Time
- Python Get IP Address
- Python Shutdown/Restart PC
- Python More Programs
- Python Sum of n Numbers
- Python Check Anagram Strings
- Python Add Binary Numbers
- Python Append Text to File
- Python Insert Element in List
- Python Delete Element from List
- Python Binary Search
- Python Bubble Sort
- Python Simple Interest
- Python Capitalize Character
- Python Capitalize Word in String
- Python Capitalize Word in File
- Python Tutorial
- Python Tutorial
- Give Online Test
- All Test List
- Python Online Test

In this article, you will learn and get code find the sum of **n** numbers entered by user using a Python program.
Here are the list of programs:

- Find sum of
**n**numbers using**for loop** - using
**while loop** - using
**list** - using user-defined
**function**

For example, if user enters the value of **n** as **3** and then three numbers as **1, 2, 3**. Then
the answer will be **1+2+3** or **6**.

This program finds the sum of **n** numbers using **for loop**. Here the value of **n** and then
**n** numbers must be entered by user as shown in the program given below:

# Find Sum of n Numbers using for Loop # ----codescracker.com---- sum = 0 print("Enter the Value of n: ") n = int(input()) print("Enter " + str(n) + " Numbers: ") for i in range(n): num = int(input()) sum = sum+num print("Sum of " + str(n) + " Numbers = " + str(sum))

Here is its sample run:

Now supply the input say **6** as value of **n** and then enter **6** numbers say **10, 20, 30, 40, 50, 60**.
Here is the sample output with exactly these inputs:

**Note - **The **str()** is used to convert from integer to string. Because the **+** operator in python
is used to concatenate only same type's value.

The following code:

for i in range(n):

is used to execute the following block of code:

num = int(input()) sum = sum+num

**n** number of times starting with value of **i** from 0 to **n-1**. For example, if user enters the value
of **n** as 10, then this block of code gets executed 10 times. Using this block, we've received 10 numbers and
adds the value and initialized to **sum**, one by one.

This program does the same job as of previous program, using **while loop** instead of **for**:

# ----codescracker.com---- sum = 0 i=0 print(end="Enter the Value of n: ") n = int(input()) print(end="Enter " + str(n) + " Numbers: ") while i<n: num = int(input()) sum = sum+num i = i+1 print("\nSum of " + str(n) + " Numbers = " + str(sum))

Here is its sample run, with user input **10** as value of **n** and **1, 2, 3, 4, 5, 6, 7, 8, 9, 10** as
10 numbers:

This program find and prints the addition result (or sum) of **n** numbers entered by user at run-time using
**list**. List is like an array:

# ----codescracker.com---- num = [] sum = 0 print(end="Enter the Value of n: ") n = int(input()) print(end="Enter " + str(n) + " Numbers: ") for i in range(n): num.insert(i, int(input())) for i in range(n): sum = sum+num[i] print("\nSum of " + str(n) + " Numbers = " + str(sum))

Here is its sample run with user input, **5** as value of **n** and **5, 6, 7, 10, 12** as 5 numbers:

The following code:

for i in range(n):

is used to execute following the statement:

num.insert(i, int(input()))

**n** number of times with **i**'s value from 0 to **n-1**. Therefore using **insert()**, the value
entered by user gets inserted to the list **num[]** one by one in this way:

- First value gets stored in
**num[i]**or**num[0]** - Second value gets stored in
**num[1]** - Third value gets stored in
**num[2]** - and so on

This is the last program of this article. This program also finds the sum of **n** numbers as done in previous
programs using different-different approaches. Like those, this program also approach in an another way
(using a user-defined function) to do the same task:

# ----codescracker.com---- def SumOfNNums(arr, tot): s = 0 for i in range(tot): s = s+arr[i] return s num = [] print(end="Enter the Value of n: ") n = int(input()) print(end="Enter " + str(n) + " Numbers: ") for i in range(n): num.insert(i, int(input())) sum = SumOfNNums(num, n) print("\nSum of " + str(n) + " Numbers = " + str(sum))

Here is its sample run with user input, **3** as value of **n** and **20, 30, 10** as 3 numbers:

With following statement:

sum = SumOfNNums(num, n)

we've called the function **SumOfNNums()**. So after executing this statement, the function gets executed and
the value return by function is the result, that is the summation of **n** numbers entered by user. So the return
value gets initialized to **sum**, and its value gets printed.