We can make use of the the built-in sum() function. Let's take a look at a few examples.
Example 1: Calculate the sum of a list of integers
int_numbers = [11, 5, 2, 17, 32, 81, 52]
sum_of_int_list = sum(int_numbers)
print(f"Sum of {int_numbers}: ", sum_of_int_list)
Output:
Sum of [11, 5, 2, 17, 32, 81, 52]: 200
Example 2: Calculate the sum of a list of floats
float_numbers = [11.2, 5.11, 2.75, 17.56, 32.43, 81.45, 52.24]
sum_of_float_list = sum(float_numbers)
print(f"Sum of {float_numbers}: ", sum_of_float_list)
Sum of [11.2, 5.11, 2.75, 17.56, 32.43, 81.45, 52.24]: 202.74
Example 3: Sum of random numbers
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- 33: Python Program to send an email vid GMail - Python
- Enable JSON Pretty Print in Java Jackson - Java
- How to add border to Android Button - Android
- Reset Font: Family, Style and Size of Notepad on Windows 11 to Default - Windows-11
- How to save webpage as pdf using macOS Safari - MacOS
- How to display only IP address using Terminal command - HowTos
- Add Text at Start and End of Each Line Notepad++ - NotepadPlusPlus
- Sublime Text 3 Convert Case to Upper, Lower, Title or Swap - Sublime-Text